From 7c72c89950fe3d82103e8b26c5082fb5bd83ac09 Mon Sep 17 00:00:00 2001 From: Logan G Date: Fri, 3 Jan 2025 18:29:08 -0700 Subject: [PATCH] Added ExtractAudio option Let's you extract audio from combined video+audio media (most websites supported by yt-dlp) --- download.go | 31 ++++++++++++++++++++++++++++++- ffmpeg.go | 23 +++++++++++++++++++++++ ytva.go | 21 ++++++++++++--------- 3 files changed, 65 insertions(+), 10 deletions(-) diff --git a/download.go b/download.go index 11f0cb1..e828c76 100644 --- a/download.go +++ b/download.go @@ -78,7 +78,28 @@ func downloadVideo(url string, path string, group GroupConfig) (err error) { } /* AUDIO */ - if group.AudioFormat != "" { + if group.ExtractFormat != "" { + log.Debugf("Extracting audio from \"%s\" with format \"%s\"", video.Info.ID, group.ExtractFormat) + + audioDLResult, err := video.Download(context.Background(), group.ExtractFormat) + if err != nil { + return err + } + + defer audioDLResult.Close() + + file, err := os.OpenFile(tempPath+"-extract", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644) + if err != nil { + return err + } + + defer file.Close() + + io.Copy(file, audioDLResult) + + audioDLResult.Close() + file.Close() + } else if group.AudioFormat != "" { log.Debugf("Downloading audio \"%s\" with format \"%s\"", video.Info.ID, group.AudioFormat) audioDLResult, err := video.Download(context.Background(), group.AudioFormat) @@ -112,6 +133,14 @@ func downloadVideo(url string, path string, group GroupConfig) (err error) { if err := os.Remove(tempPath+"-audio"); err != nil { log.Warnf("Could not remove file \"%s\"", tempPath+"-audio") } + } else if group.ExtractFormat != "" { + if err := extractAudio(tempPath+"-extract", group.CFormat, path); err != nil { + log.Errorf("Could not extract audio from \"%s\" to \"%s\"", tempPath+"-extract", path) + } + + if err := os.Remove(tempPath+"-extract"); err != nil { + log.Warnf("Could not remove file \"%s\"", tempPath+"-extract") + } } else if group.VideoFormat != "" { copyFile(tempPath+"-vid", path) diff --git a/ffmpeg.go b/ffmpeg.go index 13ecfbe..233bde1 100644 --- a/ffmpeg.go +++ b/ffmpeg.go @@ -29,3 +29,26 @@ func mergeStreams(path1 string, path2 string, format string, output string) (err return ffmpeg.Output(input, output, kwArgs).OverWriteOutput().ErrorToStdOut().Silent(silent).Run() } + +func extractAudio(path1 string, format string, output string) (err error) { + input := []*ffmpeg.Stream{ffmpeg.Input(path1)} + + defaultArgs := ffmpeg.KwArgs{"map": "0:a", "c:a": "copy", "format": format} + + var ffmpegLogLevel ffmpeg.KwArgs + var silent bool + + if Flags.Verbose { + ffmpegLogLevel = ffmpeg.KwArgs{"v": "info"} + silent = false + } else if Flags.Quiet { + ffmpegLogLevel = ffmpeg.KwArgs{"v": "quiet"} + silent = true + } else { + ffmpegLogLevel = ffmpeg.KwArgs{"v": "error"} + silent = true + } + + kwArgs := ffmpeg.MergeKwArgs([]ffmpeg.KwArgs{ffmpegLogLevel, defaultArgs}) + return ffmpeg.Output(input, output, kwArgs).OverWriteOutput().ErrorToStdOut().Silent(silent).Run() +} diff --git a/ytva.go b/ytva.go index faa7657..c3a231f 100644 --- a/ytva.go +++ b/ytva.go @@ -41,15 +41,18 @@ type _config struct { } type GroupConfig struct { - URL []string - OutputDir string - FileName string - CFormat string - VideoFormat string - AudioFormat string - NumVideos uint - SizeLimit float64 - Filters []struct { + URL []string + OutputDir string + FileName string + CFormat string + ExtractFormat string + VideoFormat string + AudioFormat string + DownloadSubtitles bool // Currently useless + DownloadThumbnails bool // Currently useless + NumVideos uint + SizeLimit float64 + Filters []struct { Name string Input string Pattern string