Skip to content

Commit 6d59cdb

Browse files
committed
Add support for extracting audio tracks.
1 parent 43d1c1f commit 6d59cdb

3 files changed

Lines changed: 116 additions & 12 deletions

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/ItagItem.java

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
package org.schabi.newpipe.extractor.services.youtube;
22

3+
import org.schabi.newpipe.extractor.MediaFormat;
4+
import org.schabi.newpipe.extractor.exceptions.ParsingException;
5+
6+
import javax.annotation.Nonnull;
7+
import javax.annotation.Nullable;
8+
import java.io.Serializable;
9+
310
import static org.schabi.newpipe.extractor.MediaFormat.M4A;
411
import static org.schabi.newpipe.extractor.MediaFormat.MPEG_4;
512
import static org.schabi.newpipe.extractor.MediaFormat.WEBM;
@@ -10,14 +17,6 @@
1017
import static org.schabi.newpipe.extractor.services.youtube.ItagItem.ItagType.VIDEO;
1118
import static org.schabi.newpipe.extractor.services.youtube.ItagItem.ItagType.VIDEO_ONLY;
1219

13-
import org.schabi.newpipe.extractor.MediaFormat;
14-
import org.schabi.newpipe.extractor.exceptions.ParsingException;
15-
16-
import javax.annotation.Nonnull;
17-
import javax.annotation.Nullable;
18-
19-
import java.io.Serializable;
20-
2120
public class ItagItem implements Serializable {
2221

2322
/**
@@ -211,18 +210,24 @@ public MediaFormat getMediaFormat() {
211210
public final ItagType itagType;
212211

213212
// Audio fields
214-
/** @deprecated Use {@link #getAverageBitrate()} instead. */
213+
/**
214+
* @deprecated Use {@link #getAverageBitrate()} instead.
215+
*/
215216
@Deprecated
216217
public int avgBitrate = AVERAGE_BITRATE_UNKNOWN;
217218
private int sampleRate = SAMPLE_RATE_UNKNOWN;
218219
private int audioChannels = AUDIO_CHANNELS_NOT_APPLICABLE_OR_UNKNOWN;
219220

220221
// Video fields
221-
/** @deprecated Use {@link #getResolutionString()} instead. */
222+
/**
223+
* @deprecated Use {@link #getResolutionString()} instead.
224+
*/
222225
@Deprecated
223226
public String resolutionString;
224227

225-
/** @deprecated Use {@link #getFps()} and {@link #setFps(int)} instead. */
228+
/**
229+
* @deprecated Use {@link #getFps()} and {@link #setFps(int)} instead.
230+
*/
226231
@Deprecated
227232
public int fps = FPS_NOT_APPLICABLE_OR_UNKNOWN;
228233

@@ -239,6 +244,8 @@ public MediaFormat getMediaFormat() {
239244
private int targetDurationSec = TARGET_DURATION_SEC_UNKNOWN;
240245
private long approxDurationMs = APPROX_DURATION_MS_UNKNOWN;
241246
private long contentLength = CONTENT_LENGTH_UNKNOWN;
247+
private String audioTrackId;
248+
private String audioTrackName;
242249

243250
public int getBitrate() {
244251
return bitrate;
@@ -539,4 +546,42 @@ public long getContentLength() {
539546
public void setContentLength(final long contentLength) {
540547
this.contentLength = contentLength > 0 ? contentLength : CONTENT_LENGTH_UNKNOWN;
541548
}
549+
550+
/**
551+
* Get the {@code audioTrackId} of the stream, if present.
552+
*
553+
* @return the {@code audioTrackId} of the stream or null
554+
*/
555+
@Nullable
556+
public String getAudioTrackId() {
557+
return audioTrackId;
558+
}
559+
560+
/**
561+
* Set the {@code audioTrackId} of the stream.
562+
*
563+
* @param audioTrackId the {@code audioTrackId} of the stream
564+
*/
565+
public void setAudioTrackId(@Nullable final String audioTrackId) {
566+
this.audioTrackId = audioTrackId;
567+
}
568+
569+
/**
570+
* Get the {@code audioTrackName} of the stream, if present.
571+
*
572+
* @return the {@code audioTrackName} of the stream or null
573+
*/
574+
@Nullable
575+
public String getAudioTrackName() {
576+
return audioTrackName;
577+
}
578+
579+
/**
580+
* Set the {@code audioTrackName} of the stream.
581+
*
582+
* @param audioTrackName the {@code audioTrackName} of the stream
583+
*/
584+
public void setAudioTrackName(@Nullable final String audioTrackName) {
585+
this.audioTrackName = audioTrackName;
586+
}
542587
}

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeStreamExtractor.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,6 +1333,8 @@ private java.util.function.Function<ItagInfo, AudioStream> getAudioStreamBuilder
13331333
.setContent(itagInfo.getContent(), itagInfo.getIsUrl())
13341334
.setMediaFormat(itagItem.getMediaFormat())
13351335
.setAverageBitrate(itagItem.getAverageBitrate())
1336+
.setAudioTrackId(itagItem.getAudioTrackId())
1337+
.setAudioTrackName(itagItem.getAudioTrackName())
13361338
.setItagItem(itagItem);
13371339

13381340
if (streamType == StreamType.LIVE_STREAM
@@ -1478,6 +1480,9 @@ private ItagInfo buildAndAddItagInfoToList(
14781480
itagItem.setQuality(formatData.getString("quality"));
14791481
itagItem.setCodec(codec);
14801482

1483+
itagItem.setAudioTrackId(formatData.getObject("audioTrack").getString("id"));
1484+
itagItem.setAudioTrackName(formatData.getObject("audioTrack").getString("displayName"));
1485+
14811486
if (streamType == StreamType.LIVE_STREAM || streamType == StreamType.POST_LIVE_STREAM) {
14821487
itagItem.setTargetDurationSec(formatData.getInt("targetDurationSec"));
14831488
}

extractor/src/main/java/org/schabi/newpipe/extractor/stream/AudioStream.java

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ public final class AudioStream extends Stream {
4040
private int indexEnd;
4141
private String quality;
4242
private String codec;
43+
44+
// Fields about the audio track id/name
45+
private String audioTrackId;
46+
private String audioTrackName;
4347
@Nullable
4448
private ItagItem itagItem;
4549

@@ -58,6 +62,10 @@ public static final class Builder {
5862
private String manifestUrl;
5963
private int averageBitrate = UNKNOWN_BITRATE;
6064
@Nullable
65+
private String audioTrackId;
66+
@Nullable
67+
private String audioTrackName;
68+
@Nullable
6169
private ItagItem itagItem;
6270

6371
/**
@@ -173,6 +181,28 @@ public Builder setAverageBitrate(final int averageBitrate) {
173181
return this;
174182
}
175183

184+
/**
185+
* Set the audio track id of the {@link AudioStream}.
186+
*
187+
* @param audioTrackId the audio track id of the {@link AudioStream}
188+
* @return this {@link Builder} instance
189+
*/
190+
public Builder setAudioTrackId(@Nullable final String audioTrackId) {
191+
this.audioTrackId = audioTrackId;
192+
return this;
193+
}
194+
195+
/**
196+
* Set the audio track name of the {@link AudioStream}.
197+
*
198+
* @param audioTrackName the audio track name of the {@link AudioStream}
199+
* @return this {@link Builder} instance
200+
*/
201+
public Builder setAudioTrackName(@Nullable final String audioTrackName) {
202+
this.audioTrackName = audioTrackName;
203+
return this;
204+
}
205+
176206
/**
177207
* Set the {@link ItagItem} corresponding to the {@link AudioStream}.
178208
*
@@ -226,7 +256,7 @@ public AudioStream build() {
226256
}
227257

228258
return new AudioStream(id, content, isUrl, mediaFormat, deliveryMethod, averageBitrate,
229-
manifestUrl, itagItem);
259+
manifestUrl, audioTrackId, audioTrackName, itagItem);
230260
}
231261
}
232262

@@ -244,6 +274,8 @@ public AudioStream build() {
244274
* @param deliveryMethod the {@link DeliveryMethod} of the stream
245275
* @param averageBitrate the average bitrate of the stream (which can be unknown, see
246276
* {@link #UNKNOWN_BITRATE})
277+
* @param audioTrackId the id of the audio track
278+
* @param audioTrackName the name of the audio track
247279
* @param itagItem the {@link ItagItem} corresponding to the stream, which cannot be null
248280
* @param manifestUrl the URL of the manifest this stream comes from (if applicable,
249281
* otherwise null)
@@ -256,6 +288,8 @@ private AudioStream(@Nonnull final String id,
256288
@Nonnull final DeliveryMethod deliveryMethod,
257289
final int averageBitrate,
258290
@Nullable final String manifestUrl,
291+
@Nullable final String audioTrackId,
292+
@Nullable final String audioTrackName,
259293
@Nullable final ItagItem itagItem) {
260294
super(id, content, isUrl, format, deliveryMethod, manifestUrl);
261295
if (itagItem != null) {
@@ -270,6 +304,8 @@ private AudioStream(@Nonnull final String id,
270304
this.codec = itagItem.getCodec();
271305
}
272306
this.averageBitrate = averageBitrate;
307+
this.audioTrackId = audioTrackId;
308+
this.audioTrackName = audioTrackName;
273309
}
274310

275311
/**
@@ -372,6 +408,24 @@ public String getCodec() {
372408
return codec;
373409
}
374410

411+
/**
412+
* Get the id of the audio track.
413+
*
414+
* @return the id of the audio track
415+
*/
416+
public String getAudioTrackId() {
417+
return audioTrackId;
418+
}
419+
420+
/**
421+
* Get the name of the audio track.
422+
*
423+
* @return the name of the audio track
424+
*/
425+
public String getAudioTrackName() {
426+
return audioTrackName;
427+
}
428+
375429
/**
376430
* {@inheritDoc}
377431
*/

0 commit comments

Comments
 (0)