Skip to content

Commit 529b8ed

Browse files
committed
Add option to DownloadDialog to enable metadata embedding
Metadata embedding is disabled by default across all post-processing algorithms. A new parameter and variable is introduced although there is the parameter 'args' already. The information on whether the metadata is going to be embedded needs to be parsed by every audio or video post-processing algorithm anyway. Getting it from the list is more difficult and less error-prone than creating a new param.
1 parent b904679 commit 529b8ed

File tree

12 files changed

+68
-25
lines changed

12 files changed

+68
-25
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,6 +1036,7 @@ private void continueSelectedDownload(@NonNull final StoredFileHelper storage) {
10361036
final Stream selectedStream;
10371037
Stream secondaryStream = null;
10381038
final char kind;
1039+
final boolean embedMetadata = dialogBinding.metadataSwitch.isChecked();
10391040
int threads = dialogBinding.threads.getProgress() + 1;
10401041
final String[] urls;
10411042
final List<MissionRecoveryInfo> recoveryInfo;
@@ -1119,8 +1120,8 @@ private void continueSelectedDownload(@NonNull final StoredFileHelper storage) {
11191120
);
11201121
}
11211122

1122-
DownloadManagerService.startMission(context, urls, storage, kind, threads,
1123-
currentInfo, psName, psArgs, nearLength, new ArrayList<>(recoveryInfo));
1123+
DownloadManagerService.startMission(context, urls, storage, kind, threads, currentInfo,
1124+
psName, embedMetadata, psArgs, nearLength, new ArrayList<>(recoveryInfo));
11241125

