Bash:bash 从 youtube 下载修剪过的 mp3 的脚本 url
Bash: bash script to download trimmed mp3 from youtube url
我想从 youtube 的视频 url 下载最初 x 秒 trimmed mp3。
我发现 youtube-dl 可以将视频从 youtube 下载到本地机器。但是,当我查看 youtube-dl 的手册页时,我找不到任何 trim 选项。
所以我尝试使用ffmpeg来trim下载mp3文件。
我喜欢写一个 bash 脚本来做同样的事情,而不是做这两个步骤。
我的尝试如下。
然而,我被困在一个地方:
"HOW TO GET THE VARIABLE NAME OF OUTPUT MP3 FILE FROM YOUTUBE-DL?"
脚本如下:
# trim initial x seconds of mp3 file
# e.g. mytrim https://www.youtube.com/watch?v=dD5RgCf1hrI 30
function mytrim() {
youtube-dl --extract-audio --embed-thumbnail --audio-format mp3 -o "%(title)s.%(ext)s"
ffmpeg -ss -i $OUTPUT_MP3 -acodec copy -y temp.mp3
mv temp.mp3 $OUTPUT_MP3
}
如何获取变量值$OUTPUT_MP3?
echo "%(title)s.%(ext)s" 给出逐字输出,不给出输出文件名。
我们怎样才能使脚本工作?
我们将不胜感激。
youtube-dl
支持 --get-filename
选项,该选项实际上并不下载任何内容,但会在标准输出上给出计算出的文件名。
mytrim() {
local downloaded_file
youtube-dl --extract-audio --embed-thumbnail --audio-format mp3 -o "%(title)s.%(ext)s"
downloaded_file=$(youtube-dl --get-filename --extract-audio --embed-thumbnail --audio-format mp3 -o "%(title)s.%(ext)s" )
ffmpeg -ss -i "${downloaded_file}" -acodec copy -y temp.mp3
mv temp.mp3 "${downloaded_file}"
}
非常感谢@umlaute,
我在 bash_profile 中添加了以下函数:
# download best video quality using youtube-dl
# usage: myvid https://youtu.be/450p7goxZqg?t=4
function myvid() {
youtube-dl -f bestvideo+bestaudio ""
rm -r youtube_video_time.txt
}
# usage: mymp3 youtube_video_url
mymp3() {
local downloaded_file
youtube-dl --extract-audio --embed-thumbnail --audio-format mp3 -o "%(title)s.%(ext)s"
downloaded_file=$(youtube-dl --get-filename --extract-audio --embed-thumbnail --audio-format mp3 -o "%(title)s.%(ext)s" )
}
# initial x seconds trimmed mp3 song
# mytrim 5 https://www.youtube.com/watch?v=iLQxbEkN85o
mytrim() {
local downloaded_file
youtube-dl --extract-audio --embed-thumbnail --audio-format mp3 -o "%(title)s.%(ext)s"
downloaded_file=$(youtube-dl --get-filename --extract-audio --embed-thumbnail --audio-format mp3 -o "%(title)s.%(ext)s" )
ffmpeg -ss -i "${downloaded_file}" -acodec copy -y temp.mp3
mv temp.mp3 "${downloaded_file}"
clear
echo "${downloaded_file}"
# Now replace whitespace by underscore
find . -type f -name "* *.mp3" -exec bash -c 'mv "[=10=]" "${0// /_}"' {} \;
# Lowercase the file name
for i in $(find . -name '*[A-Z]*.mp3' -type f); do mv "$i" "$(echo $i|tr A-Z a-z)"; done
}
从文本文件下载歌曲
IN_URL="/Volumes/Media/Music/download_youtube_mp3/songs.txt"
ODIR="downloaded_songs/%(title)s.%(ext)s"
youtube-dl --extract-audio --embed-thumbnail --audio-format mp3 --audio-quality=320k --output $ODIR --batch-file=$IN_URL
我想从 youtube 的视频 url 下载最初 x 秒 trimmed mp3。
我发现 youtube-dl 可以将视频从 youtube 下载到本地机器。但是,当我查看 youtube-dl 的手册页时,我找不到任何 trim 选项。
所以我尝试使用ffmpeg来trim下载mp3文件。
我喜欢写一个 bash 脚本来做同样的事情,而不是做这两个步骤。
我的尝试如下。
然而,我被困在一个地方:
"HOW TO GET THE VARIABLE NAME OF OUTPUT MP3 FILE FROM YOUTUBE-DL?"
脚本如下:
# trim initial x seconds of mp3 file
# e.g. mytrim https://www.youtube.com/watch?v=dD5RgCf1hrI 30
function mytrim() {
youtube-dl --extract-audio --embed-thumbnail --audio-format mp3 -o "%(title)s.%(ext)s"
ffmpeg -ss -i $OUTPUT_MP3 -acodec copy -y temp.mp3
mv temp.mp3 $OUTPUT_MP3
}
如何获取变量值$OUTPUT_MP3?
echo "%(title)s.%(ext)s" 给出逐字输出,不给出输出文件名。
我们怎样才能使脚本工作?
我们将不胜感激。
youtube-dl
支持 --get-filename
选项,该选项实际上并不下载任何内容,但会在标准输出上给出计算出的文件名。
mytrim() {
local downloaded_file
youtube-dl --extract-audio --embed-thumbnail --audio-format mp3 -o "%(title)s.%(ext)s"
downloaded_file=$(youtube-dl --get-filename --extract-audio --embed-thumbnail --audio-format mp3 -o "%(title)s.%(ext)s" )
ffmpeg -ss -i "${downloaded_file}" -acodec copy -y temp.mp3
mv temp.mp3 "${downloaded_file}"
}
非常感谢@umlaute,
我在 bash_profile 中添加了以下函数:
# download best video quality using youtube-dl
# usage: myvid https://youtu.be/450p7goxZqg?t=4
function myvid() {
youtube-dl -f bestvideo+bestaudio ""
rm -r youtube_video_time.txt
}
# usage: mymp3 youtube_video_url
mymp3() {
local downloaded_file
youtube-dl --extract-audio --embed-thumbnail --audio-format mp3 -o "%(title)s.%(ext)s"
downloaded_file=$(youtube-dl --get-filename --extract-audio --embed-thumbnail --audio-format mp3 -o "%(title)s.%(ext)s" )
}
# initial x seconds trimmed mp3 song
# mytrim 5 https://www.youtube.com/watch?v=iLQxbEkN85o
mytrim() {
local downloaded_file
youtube-dl --extract-audio --embed-thumbnail --audio-format mp3 -o "%(title)s.%(ext)s"
downloaded_file=$(youtube-dl --get-filename --extract-audio --embed-thumbnail --audio-format mp3 -o "%(title)s.%(ext)s" )
ffmpeg -ss -i "${downloaded_file}" -acodec copy -y temp.mp3
mv temp.mp3 "${downloaded_file}"
clear
echo "${downloaded_file}"
# Now replace whitespace by underscore
find . -type f -name "* *.mp3" -exec bash -c 'mv "[=10=]" "${0// /_}"' {} \;
# Lowercase the file name
for i in $(find . -name '*[A-Z]*.mp3' -type f); do mv "$i" "$(echo $i|tr A-Z a-z)"; done
}
从文本文件下载歌曲
IN_URL="/Volumes/Media/Music/download_youtube_mp3/songs.txt"
ODIR="downloaded_songs/%(title)s.%(ext)s"
youtube-dl --extract-audio --embed-thumbnail --audio-format mp3 --audio-quality=320k --output $ODIR --batch-file=$IN_URL