2024-10-13 17:17:40 +02:00
|
|
|
defmodule HahaYes.Utility do
|
2024-10-13 17:41:55 +02:00
|
|
|
@moduledoc """
|
|
|
|
Various utilities to be reused in commands
|
|
|
|
"""
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Download utility with the format at 480p by default.
|
|
|
|
|
|
|
|
## Example
|
|
|
|
|
|
|
|
```
|
|
|
|
iex> HahaYes.Utility.download("https://x.com/i/status/1844841048603783249", "#{System.tmp_dir}/test")
|
2024-10-13 18:02:16 +02:00
|
|
|
{:ok, "#{System.tmp_dir}/test.mp4"}
|
2024-10-13 17:41:55 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
```
|
2024-10-13 18:02:16 +02:00
|
|
|
iex> HahaYes.Utility.download("http://example.com", "#{System.tmp_dir}/test")
|
|
|
|
{:error,"[generic] Extracting URL: http://example.com\\n[generic] example: Downloading webpage\\n[generic] example: Extracting information\\nERROR: Unsupported URL: http://example.com\\n"}
|
2024-10-13 17:41:55 +02:00
|
|
|
```
|
|
|
|
"""
|
|
|
|
|
2024-10-13 17:17:40 +02:00
|
|
|
def download(url, output, format \\ "bestvideo[height<=?480]+bestaudio/best") do
|
|
|
|
opt = ["-f", format, url, "-o", "#{output}.%(ext)s", "--force-overwrites", "--playlist-reverse", "--no-playlist", "--remux-video=mp4/webm/mov", "--no-warnings"];
|
|
|
|
|
|
|
|
{error_output, status} = System.cmd("yt-dlp", opt, [stderr_to_stdout: true])
|
|
|
|
if status !== 0 do
|
|
|
|
{:error, error_output}
|
|
|
|
else
|
|
|
|
{:ok, Enum.at(Path.wildcard("#{output}.*"), 0)}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|