Skip to content

Commit 3369607

Browse files
authored
Feat: opus metadata encoding (#12974)
Feat: Downloading: Add opus audio metadata tags for title, author, date, and a comment tag with the originating URL This removes the DownloadManagerService.EXTRA_SOURCE field, which is always inferred from the StreamInfo.
1 parent 2e8e203 commit 3369607

File tree

5 files changed

+107
-18
lines changed

5 files changed

+107
-18
lines changed

app/src/main/java/org/schabi/newpipe/download/DownloadDialog.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1133,7 +1133,7 @@ private void continueSelectedDownload(@NonNull final StoredFileHelper storage) {
11331133
}
11341134

11351135
DownloadManagerService.startMission(context, urls, storage, kind, threads,
1136-
currentInfo.getUrl(), psName, psArgs, nearLength, new ArrayList<>(recoveryInfo));
1136+
currentInfo, psName, psArgs, nearLength, new ArrayList<>(recoveryInfo));
11371137

11381138
Toast.makeText(context, getString(R.string.download_has_started),
11391139
Toast.LENGTH_SHORT).show();

app/src/main/java/org/schabi/newpipe/streams/OggFromWebMWriter.java

Lines changed: 91 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
package org.schabi.newpipe.streams;
22

3+
import static org.schabi.newpipe.MainActivity.DEBUG;
4+
5+
import android.util.Log;
6+
import android.util.Pair;
7+
38
import androidx.annotation.NonNull;
49
import androidx.annotation.Nullable;
510

11+
import org.schabi.newpipe.extractor.stream.StreamInfo;
612
import org.schabi.newpipe.streams.WebMReader.Cluster;
713
import org.schabi.newpipe.streams.WebMReader.Segment;
814
import org.schabi.newpipe.streams.WebMReader.SimpleBlock;
@@ -13,6 +19,10 @@
1319
import java.io.IOException;
1420
import java.nio.ByteBuffer;
1521
import java.nio.ByteOrder;
22+
import java.time.format.DateTimeFormatter;
23+
import java.util.ArrayList;
24+
import java.util.List;
25+
import java.util.stream.Collectors;
1626