11251126
Toast.makeText(context, getString(R.string.download_has_started),
11261127
Toast.LENGTH_SHORT).show();

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,11 @@ public class Mp4FromDashWriter {
6464
private final ArrayList<Integer> compatibleBrands = new ArrayList<>(5);
6565

6666

67+
private final boolean embedMetadata;
6768
private final Mp4MetadataHelper metadataHelper;
6869

69-
public Mp4FromDashWriter(final StreamInfo streamInfo,
70+
public Mp4FromDashWriter(final boolean embedMetadata,
71+
final StreamInfo streamInfo,
7072
final Bitmap thumbnail,
7173
final SharpStream... sources) throws IOException {
7274
for (final SharpStream src : sources) {
@@ -75,6 +77,7 @@ public Mp4FromDashWriter(final StreamInfo streamInfo,
7577
}
7678
}
7779

80+
this.embedMetadata = embedMetadata;
7881
this.metadataHelper = new Mp4MetadataHelper(
7982
this::auxOffset,
8083
buffer -> {
@@ -750,7 +753,9 @@ private int makeMoov(final int[] defaultMediaTime, final TablesInfo[] tablesInfo
750753

751754
makeMvhd(longestTrack);
752755

753-
metadataHelper.makeUdta();
756+
if (embedMetadata) {
757+
metadataHelper.makeUdta();
758+
}
754759

755760
for (int i = 0; i < tracks.length; i++) {
756761
if (tracks[i].trak.tkhd.matrix.length != 36) {

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,18 +121,21 @@ public class OggFromWebMWriter implements Closeable {
121121
private long segmentTableNextTimestamp = TIME_SCALE_NS;
122122

123123
private final int[] crc32Table = new int[256];
124+
private final boolean embedMetadata;
124125
private final StreamInfo streamInfo;
125126
private final Bitmap thumbnail;
126127

127128
/**
128129
* Constructor of OggFromWebMWriter.
129130
* @param source
130131
* @param target
132+
* @param embedMetadata whether to embed metadata in the output Ogg stream
131133
* @param streamInfo the stream info
132134
* @param thumbnail the thumbnail bitmap used as cover art
133135
*/
134136
public OggFromWebMWriter(@NonNull final SharpStream source,
135137
@NonNull final SharpStream target,
138+
final boolean embedMetadata,
136139
@Nullable final StreamInfo streamInfo,
137140
@Nullable final Bitmap thumbnail) {
138141
if (!source.canRead() || !source.canRewind()) {
@@ -144,6 +147,7 @@ public OggFromWebMWriter(@NonNull final SharpStream source,
144147

145148
this.source = source;
146149
this.output = target;
150+
this.embedMetadata = embedMetadata;
147151
this.streamInfo = streamInfo;
148152
this.thumbnail = thumbnail;
149153

@@ -264,9 +268,11 @@ public void build() throws IOException {
264268
}
265269

266270
/* step 3: create packet with metadata */
267-
final byte[] buffer = makeCommentHeader();
268-
if (buffer != null) {
269-
addPacketSegmentMultiPage(buffer, header);
271+
if (embedMetadata) {
272+
final byte[] buffer = makeCommentHeader();
273+
if (buffer != null) {
274+
addPacketSegmentMultiPage(buffer, header);
275+
}
270276
}
271277

272278
/* step 4: calculate amount of packets */

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ boolean test(SharpStream... sources) throws IOException {
3131
@Override
3232
int process(SharpStream out, SharpStream... sources) throws IOException {
3333
Mp4FromDashWriter muxer = new Mp4FromDashWriter(
34-
this.streamInfo, this.thumbnail, sources[0]);
34+
this.embedMetadata, this.streamInfo, this.thumbnail, sources[0]);
3535
muxer.setMainBrand(0x4D344120);// binary string "M4A "
3636
muxer.parseSources();
3737
muxer.selectTracks(0);

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ public class Mp3Metadata extends Postprocessing {
5050
super(true, true, ALGORITHM_MP3_METADATA);
5151
}
5252

53+
@Override
54+
boolean test(SharpStream... sources) {
55+
return this.embedMetadata;
56+
}
57+
5358
@Override
5459
int process(SharpStream out, SharpStream... sources) throws IOException {
5560
if (sources == null || sources.length == 0 || sources[0] == null) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ class Mp4FromDashMuxer extends Postprocessing {
1616

1717
@Override
1818
int process(SharpStream out, SharpStream... sources) throws IOException {
19-
Mp4FromDashWriter muxer = new Mp4FromDashWriter(this.streamInfo, this.thumbnail, sources);
19+
Mp4FromDashWriter muxer = new Mp4FromDashWriter(
20+
this.embedMetadata, this.streamInfo, this.thumbnail, sources);
2021
muxer.parseSources();
2122
muxer.selectTracks(0, 0);
2223
muxer.build(out);

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ public class Mp4Metadata extends Postprocessing {
3232

3333
@Override
3434
boolean test(SharpStream... sources) throws IOException {
35+
// nothing to do if metadata should not be embedded
36+
if (!embedMetadata) return false;
37+
3538
// quick check: ensure there's at least one source and it looks like an MP4,
3639
// i.e. the file has a 'moov' box near the beginning.
3740
// THe 'udta' box is inserted inside 'moov', so if there's no 'moov' we can't do anything.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ boolean test(SharpStream... sources) throws IOException {
3535
@Override
3636
int process(SharpStream out, @NonNull SharpStream... sources) throws IOException {
3737
OggFromWebMWriter demuxer = new OggFromWebMWriter(
38-
sources[0], out, streamInfo, thumbnail);
38+
sources[0], out, embedMetadata, streamInfo, thumbnail);
3939
demuxer.parseSource();
4040
demuxer.selectTrack(0);
4141
demuxer.build();

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ public abstract class Postprocessing implements Serializable {
3535
public static final String ALGORITHM_M4A_NO_DASH = "mp4D-m4a";
3636
public static final String ALGORITHM_OGG_FROM_WEBM_DEMUXER = "webm-ogg-d";
3737

38-
public static Postprocessing getAlgorithm(@NonNull String algorithmName, String[] args,
38+
public static Postprocessing getAlgorithm(@NonNull String algorithmName,
39+
boolean embedMetadata,
40+
String[] args,
3941
@NonNull StreamInfo streamInfo) {
4042
Postprocessing instance;
4143

@@ -67,6 +69,7 @@ public static Postprocessing getAlgorithm(@NonNull String algorithmName, String[
6769

6870
instance.args = args;
6971
instance.streamInfo = streamInfo;
72+
instance.embedMetadata = embedMetadata;
7073
return instance;
7174
}
7275

@@ -88,6 +91,11 @@ public static Postprocessing getAlgorithm(@NonNull String algorithmName, String[
8891

8992
private String[] args;
9093

94+
/**
95+
* Indicates whether the metadata should be embedded in the file or not.
96+
*/
97+
boolean embedMetadata;
98+
9199
/**
92100
* StreamInfo object related to the current download
93101
*/

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

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

4141
import org.schabi.newpipe.R;
4242
import org.schabi.newpipe.download.DownloadActivity;
43-
import org.schabi.newpipe.extractor.Image;
4443
import org.schabi.newpipe.extractor.stream.StreamInfo;
4544
import org.schabi.newpipe.player.helper.LockManager;
4645
import org.schabi.newpipe.streams.io.StoredDirectoryHelper;
@@ -75,6 +74,7 @@ public class DownloadManagerService extends Service {
7574
private static final String EXTRA_KIND = "DownloadManagerService.extra.kind";
7675
private static final String EXTRA_THREADS = "DownloadManagerService.extra.threads";
7776
private static final String EXTRA_POSTPROCESSING_NAME = "DownloadManagerService.extra.postprocessingName";
77+
private static final String EXTRA_POSTPROCESSING_METADATA = "DownloadManagerService.extra.postprocessingMetadata";
7878
private static final String EXTRA_POSTPROCESSING_ARGS = "DownloadManagerService.extra.postprocessingArgs";
7979
private static final String EXTRA_NEAR_LENGTH = "DownloadManagerService.extra.nearLength";
8080
private static final String EXTRA_PATH = "DownloadManagerService.extra.storagePath";
@@ -349,27 +349,29 @@ public void updateForegroundState(boolean state) {
349349
/**
350350
* Start a new download mission
351351
*
352-
* @param context the activity context
353-
* @param urls array of urls to download
354-
* @param storage where the file is saved
355-
* @param kind type of file (a: audio v: video s: subtitle ?: file-extension defined)
356-
* @param threads the number of threads maximal used to download chunks of the file.
357-
* @param psName the name of the required post-processing algorithm, or {@code null} to ignore.
358-
* @param streamInfo stream metadata that may be written into the downloaded file.
359-
* @param psArgs the arguments for the post-processing algorithm.
360-
* @param nearLength the approximated final length of the file
361-
* @param recoveryInfo array of MissionRecoveryInfo, in case is required recover the download
352+
* @param context the activity context
353+
* @param urls array of urls to download
354+
* @param storage where the file is saved
355+
* @param kind type of file (a: audio v: video s: subtitle ?: file-extension defined)
356+
* @param threads the number of threads maximal used to download chunks of the file.
357+
* @param streamInfo stream metadata that may be written into the downloaded file.
358+
* @param psName the name of the required post-processing algorithm, or {@code null} to ignore.
359+
* @param embedMetadata whether the metadata should be embedded into the downloaded file.
360+
* @param psArgs the arguments for the post-processing algorithm.
361+
* @param nearLength the approximated final length of the file
362+
* @param recoveryInfo array of MissionRecoveryInfo, in case is required recover the download
362363
*/
363364
public static void startMission(Context context, String[] urls, StoredFileHelper storage,
364365
char kind, int threads, StreamInfo streamInfo, String psName,
365-
String[] psArgs, long nearLength,
366+
boolean embedMetadata, String[] psArgs, long nearLength,
366367
ArrayList<MissionRecoveryInfo> recoveryInfo) {
367368
final Intent intent = new Intent(context, DownloadManagerService.class)
368369
.setAction(Intent.ACTION_RUN)
369370
.putExtra(EXTRA_URLS, urls)
370371
.putExtra(EXTRA_KIND, kind)
371372
.putExtra(EXTRA_THREADS, threads)
372373
.putExtra(EXTRA_POSTPROCESSING_NAME, psName)
374+
.putExtra(EXTRA_POSTPROCESSING_METADATA, embedMetadata)
373375
.putExtra(EXTRA_POSTPROCESSING_ARGS, psArgs)
374376
.putExtra(EXTRA_NEAR_LENGTH, nearLength)
375377
.putExtra(EXTRA_RECOVERY_INFO, recoveryInfo)
@@ -388,6 +390,7 @@ private void startMission(Intent intent) {
388390
int threads = intent.getIntExtra(EXTRA_THREADS, 1);
389391
char kind = intent.getCharExtra(EXTRA_KIND, '?');
390392
String psName = intent.getStringExtra(EXTRA_POSTPROCESSING_NAME);
393+
boolean embedMetadata = intent.getBooleanExtra(EXTRA_POSTPROCESSING_METADATA, false);
391394
String[] psArgs = intent.getStringArrayExtra(EXTRA_POSTPROCESSING_ARGS);
392395
long nearLength = intent.getLongExtra(EXTRA_NEAR_LENGTH, 0);
393396
String tag = intent.getStringExtra(EXTRA_STORAGE_TAG);
@@ -407,7 +410,7 @@ private void startMission(Intent intent) {
407410
if (psName == null)
408411
ps = null;
409412
else
410-
ps = Postprocessing.getAlgorithm(psName, psArgs, streamInfo);
413+
ps = Postprocessing.getAlgorithm(psName, embedMetadata, psArgs, streamInfo);
411414

412415
final DownloadMission mission = new DownloadMission(
413416
urls, storage, kind, ps, streamInfo, getApplicationContext());

0 commit comments

Comments
 (0)