Komaki's blog <(・`彡 )з

Youtubeの動画をダウンロードしてmp3に変換するシェルスクリプトを作った

今まで、mpsyt(https://github.com/mps-youtube/mps-youtube)で、Youtubeのプレイリストを再生してました。

しかし、Debianではうまく動かないため、mpsytをやめて別のCLIクライアントを探すことにしました。

更新

2019-01-02

cmus

色々と探しましたが、良さそうなCLIクライントがなかったので、mp3をローカルで管理することにしました。

ということで、cmus(https://cmus.github.io/)を使うことにしました。

評判通り、快適ですね。

Youtubeの動画をダウンロード

YoutubeからダウンロードするCLIツールをググると、youtube-dl(https://rg3.github.io/youtube-dl/)が出てきますが、Go製のCLIツールを探すことにしました。

ytdl(https://github.com/rylio/ytdl)というCLIツールを見つけました。

パッケージとしも使えるし、使う前から、良いはずと。

rylio/ytdl

こんな感じでダウンロードできる。

(cmd) ~ $ ytdl https://www.youtube.com/watch?v=3OnnDqH6Wj8
INFO[0000] Fetching video info...
INFO[0000] Downloading to Flo Rida - Good Feeling [Official Video].mp4
 22.38 MiB / 22.38 MiB [======================================================================================================================================================================] 100.00% 9.98 MiB/s 2s

-oオプションで指定できる。

(cmd) ~ $ ytdl -o /var/tmp/{{.Title}}.{{.Ext}} https://www.youtube.com/watch?v=3OnnDqH6Wj8
INFO[0000] Fetching video info...
INFO[0000] Downloading to /var/tmp/Flo Rida - Good Feeling [Official Video].mp4
 22.38 MiB / 22.38 MiB [======================================================================================================================================================================] 100.00% 9.08 MiB/s 2s

-jオプションでjsonを取得できる。jq(jqはインストールしてね)でtitleを取得できる。

(cmd) ~ $ ytdl -j https://www.youtube.com/watch?v=3OnnDqH6Wj8 | jq ".title"
"Flo Rida - Good Feeling [Official Video]"

シェルスクリプト作った

必須コマンド

各自インストールしてください。

コード

ファイル名は何でもよいです。(私はytdl2mp3.shというファイルにしました。)

パスの通っているフォルダに保存してchmodしてください。

#!/usr/bin/env bash
set -Ceuo pipefail

# export
export LC_ALL=C
export LANG=C

# set youtube id from param
YOUTUBEID=$(echo $1 | awk -F "v=" '{print $2}' | awk -F "&" '{print $1}')

if [ x$YOUTUBEID = "x" ]; then
  echo "youtube id(v=ABCD) not found."
  exit 1
fi

# set url
URL="https://www.youtube.com/watch?v=$YOUTUBEID"

# set music dir
MUSICDIR="$HOME/Music/cmus"

# mkdir music dir
if [ ! -d $MUSICDIR ]; then
  mkdir -p $MUSICDIR
fi

# get ytdl json
YTDLJSON=$(ytdl -j $URL)

# get author from json
YTDLAUTHOR=$(echo $YTDLJSON | jq ".author" | sed "s/\"//g")

# get title from json
YTDLTITLE=$(echo $YTDLJSON | jq ".title" | sed "s/\"//g")

# set file path
FILEMP4="$MUSICDIR/$YTDLAUTHOR/$YTDLTITLE.mp4"
FILEMP3="$MUSICDIR/$YTDLAUTHOR/$YTDLTITLE.mp3"

# mkdir author dir
if [ ! -d "$MUSICDIR/$YTDLAUTHOR" ]; then
  mkdir -p "$MUSICDIR/$YTDLAUTHOR"
fi

# check file exist
if [ -f "$FILEMP3" ]; then
  echo "File '$FILEMP3' already exists."
  exit 1
fi

# download mp4 file
ytdl --silent -o "$FILEMP4" $URL

# convert mp4 2 mp3
ffmpeg -loglevel panic -i "$FILEMP4" -vn -acodec libmp3lame -ac 2 -ab 160k -ar 48000 "$FILEMP3"

#rm $FILEMP4
if [ -f "$FILEMP4" ]; then
  rm -f "$FILEMP4"
fi

# done
echo "Downloaded to '$FILEMP3'"

使ってみた

こんな感じになります。

URLをコピって、以下のように実行すればうまいこといくはずです。

(ins) ~ $ ytdl2mp3.sh "https://www.youtube.com/watch?v=2vjPBrBU-TM&list=ALL0WmM0pDa_8oUWlErcd1KHhp1UoWe0q2&index=115"
Downloaded to '/home/teruhiro/Music/cmus/SiaVEVO/Sia - Chandelier (Official Music Video).mp3'
(ins) ~/Music/cmus $ tree
.
├── AdeleVEVO
│   ├── Adele - Chasing Pavements.mp3
│   ├── Adele - Hello.mp3
│   ├── Adele - Rolling in the Deep.mp3
│   ├── Adele - Send My Love (To Your New Lover).mp3
│   ├── Adele - Someone Like You.mp3
│   └── Adele - When We Were Young (Live at The Church Studios).mp3
├── BeyoncéVEVO
│   └── Beyoncé - Halo.mp3
├── Ed Sheeran
│   └── Ed Sheeran - Perfect (Official Music Video).mp3
├── Galantis
│   ├── Galantis - Don't Care (Official Audio).mp3
│   ├── Galantis & Hook N Sling - Love On Me (Official Video).mp3
│   ├── Galantis - Spaceship feat. Uffie (Official Music Video).mp3
│   └── Galantis & Throttle - Tell Me You Love Me (Official Music Video).mp3
├── KygoOfficialVEVO
│   ├── Kygo - Firestone ft. Conrad Sewell (Official Video).mp3
│   └── Kygo & Justin Jesso - Stargazing (Official Video).mp3
└── LadyGagaVEVO
    └── Lady Gaga, Bradley Cooper - Shallow (A Star Is Born).mp3

6 directories, 15 files

あとがき

これでcmusで再生できるようになりました。

#Youtube #Shell script #mp3 #ytdl #Golang