1727
/**
1828
* @author kapodamy
@@ -52,8 +62,10 @@ public class OggFromWebMWriter implements Closeable {
5262
private long segmentTableNextTimestamp = TIME_SCALE_NS;
5363

5464
private final int[] crc32Table = new int[256];
65+
private final StreamInfo streamInfo;
5566

56-
public OggFromWebMWriter(@NonNull final SharpStream source, @NonNull final SharpStream target) {
67+
public OggFromWebMWriter(@NonNull final SharpStream source, @NonNull final SharpStream target,
68+
@Nullable final StreamInfo streamInfo) {
5769
if (!source.canRead() || !source.canRewind()) {
5870
throw new IllegalArgumentException("source stream must be readable and allows seeking");
5971
}
@@ -63,6 +75,7 @@ public OggFromWebMWriter(@NonNull final SharpStream source, @NonNull final Sharp
6375

6476
this.source = source;
6577
this.output = target;
78+
this.streamInfo = streamInfo;
6679

6780
this.streamId = (int) System.currentTimeMillis();
6881

@@ -271,12 +284,31 @@ private int makePacketheader(final long granPos, @NonNull final ByteBuffer buffe
271284

272285
@Nullable
273286
private byte[] makeMetadata() {
287+
if (DEBUG) {
288+
Log.d("OggFromWebMWriter", "Downloading media with codec ID " + webmTrack.codecId);
289+
}
290+
274291
if ("A_OPUS".equals(webmTrack.codecId)) {
275-
return new byte[]{
276-
0x4F, 0x70, 0x75, 0x73, 0x54, 0x61, 0x67, 0x73, // "OpusTags" binary string
277-
0x00, 0x00, 0x00, 0x00, // writing application string size (not present)
278-
0x00, 0x00, 0x00, 0x00 // additional tags count (zero means no tags)
279-
};
292+
final var metadata = new ArrayList<Pair<String, String>>();
293+
if (streamInfo != null) {
294+
metadata.add(Pair.create("COMMENT", streamInfo.getUrl()));
295+
metadata.add(Pair.create("GENRE", streamInfo.getCategory()));
296+
metadata.add(Pair.create("ARTIST", streamInfo.getUploaderName()));
297+
metadata.add(Pair.create("TITLE", streamInfo.getName()));
298+
metadata.add(Pair.create("DATE", streamInfo
299+
.getUploadDate()
300+
.getLocalDateTime()
301+
.format(DateTimeFormatter.ISO_DATE)));
302+
}
303+
304+
if (DEBUG) {
305+
Log.d("OggFromWebMWriter", "Creating metadata header with this data:");
306+
metadata.forEach(p -> {
307+
Log.d("OggFromWebMWriter", p.first + "=" + p.second);
308+
});
309+
}
310+
311+
return makeOpusTagsHeader(metadata);
280312
} else if ("A_VORBIS".equals(webmTrack.codecId)) {
281313
return new byte[]{
282314
0x03, // ¿¿¿???
@@ -290,6 +322,59 @@ private byte[] makeMetadata() {
290322
return null;
291323
}
292324

325+
/**
326+
* This creates a single metadata tag for use in opus metadata headers. It contains the four
327+
* byte string length field and includes the string as-is. This cannot be used independently,
328+
* but must follow a proper "OpusTags" header.
329+
*
330+
* @param pair A key-value pair in the format "KEY=some value"
331+
* @return The binary data of the encoded metadata tag
332+
*/
333+
private static byte[] makeOpusMetadataTag(final Pair<String, String> pair) {
334+
final var keyValue = pair.first.toUpperCase() + "=" + pair.second.trim();
335+
336+
final var bytes = keyValue.getBytes();
337+
final var buf = ByteBuffer.allocate(4 + bytes.length);
338+
buf.order(ByteOrder.LITTLE_ENDIAN);
339+
buf.putInt(bytes.length);
340+
buf.put(bytes);
341+
return buf.array();
342+
}
343+
344+
/**
345+
* This returns a complete "OpusTags" header, created from the provided metadata tags.
346+
* <p>
347+
* You probably want to use makeOpusMetadata(), which uses this function to create
348+
* a header with sensible metadata filled in.
349+
*
350+
* @param keyValueLines A list of pairs of the tags. This can also be though of as a mapping
351+
* from one key to multiple values.
352+
* @return The binary header
353+
*/
354+
private static byte[] makeOpusTagsHeader(final List<Pair<String, String>> keyValueLines) {
355+
final var tags = keyValueLines
356+
.stream()
357+
.filter(p -> !p.second.isBlank())
358+
.map(OggFromWebMWriter::makeOpusMetadataTag)
359+
.collect(Collectors.toUnmodifiableList());
360+
361+
final var tagsBytes = tags.stream().collect(Collectors.summingInt(arr -> arr.length));
362+
363+
// Fixed header fields + dynamic fields
364+
final var byteCount = 16 + tagsBytes;
365+
366+
final var head = ByteBuffer.allocate(byteCount);
367+
head.order(ByteOrder.LITTLE_ENDIAN);
368+
head.put(new byte[]{
369+
0x4F, 0x70, 0x75, 0x73, 0x54, 0x61, 0x67, 0x73, // "OpusTags" binary string
370+
0x00, 0x00, 0x00, 0x00, // vendor (aka. Encoder) string of length 0
371+
});
372+
head.putInt(tags.size()); // 4 bytes for tag count
373+
tags.forEach(head::put); // dynamic amount of tag bytes
374+
375+
return head.array();
376+
}
377+
293378
private void write(final ByteBuffer buffer) throws IOException {
294379
output.write(buffer.array(), 0, buffer.position());
295380
buffer.position(0);

app/src/main/java/us/shandian/giga/postprocessing/OggFromWebmDemuxer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ boolean test(SharpStream... sources) throws IOException {
3434

3535
@Override
3636
int process(SharpStream out, @NonNull SharpStream... sources) throws IOException {
37-
OggFromWebMWriter demuxer = new OggFromWebMWriter(sources[0], out);
37+
OggFromWebMWriter demuxer = new OggFromWebMWriter(sources[0], out, streamInfo);
3838
demuxer.parseSource();
3939
demuxer.selectTrack(0);
4040
demuxer.build();

app/src/main/java/us/shandian/giga/postprocessing/Postprocessing.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import androidx.annotation.NonNull;
66

7+
import org.schabi.newpipe.extractor.stream.StreamInfo;
78
import org.schabi.newpipe.streams.io.SharpStream;
89

910
import java.io.File;
@@ -30,7 +31,8 @@ public abstract class Postprocessing implements Serializable {
3031
public transient static final String ALGORITHM_M4A_NO_DASH = "mp4D-m4a";
3132
public transient static final String ALGORITHM_OGG_FROM_WEBM_DEMUXER = "webm-ogg-d";
3233

33-
public static Postprocessing getAlgorithm(@NonNull String algorithmName, String[] args) {
34+
public static Postprocessing getAlgorithm(@NonNull String algorithmName, String[] args,
35+
StreamInfo streamInfo) {
3436
Postprocessing instance;
3537

3638
switch (algorithmName) {
@@ -56,6 +58,7 @@ public static Postprocessing getAlgorithm(@NonNull String algorithmName, String[
5658
}
5759

5860
instance.args = args;
61+
instance.streamInfo = streamInfo;
5962
return instance;
6063
}
6164

@@ -75,8 +78,8 @@ public static Postprocessing getAlgorithm(@NonNull String algorithmName, String[
7578
*/
7679
private final String name;
7780

78-
7981
private String[] args;
82+
protected StreamInfo streamInfo;
8083

8184
private transient DownloadMission mission;
8285

app/src/main/java/us/shandian/giga/service/DownloadManagerService.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040

4141
import org.schabi.newpipe.R;
4242
import org.schabi.newpipe.download.DownloadActivity;
43+
import org.schabi.newpipe.extractor.stream.StreamInfo;
4344
import org.schabi.newpipe.player.helper.LockManager;
4445
import org.schabi.newpipe.streams.io.StoredDirectoryHelper;
4546
import org.schabi.newpipe.streams.io.StoredFileHelper;
@@ -74,12 +75,12 @@ public class DownloadManagerService extends Service {
7475
private static final String EXTRA_THREADS = "DownloadManagerService.extra.threads";
7576
private static final String EXTRA_POSTPROCESSING_NAME = "DownloadManagerService.extra.postprocessingName";
7677
private static final String EXTRA_POSTPROCESSING_ARGS = "DownloadManagerService.extra.postprocessingArgs";
77-
private static final String EXTRA_SOURCE = "DownloadManagerService.extra.source";
7878
private static final String EXTRA_NEAR_LENGTH = "DownloadManagerService.extra.nearLength";
7979
private static final String EXTRA_PATH = "DownloadManagerService.extra.storagePath";
8080
private static final String EXTRA_PARENT_PATH = "DownloadManagerService.extra.storageParentPath";
8181
private static final String EXTRA_STORAGE_TAG = "DownloadManagerService.extra.storageTag";
8282
private static final String EXTRA_RECOVERY_INFO = "DownloadManagerService.extra.recoveryInfo";
83+
private static final String EXTRA_STREAM_INFO = "DownloadManagerService.extra.streamInfo";
8384

8485
private static final String ACTION_RESET_DOWNLOAD_FINISHED = APPLICATION_ID + ".reset_download_finished";
8586
private static final String ACTION_OPEN_DOWNLOADS_FINISHED = APPLICATION_ID + ".open_downloads_finished";
@@ -353,28 +354,28 @@ public void updateForegroundState(boolean state) {
353354
* @param kind type of file (a: audio v: video s: subtitle ?: file-extension defined)
354355
* @param threads the number of threads maximal used to download chunks of the file.
355356
* @param psName the name of the required post-processing algorithm, or {@code null} to ignore.
356-
* @param source source url of the resource
357+
* @param streamInfo stream metadata that may be written into the downloaded file.
357358
* @param psArgs the arguments for the post-processing algorithm.
358359
* @param nearLength the approximated final length of the file
359360
* @param recoveryInfo array of MissionRecoveryInfo, in case is required recover the download
360361
*/
361362
public static void startMission(Context context, String[] urls, StoredFileHelper storage,
362-
char kind, int threads, String source, String psName,
363+
char kind, int threads, StreamInfo streamInfo, String psName,
363364
String[] psArgs, long nearLength,
364365
ArrayList<MissionRecoveryInfo> recoveryInfo) {
365366
final Intent intent = new Intent(context, DownloadManagerService.class)
366367
.setAction(Intent.ACTION_RUN)
367368
.putExtra(EXTRA_URLS, urls)
368369
.putExtra(EXTRA_KIND, kind)
369370
.putExtra(EXTRA_THREADS, threads)
370-
.putExtra(EXTRA_SOURCE, source)
371371
.putExtra(EXTRA_POSTPROCESSING_NAME, psName)
372372
.putExtra(EXTRA_POSTPROCESSING_ARGS, psArgs)
373373
.putExtra(EXTRA_NEAR_LENGTH, nearLength)
374374
.putExtra(EXTRA_RECOVERY_INFO, recoveryInfo)
375375
.putExtra(EXTRA_PARENT_PATH, storage.getParentUri())
376376
.putExtra(EXTRA_PATH, storage.getUri())
377-
.putExtra(EXTRA_STORAGE_TAG, storage.getTag());
377+
.putExtra(EXTRA_STORAGE_TAG, storage.getTag())
378+
.putExtra(EXTRA_STREAM_INFO, streamInfo);
378379

379380
context.startService(intent);
380381
}
@@ -387,9 +388,9 @@ private void startMission(Intent intent) {
387388
char kind = intent.getCharExtra(EXTRA_KIND, '?');
388389
String psName = intent.getStringExtra(EXTRA_POSTPROCESSING_NAME);
389390
String[] psArgs = intent.getStringArrayExtra(EXTRA_POSTPROCESSING_ARGS);
390-
String source = intent.getStringExtra(EXTRA_SOURCE);
391391
long nearLength = intent.getLongExtra(EXTRA_NEAR_LENGTH, 0);
392392
String tag = intent.getStringExtra(EXTRA_STORAGE_TAG);
393+
StreamInfo streamInfo = (StreamInfo)intent.getSerializableExtra(EXTRA_STREAM_INFO);
393394
final var recovery = IntentCompat.getParcelableArrayListExtra(intent, EXTRA_RECOVERY_INFO,
394395
MissionRecoveryInfo.class);
395396
Objects.requireNonNull(recovery);
@@ -405,11 +406,11 @@ private void startMission(Intent intent) {
405406
if (psName == null)
406407
ps = null;
407408
else
408-
ps = Postprocessing.getAlgorithm(psName, psArgs);
409+
ps = Postprocessing.getAlgorithm(psName, psArgs, streamInfo);
409410

410411
final DownloadMission mission = new DownloadMission(urls, storage, kind, ps);
411412
mission.threadCount = threads;
412-
mission.source = source;
413+
mission.source = streamInfo.getUrl();
413414
mission.nearLength = nearLength;
414415
mission.recoveryInfo = recovery.toArray(new MissionRecoveryInfo[0]);
415416

0 commit comments

Comments
 (0)