Has*_*ziz 4 video bash ffmpeg bash-scripting youtube-dl
我经常遇到需要裁剪部分内容的 YouTube 视频,但这些视频可能很长,而下载整个视频只是为了处理其中的一小部分会花费太长时间并且非常浪费。
尽管自 2013 年以来就出现了youtube-dl
关于此问题的 Github 问题,并且在这方面取得了一些进展,但尚未实现任何目标,并且截至 2021 年,此功能仍然不可用。
还有哪些其他解决方案可以仅下载我需要的视频部分?
经过几天的搜索,迄今为止我遇到的最佳解决方案结合了youtube-dl
获取 YouTube 视频各个流的内部 URL 的能力和ffmpeg
使用这些流作为输入的能力。
运行以下 Bash magic,分别使用目标链接的视频和音频流的内部 YouTube URL自动填充video_url
和变量:audio_url
# As scary as it looks, perfectly safe to run in a terminal
{
read -r video_url
read -r audio_url
} < <(
youtube-dl --get-url --youtube-skip-dash-manifest https://www.youtube.com/watch?v=MfnzBYV5fxs
)
Run Code Online (Sandbox Code Playgroud)
最后,传递时间戳以下载您想要的视频部分:
# Download 2 minutes of the video between 5 mins in to 7 mins, using timestamps:
ffmpeg -ss 00:05:00.00 -to 00:07:00.00 -i "$video_url" -ss 00:05:00.00 -to 00:07:00.00 -i "$audio_url" output.mkv
Run Code Online (Sandbox Code Playgroud)
或者,如果您更喜欢裁剪持续时间而不是两个时间戳之间的时间:
# Download 2 minutes of the video between 5 mins in to 7 mins, using duration:
ffmpeg -ss 00:05:00.00 -i "$video_url" -ss 00:05:00.00 -i "$audio_url" -t 2:00 output.mkv
Run Code Online (Sandbox Code Playgroud)
如果您使用 Bash 之外的任何其他 shell,则必须以手动方式执行此操作:
运行以下命令将输出目标视频的视频和音频流的 YouTube(非常长)内部 URL:
youtube-dl --get-url --youtube-skip-dash-manifest "https://www.youtube.com/watch?v=MfnzBYV5fxs"
Run Code Online (Sandbox Code Playgroud)
将这些 URL 插入以下命令中,分别粘贴到<video_url>
和 的位置<audio_url>
:
# Download 2 minutes of the video between 5 mins in to 7 mins, using timestamps:
ffmpeg -ss 00:05:00.00 -to 00:07:00.00 -i <video_url> -ss 00:05:00.00 -to 00:07:00.00 -i <audio_url> output.mkv
Run Code Online (Sandbox Code Playgroud)
或者,如果您更喜欢裁剪持续时间而不是两个时间戳之间的时间:
# Download 2 minutes of the video between 5 mins in to 7 mins, using duration:
ffmpeg -ss 00:05:00.00 -i <video_url> -ss 00:05:00.00 -i <audio_url> -t 2:00 output.mkv
Run Code Online (Sandbox Code Playgroud)