Add direct download for video

Signed-off-by: Joseph Nuthalapati's avatarJoseph Nuthalapati <[email protected]>
parent d2ebe4d2
......@@ -56,6 +56,22 @@ proc play*(player: string, args: openArray[string], title: string) =
else:
discard execProcess(player, args=args, options=processOptions)
func buildMusicDownloadArgs*(url: string): seq[string] =
{.noSideEffect.}:
var args = @["--ignore-errors", "-f", "bestaudio", "--extract-audio", "--audio-format", "mp3", "--audio-quality", "0", "-o"]
let downloadLocation = &"'{expandTilde(musicDownloadDirectory)}/%(title)s.%(ext)s'"
args.add(downloadLocation)
args.add(url)
return args
func buildVideoDownloadArgs*(url: string): seq[string] =
{.noSideEffect.}:
var args = @["-f", "best", "-o"]
let downloadLocation = &"'{expandTilde(videoDownloadDirectory)}/%(title)s.%(ext)s'"
args.add(downloadLocation)
args.add(url)
return args
proc download*(args: openArray[string], title: string) =
styledEcho "\n", fgGreen, "Downloading ", styleBright, fgMagenta, title
discard execShellCmd(&"youtube-dl {args.join(\" \")}")
......@@ -69,9 +85,12 @@ func stripZshEscaping(url: string): string =
func sanitizeURL*(url: string): string =
urlLongen(stripZshEscaping(url))
proc directPlay*(searchQuery: string, player: string) =
let url = sanitizeURL(searchQuery)
if searchQuery.startswith("magnet:"):
proc directPlay*(url: string, player: string) =
if url.startswith("magnet:"):
discard execProcess("peerflix", args=[url, &"--{player}"], options=processOptions)
else:
discard execProcess(player, args=[url], options=processOptions)
proc directDownload*(url: string) =
let args = buildVideoDownloadArgs(url)
discard execShellCmd(&"youtube-dl {args.join(\" \")}")
......@@ -49,7 +49,10 @@ proc main() =
quit(1)
if searchQuery.startswith("https:") or searchQuery.startswith("magnet:"):
directPlay(searchQuery, player)
if options["download"]:
directDownload(sanitizeURL(searchQuery))
else:
directPlay(sanitizeURL(searchQuery), player)
quit(0)
let searchResults = extractTitlesAndUrls(getYoutubePage(searchQuery))
......@@ -68,28 +71,12 @@ proc main() =
if options["fullScreen"]: args.add("--fullscreen")
return args
func buildMusicDownloadArgs(number: int): seq[string] =
{.noSideEffect.}:
var args = @["--ignore-errors", "-f", "bestaudio", "--extract-audio", "--audio-format", "mp3", "--audio-quality", "0", "-o"]
let downloadLocation = &"'{expandTilde(musicDownloadDirectory)}/{searchResults[number].title}.mp3'"
args.add(downloadLocation)
args.add(searchResults[number].url)
return args
func buildVideoDownloadArgs(number: int): seq[string] =
{.noSideEffect.}:
var args = @["-f", "best", "-o"]
let downloadLocation = &"'{expandTilde(videoDownloadDirectory)}/%(title)s.%(ext)s'"
args.add(downloadLocation)
args.add(searchResults[number].url)
return args
proc handleUserInput(number: int) =
if options["download"]:
if options["musicOnly"]:
download(buildMusicDownloadArgs(number), searchResults[number].title)
download(buildMusicDownloadArgs(searchResults[number].url), searchResults[number].title)
else:
download(buildVideoDownloadArgs(number), searchResults[number].title)
download(buildVideoDownloadArgs(searchResults[number].url), searchResults[number].title)
else:
play(player, buildPlayerArgs(number), searchResults[number].title)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment