Skip to content

Commit

Permalink
Add --exec-before and --exec-after options
Browse files Browse the repository at this point in the history
* Adds a pair of additional, optional, parameters --exec-before and
  --exec-after which both take a path to a local executable that gets
  run before the download starts, or after it completes respectively.
  The executable will be passed the completed filename as the first argument
  with the URL as the second.
  • Loading branch information
splaspood committed Jul 29, 2024
1 parent 670f25d commit 8dc9ac5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ Options:
--error
Print only errors and general information.
--exec-path COMMAND_PATH
Execute the given COMMAND_PATH after a completed download.
The first argument to COMMAND_PATH will be the completed filename.
--ffmpeg-path FFMPEG_PATH
Set a specific ffmpeg location, including program name.
e.g. "C:\ffmpeg\ffmpeg.exe" or "/opt/ffmpeg/ffmpeg"
Expand Down
26 changes: 26 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,18 @@ Options:
--add-metadata
Write some basic metadata information to the final file.
--after-exec-path COMMAND_PATH
Execute the given COMMAND_PATH after a completed download.
The first argument to COMMAND_PATH will be the completed filename.
--audio-url GOOGLEVIDEO_URL
Pass in the given url as the audio fragment url. Must be a
Google Video url with an itag parameter of 140.
--before-exec-path COMMAND_PATH
Execute the given COMMAND_PATH before a download is started.
The first argument to COMMAND_PATH will be the target filename.
-c
--cookies COOKIES_FILE
Give a cookies.txt file that has your youtube cookies. Allows
Expand Down Expand Up @@ -394,6 +402,8 @@ var (
gvVideoUrl string
tempDir string
ffmpegPath string
execBefore string
execAfter string
proxyUrl *url.URL
threadCount uint
fragMaxTries uint
Expand Down Expand Up @@ -499,6 +509,8 @@ func init() {
cliFlags.StringVar(&tempDir, "td", "", "Temporary directory for downloading files.")
cliFlags.StringVar(&tempDir, "temporary-dir", "", "Temporary directory for downloading files.")
cliFlags.StringVar(&ffmpegPath, "ffmpeg-path", "ffmpeg", "Specify a custom ffmpeg program location, including program name.")
cliFlags.StringVar(&execBefore, "exec-before", "", "Execute this command before a download starts.")
cliFlags.StringVar(&execAfter, "exec-after", "", "Execute this command after a download completes.")
cliFlags.IntVar(&retrySecs, "r", 0, "Seconds to wait between checking stream status.")
cliFlags.IntVar(&retrySecs, "retry-stream", 0, "Seconds to wait between checking stream status.")
cliFlags.UintVar(&threadCount, "threads", 1, "Number of download threads for each stream type.")
Expand Down Expand Up @@ -559,6 +571,16 @@ func init() {
})
}

func ExecCallback(execPath string, execArgs []string) {
if execPath != "" {
LogGeneral("Attempting to call execute exec-before/after COMMAND_PATH")
eRetcode := Execute(execPath, execArgs)
if eRetcode != 0 {
LogError("Execute returned code %d. Something must have gone wrong with %s.", eRetcode, execPath)
}
}
}

// ehh, bad way to do this probably but allows deferred functions to run
// while also allowing early return with a non-0 exit code.
func run() int {
Expand Down Expand Up @@ -865,12 +887,14 @@ func run() int {
if len(info.GetDownloadUrl(DtypeAudio)) > 0 {
LogInfo("Starting download to %s", afile)
go info.DownloadStream(DtypeAudio, afile, progressChan, dlDoneChan)
ExecCallback(execBefore, []string{finalAudioFile, info.URL})
activeDownloads += 1
}

if len(info.GetDownloadUrl(DtypeVideo)) > 0 {
LogInfo("Starting download to %s", vfile)
go info.DownloadStream(DtypeVideo, vfile, progressChan, dlDoneChan)
ExecCallback(execBefore, []string{finalVideoFile})
activeDownloads += 1
}

Expand Down Expand Up @@ -1119,6 +1143,8 @@ func run() int {
LogGeneral("%[1]sFinal audio file: %[2]s%[1]s", "\n", audioFFMpegArgs.FileName)
}

ExecCallback(execAfter, []string{ffmpegArgs.FileName, info.URL})

return 0
}

Expand Down

0 comments on commit 8dc9ac5

Please sign in to comment.