Skip to content

Commit

Permalink
Merge pull request #618 from john-black-3k/youtube_merge_streams_issues
Browse files Browse the repository at this point in the history
Youtube merge streams issues
  • Loading branch information
iawia002 authored Feb 17, 2020
2 parents 33f16ef + 38f0b06 commit c4b096a
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 11 deletions.
10 changes: 9 additions & 1 deletion downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,15 @@ func Download(v Data, refer string, chunkSizeMB int) error {
}

// Skip the complete file that has been merged
mergedFilePath, err := utils.FilePath(title, "mp4", false)
var (
mergedFilePath string
err error
)
if v.Site == "YouTube youtube.com" {
mergedFilePath, err = utils.FilePath(title, data.URLs[0].Ext, false)
} else {
mergedFilePath, err = utils.FilePath(title, "mp4", false)
}
if err != nil {
return err
}
Expand Down
71 changes: 62 additions & 9 deletions extractors/youtube/youtube.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,25 +219,78 @@ func extractVideoURLS(data playerResponseType, videoInfo *ytdl.VideoInfo) (map[s
// Unlike `url_encoded_fmt_stream_map`, all videos in `adaptive_fmts` have no sound,
// we need download video and audio both and then merge them.

// get audio file for videos in AdaptiveFormats
var audio downloader.URL
for _, f := range data.StreamingData.AdaptiveFormats {
if strings.HasPrefix(f.MimeType, "audio/mp4") {
audioURL, err := getRealURL(f, videoInfo, "m4a")
if err != nil {
return nil, err
// Get separate m4a and webm audio streams for videos in AdaptiveFormats.
// Prefer medium quality audio over low quality (there is no AUDIO_QUALITY_HIGH).
var fM4aMedium, fM4aLow, fWebmMedium, fWebmLow *streamFormat
for i, f := range data.StreamingData.AdaptiveFormats {
switch {
case strings.HasPrefix(f.MimeType, "audio/mp4"):
if f.AudioQuality == "AUDIO_QUALITY_MEDIUM" {
fM4aMedium = &data.StreamingData.AdaptiveFormats[i]
} else {
fM4aLow = &data.StreamingData.AdaptiveFormats[i]
}
case strings.HasPrefix(f.MimeType, "audio/webm"):
if f.AudioQuality == "AUDIO_QUALITY_MEDIUM" {
fWebmMedium = &data.StreamingData.AdaptiveFormats[i]
} else {
fWebmLow = &data.StreamingData.AdaptiveFormats[i]
}
audio = *audioURL
}

if fM4aMedium != nil && fWebmMedium != nil {
break
}
}

var audioM4a downloader.URL
if fM4aMedium != nil {
audioURL, err := getRealURL(*fM4aMedium, videoInfo, "m4a")
if err != nil {
return nil, err
}
audioM4a = *audioURL
} else if fM4aLow != nil {
audioURL, err := getRealURL(*fM4aLow, videoInfo, "m4a")
if err != nil {
return nil, err
}
audioM4a = *audioURL
}

var audioWebm downloader.URL
if fWebmMedium != nil {
audioURL, err := getRealURL(*fWebmMedium, videoInfo, "webm")
if err != nil {
return nil, err
}
audioWebm = *audioURL
} else if fM4aLow != nil {
audioURL, err := getRealURL(*fWebmLow, videoInfo, "webm")
if err != nil {
return nil, err
}
audioWebm = *audioURL
}

var emptyURL downloader.URL
for _, f := range data.StreamingData.AdaptiveFormats {
stream, err := genStream(f, videoInfo)
if err != nil {
return nil, err
}
stream.URLs = append(stream.URLs, audio)

// append audio stream only for adaptive video streams (not audio)
switch {
case strings.HasPrefix(f.MimeType, "video/mp4"):
if audioM4a != emptyURL {
stream.URLs = append(stream.URLs, audioM4a)
}
case strings.HasPrefix(f.MimeType, "video/webm"):
if audioWebm != emptyURL {
stream.URLs = append(stream.URLs, audioWebm)
}
}

streams[strconv.Itoa(f.Itag)] = *stream
}
Expand Down
2 changes: 1 addition & 1 deletion utils/ffmpeg.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func MergeAudioAndVideo(paths []string, mergedFilePath string) error {
cmds = append(cmds, "-i", path)
}
cmds = append(
cmds, "-c:v", "copy", "-c:a", "aac", "-strict", "experimental",
cmds, "-c:v", "copy", "-c:a", "copy",
mergedFilePath,
)
return runMergeCmd(exec.Command("ffmpeg", cmds...), paths, "")
Expand Down

0 comments on commit c4b096a

Please sign in to comment.