LoginSignup
2
0

YouTubeからopusで正方形のサムネ付きでダウンロード(Download from YouTube with square thumbnails in opus)

Last updated at Posted at 2023-10-03

題名の通りです。
今まで、opusではサムネをつけられないと思っていて、mp3にわざわざ変えていたのですが、やっとどうにかする方法を見つけました。

yt-dlp -x -f "bestaudio[acodec=opus]" "url" --embed-thumbnail --convert-thumbnails jpg --exec-before-download "ffmpeg -i %(thumbnails.-1.filepath)q -vf crop=\"'if(gt(ih,iw),iw,ih)':'if(gt(iw,ih),ih,iw)'\" _%(thumbnails.-1.filepath)q" --exec-before-download "rm %(thumbnails.-1.filepath)q" --exec-before-download "mv _%(thumbnails.-1.filepath)q %(thumbnails.-1.filepath)q"

長いので、改行すると

yt-dlp -x -f "bestaudio[acodec=opus]" "url" --embed-thumbnail --convert-thumbnails jpg 
--exec-before-download "ffmpeg -i %(thumbnails.-1.filepath)q -vf crop=\"'if(gt(ih,iw),iw,ih)':'if(gt(iw,ih),ih,iw)'\" _%(thumbnails.-1.filepath)q" 
--exec-before-download "rm %(thumbnails.-1.filepath)q" 
--exec-before-download "mv _%(thumbnails.-1.filepath)q %(thumbnails.-1.filepath)q"

結局ffmpegで埋め込む方法も、opusencで埋め込む方法もわからなかったです。
ので、後からくっつける方法が分からずじまい、、、
おおよそここにお世話になりました。
mp3の時とは違うらしくmp3ではここにやり方があります。


Pythonで使う。

自分はpythonから使うので、そのために色々いじりました。結果以下のようになりました。

def getTitle(url):
    from youtubesearchpython import Video
    ydl_audio_opts = {
        'noplaylist': 'True',
        'nocheckcertificate': 'True'}
    replaceList={' ' : '_',
                 ' ' : '_',
                 ':' : '-',
                 '[' : '「',
                 ']' : '」',
                 '/' : '/',
                 '\n': ''}
    title=Video.getInfo(url)["title"]
    for key, value in replaceList.items():
        title=title.replace(key, value)
    print("getTitle Done")
    return title

def download_opus(url,path,title):
    import subprocess
    script=f"yt-dlp -x -f 'bestaudio[acodec=opus]' --no-check-certificate --no-playlist -o '{path}/{title}.%(ext)s' '{url}' "\
    "--embed-thumbnail --convert-thumbnails jpg --exec-before-download "\
    "\"ffmpeg -y -i %(thumbnails.-1.filepath)q -vf crop=\\\"'if(gt(ih,iw),iw,ih)' : 'if(gt(iw,ih),ih,iw)'\\\" %(thumbnails.-1.filepath)q\""
    devnull = open('/dev/null', 'w')
    subprocess.run(script, stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True, executable="/bin/zsh")

前半のgetTitleは、youtubesearchpythonというやつを使っていて、入れたくない文字を辞書型にして消しています。
特に困ったことなく適当に作れました。一応ここを参考にしました。

後半のdownload_opusは大変でした。
まず、ffmpeg~の部分で、このページの一番上に書かれている、他サイトからのコピペは、

  1. 変換後ファイルを名前を変えて作成
  2. 変換前ファイルを削除
  3. 変換後ファイルの名前を変換前ファイルと同じに

とやっていて、これが無駄に感じたので、-yのオプションで上書き保存するようにして、解決

次に、subprocess.runexecutable="/bin/zsh"ですが、macではshellにzshを使用するので、これに設定
見た感じ、winのshと文法(特に"'らへん)が違うようなので、winでは動かないかもですが、自分はwinを持っていなので、、、
参考にしたサイト

また、上から変えたところとして、一番自分でもよくわかっていないのが、\によるエスケープシーケンスです
2重にエスケープシーケンスしなければならないことに気づくまで結構かかりました。
割と色々試してやっと上手くいった感じですので、説明はできかねます。まあ、ただの大学生なので、多めに見てください

サムネは自分のmac環境だとfoobar2000モバイルで表示されました。普通のは試してません。
他に表示できるアプリがありましたら教えてほしいです。
ちなみにwebmはサムネイル非対応とどこかに書いてありました。
手元にあるopusに手元にあるjpgをくっつける方法がわかる人は教えていただければと思います,,,

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0