Add support for extracting audio tracks.

This commit is contained in:
Kavin 2022-11-13 21:35:26 +00:00
parent 43d1c1f8b1
commit 6d59cdbe3a
No known key found for this signature in database
GPG Key ID: 49451E4482CC5BCD
3 changed files with 116 additions and 12 deletions

View File

@ -1,5 +1,12 @@
package org.schabi.newpipe.extractor.services.youtube;
import org.schabi.newpipe.extractor.MediaFormat;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.Serializable;
import static org.schabi.newpipe.extractor.MediaFormat.M4A;
import static org.schabi.newpipe.extractor.MediaFormat.MPEG_4;
import static org.schabi.newpipe.extractor.MediaFormat.WEBM;
@ -10,14 +17,6 @@ import static org.schabi.newpipe.extractor.services.youtube.ItagItem.ItagType.AU
import static org.schabi.newpipe.extractor.services.youtube.ItagItem.ItagType.VIDEO;
import static org.schabi.newpipe.extractor.services.youtube.ItagItem.ItagType.VIDEO_ONLY;
import org.schabi.newpipe.extractor.MediaFormat;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.Serializable;
public class ItagItem implements Serializable {
/**
@ -211,18 +210,24 @@ public class ItagItem implements Serializable {
public final ItagType itagType;
// Audio fields
/** @deprecated Use {@link #getAverageBitrate()} instead. */
/**
* @deprecated Use {@link #getAverageBitrate()} instead.
*/
@Deprecated
public int avgBitrate = AVERAGE_BITRATE_UNKNOWN;
private int sampleRate = SAMPLE_RATE_UNKNOWN;
private int audioChannels = AUDIO_CHANNELS_NOT_APPLICABLE_OR_UNKNOWN;
// Video fields
/** @deprecated Use {@link #getResolutionString()} instead. */
/**
* @deprecated Use {@link #getResolutionString()} instead.
*/
@Deprecated
public String resolutionString;
/** @deprecated Use {@link #getFps()} and {@link #setFps(int)} instead. */
/**
* @deprecated Use {@link #getFps()} and {@link #setFps(int)} instead.
*/
@Deprecated
public int fps = FPS_NOT_APPLICABLE_OR_UNKNOWN;
@ -239,6 +244,8 @@ public class ItagItem implements Serializable {
private int targetDurationSec = TARGET_DURATION_SEC_UNKNOWN;
private long approxDurationMs = APPROX_DURATION_MS_UNKNOWN;
private long contentLength = CONTENT_LENGTH_UNKNOWN;
private String audioTrackId;
private String audioTrackName;
public int getBitrate() {
return bitrate;
@ -539,4 +546,42 @@ public class ItagItem implements Serializable {
public void setContentLength(final long contentLength) {
this.contentLength = contentLength > 0 ? contentLength : CONTENT_LENGTH_UNKNOWN;
}
/**
* Get the {@code audioTrackId} of the stream, if present.
*
* @return the {@code audioTrackId} of the stream or null
*/
@Nullable
public String getAudioTrackId() {
return audioTrackId;
}
/**
* Set the {@code audioTrackId} of the stream.
*
* @param audioTrackId the {@code audioTrackId} of the stream
*/
public void setAudioTrackId(@Nullable final String audioTrackId) {
this.audioTrackId = audioTrackId;
}
/**
* Get the {@code audioTrackName} of the stream, if present.
*
* @return the {@code audioTrackName} of the stream or null
*/
@Nullable
public String getAudioTrackName() {
return audioTrackName;
}
/**
* Set the {@code audioTrackName} of the stream.
*
* @param audioTrackName the {@code audioTrackName} of the stream
*/
public void setAudioTrackName(@Nullable final String audioTrackName) {
this.audioTrackName = audioTrackName;
}
}

View File

@ -1333,6 +1333,8 @@ public class YoutubeStreamExtractor extends StreamExtractor {
.setContent(itagInfo.getContent(), itagInfo.getIsUrl())
.setMediaFormat(itagItem.getMediaFormat())
.setAverageBitrate(itagItem.getAverageBitrate())
.setAudioTrackId(itagItem.getAudioTrackId())
.setAudioTrackName(itagItem.getAudioTrackName())
.setItagItem(itagItem);
if (streamType == StreamType.LIVE_STREAM
@ -1478,6 +1480,9 @@ public class YoutubeStreamExtractor extends StreamExtractor {
itagItem.setQuality(formatData.getString("quality"));
itagItem.setCodec(codec);
itagItem.setAudioTrackId(formatData.getObject("audioTrack").getString("id"));
itagItem.setAudioTrackName(formatData.getObject("audioTrack").getString("displayName"));
if (streamType == StreamType.LIVE_STREAM || streamType == StreamType.POST_LIVE_STREAM) {
itagItem.setTargetDurationSec(formatData.getInt("targetDurationSec"));
}

View File

@ -40,6 +40,10 @@ public final class AudioStream extends Stream {
private int indexEnd;
private String quality;
private String codec;
// Fields about the audio track id/name
private String audioTrackId;
private String audioTrackName;
@Nullable
private ItagItem itagItem;
@ -58,6 +62,10 @@ public final class AudioStream extends Stream {
private String manifestUrl;
private int averageBitrate = UNKNOWN_BITRATE;
@Nullable
private String audioTrackId;
@Nullable
private String audioTrackName;
@Nullable
private ItagItem itagItem;
/**
@ -173,6 +181,28 @@ public final class AudioStream extends Stream {
return this;
}
/**
* Set the audio track id of the {@link AudioStream}.
*
* @param audioTrackId the audio track id of the {@link AudioStream}
* @return this {@link Builder} instance
*/
public Builder setAudioTrackId(@Nullable final String audioTrackId) {
this.audioTrackId = audioTrackId;
return this;
}
/**
* Set the audio track name of the {@link AudioStream}.
*
* @param audioTrackName the audio track name of the {@link AudioStream}
* @return this {@link Builder} instance
*/
public Builder setAudioTrackName(@Nullable final String audioTrackName) {
this.audioTrackName = audioTrackName;
return this;
}
/**
* Set the {@link ItagItem} corresponding to the {@link AudioStream}.
*
@ -226,7 +256,7 @@ public final class AudioStream extends Stream {
}
return new AudioStream(id, content, isUrl, mediaFormat, deliveryMethod, averageBitrate,
manifestUrl, itagItem);
manifestUrl, audioTrackId, audioTrackName, itagItem);
}
}
@ -244,6 +274,8 @@ public final class AudioStream extends Stream {
* @param deliveryMethod the {@link DeliveryMethod} of the stream
* @param averageBitrate the average bitrate of the stream (which can be unknown, see
* {@link #UNKNOWN_BITRATE})
* @param audioTrackId the id of the audio track
* @param audioTrackName the name of the audio track
* @param itagItem the {@link ItagItem} corresponding to the stream, which cannot be null
* @param manifestUrl the URL of the manifest this stream comes from (if applicable,
* otherwise null)
@ -256,6 +288,8 @@ public final class AudioStream extends Stream {
@Nonnull final DeliveryMethod deliveryMethod,
final int averageBitrate,
@Nullable final String manifestUrl,
@Nullable final String audioTrackId,
@Nullable final String audioTrackName,
@Nullable final ItagItem itagItem) {
super(id, content, isUrl, format, deliveryMethod, manifestUrl);
if (itagItem != null) {
@ -270,6 +304,8 @@ public final class AudioStream extends Stream {
this.codec = itagItem.getCodec();
}
this.averageBitrate = averageBitrate;
this.audioTrackId = audioTrackId;
this.audioTrackName = audioTrackName;
}
/**
@ -372,6 +408,24 @@ public final class AudioStream extends Stream {
return codec;
}
/**
* Get the id of the audio track.
*
* @return the id of the audio track
*/
public String getAudioTrackId() {
return audioTrackId;
}
/**
* Get the name of the audio track.
*
* @return the name of the audio track
*/
public String getAudioTrackName() {
return audioTrackName;
}
/**
* {@inheritDoc}
*/