初めに
最近YouTubeを見ることが多くなったんですが、消えてなくなっちゃう事もあるので出来たら手元に残しておきたいです。
結構Webでフリー系でダウンロードをやってくれるサーバーもありますが、720pのSDグレードで、1080pのHDグレードで落とせるのは有料になってますよね。
私はマックユーザーなんでマックで簡単に夜間に寝ている間に落としてもらえると、出勤のときにスマホのVLCに入れて、移動中に聞きながら出勤ってのも出来て良いなと思うんですよね。
歴史 & 4KPlayerの終焉
かなり以前はGetter1とか良いフリーウエアーがあったんですが最近見かけませんね。 私も去年まで中華系ソフト4KPlayerってのがあって、それを使っていたんですが。2025年の秋頃からサイトが消えてなくなり、そしてYoutubeのダウンロードのためのライブラリーの更新も無くなってしまったんで、ちょっと将来的な運用は難しそうですね。
そこで今回Google Geminiに聞いてみると、Linux系のアプリyt-dipが良いということらしくて、今回試しに導入してみました。 もちろんAppleScriptで運用しますよ。
まずはMacPortsで準備
マックでLinux系のアプリを使うための管理ツール、私自身今までMacPortsを使っていたんで、それはインストールしてあったんですが大分更新してなかったんでいろいろトラブルがあり修正してみましたわ。 MacPortsを入れてから恐らくメジャーなシステムOSアップデートを2回程やっているせいで、まずは、MacPortsのバージョンが対応してなかったことと、利用に必要なCommandLineToolのライブラリーに古い設定が残っていて、それをインストールし直ししました。 この辺については私個人の問題なのでちょっと省略します。
MacPortsはbrewと同じ、Linuxのopen-source communityのライブラリーを使えるので重宝します。 インストール方法は3ステップ。
- このサイトから自分のOSにあった MacPorts-2.12.5-26-Tahoe.pkg をタウンロードしてインストール
- MacOSのサポートしているCommand Line Toolを xcode-select --install とターミナルに打ち込むことでインストラーが起動してインストールしてくれます。
- 起動の初回にX-codeのライセンスアグリーメントの案内が出るので、文章を読んで最後にTerminalに agree つまり同意ですね、って打ち込んでエンターキーを打ち込むとその後はMacPortsを使うことが出来ます。
まずMacPortsのライブラリーを最新版にするため
- sudo port selfupdate
次に今までインストールしている種々のプログラムをアップデートしておくために下のコマンドをたたく
- sudo port upgrade outdated
これでしばらくするとライブラリーが更新されます。
Youtube用のアプリ ffmpegとyt-dipをインストール
- sudo port install ffmpeg
- sudo port install yt-dip
これで一応いろいろ入れるけど大丈夫か確認を送ってきますので YesのYを入力してエンターするとインストールが始まります。 時々リンクエラーがあったりしてダウンロード出来ないものもあるので最後に
- sudo port selfupdate && sudo port upgrade yt-dlp
で確認して終了です。
これからが本番AppleScript
さてこれからはAppleScriptでの操作ですね。 今回はSafari等のURLをファインダーにドロップした時にでるリンクファイル 拡張子.weblocをアプレット型AppleScriptにドロップして、連続してファイルをダウンロードしてもらおうって寸法です。
|
##使用方法として、自分のDropbox/Scripts/module/と言うフォルダにTubeD.scptをおいて置きます。 アプレットに書き出した後も簡単にscptファイルを書き換えるだけで運用させるためですね。 この設計思想はM-loopとおんなじかな。
tell application "Finder" to set temp to selection as alias list --動作テスト用、ファイルを選択して動作させるとドロップしたときの挙動をテスト出来ます。
return main(temp) of me
on open theFiles --Dropさせてループさせます。 return main(theFiles) end open
on main(theFiles) set TubeD to load script file (((path to home folder from user domain) as string) & "Dropbox:Scripts:module:TubeD.scpt") as alias
repeat with aFile in theFiles ##アップルの.weblocファイルからURLを抽出する アップルスクリプト tell application "System Events" to set plistFile to property list file (POSIX path of aFile) tell application "System Events" to set extractedURL to value of property list item "URL" of contents of plistFile ##抽出したURLを使ってyt-dipに命令を出します。 if extractedURL starts with "https://www.youtube.com/" then DLYoutube({url_:extractedURL}) of TubeD end repeat
end main
on DLYoutube(q) set q to q & {url_:"", oFile:(path to desktop folder as string), terminal:true} --oFileの場所を変えると自分の好きな場所にダウンロードファイルを落としてくれます。 --set T to load script file (((path to home folder from user domain) as string) & "Dropbox:Scripts:module:Terminal.scpt") as alias --もとはこうやってロードしてるんですが一枚のファイルにする為変更してます。 if q's url_ is "" then return false --set cmd to "/opt/local/bin/yt-dlp -f 'bestvideo+bestaudio/best' --merge-output-format mp4 -P " & quoted form of (q's oFile)'s POSIX path & " " & quoted form of q's url_ --2026/8/24追記、上記だと最近動かないみたいです。 Youtubeの設定変更でcookiesを認識しに行くようになったようで、当方はSafariなのでSafariで設定してます。 set cmd to "/opt/local/bin/yt-dlp -f 'bestvideo+bestaudio/best' --merge-output-format mp4 -P " & quoted form of (q's oFile)'s POSIX path & " " & quoted form of q's url_ & " --cookies-from-browser safari" if q's terminal then term({ActionList:{"", "$Here", cmd, "$Here"}, ti:1, wSec:3600, closeWindow:true, WaitBusy:true}) of me else do shell script cmd --qの{terminal:false}に設定するとアップルスクリプト内でyt-dipを動作させてくれますが、問題があります。 どうも動画ファイルと音声ファイルが別々に保存されてしまうみたいで。 end if end DLYoutube
#これは以前M-loopで使ってましたね。 moduleから参照するとファイルが分かれちゃうんで、本体にコピーしました。 on term(q) --termは基本的に返値なし set q to {error_:""} & q & {tab_:"", ActionList:{}, wSec:10, ti:0.1, FailSafe:true, WaitBusy:false, closeWindow:true} --FailSafeはfalseならチェックなし所定時間経過したら進む。 --Prompt1, Action1, Prompt2, Action2 の順番で処理し、空白が出現した段階で停止する。 --"$Here"ならproHere, "$There"ならProThereを入れる set ProHere to "@*~" set ProThere to "@*~" if (do shell script "Echo " & (system info)'s system version & "|sed -e 's/\\(^[0-9]*.[0-9]*\\)[^0-9].*/\\1/g'") ≤ 10.13 then set ProHere to "~*\\$"
set max to round ((length of q's ActionList) / 2) --偶数個ないと最後は実行されない。 --tell application "Terminal" to close every window
repeat with i from 1 to max if q's tab_ is "" then tell application "Terminal" to set q's tab_ to do script item (i * 2 - 1) of q's ActionList else tell application "Terminal" to do script (item (i * 2 - 1) of q's ActionList) in q's tab_ end if delay q's ti set wPrompt to item (i * 2) of q's ActionList if wPrompt is "$Here" or wPrompt is "$local" or wPrompt is "$client" then set wPrompt to ProHere if wPrompt is "$There" or wPrompt is "$remote" or wPrompt is "$server" then set wPrompt to ProThere
set q to WaitPrompt({tab_:q's tab_, wPrompt:wPrompt} & q) of me & q if q's FailSafe then return {error_:"ActionList:" & item (i * 2) of q's ActionList & "が返却されない"} & q end repeat if q's closeWindow then tell application "Terminal" to close window 1 return q end term
--パスワードプロンプトはBusy待機中にBusyになる --return WaitPrompt({wPrompt:"@iMac-17 ~ %"}) on WaitPrompt(q) set q to q & {tab_:"", wSec:10, FailSafe:true, wPrompt:"", ti:0.1, SerchFullText:false, WaitBusy:true, LastLine:""}
if q's tab_ is "" then tell application "Terminal" to set q's tab_ to tab 1 of window 1 if q's wPrompt is "" then delay q's ti return {FailSafe:false} & q end if
tell application "Terminal" to tell q's tab_ if q's WaitBusy then repeat while busy is true delay q's ti end repeat end if if q's wPrompt is "" then return q set a to "" repeat q's wSec times set history_ to history as string set Len to 1 try set Len to (length of history_) - 1000 end try if Len < 1 then set Len to 1 set history_ to text Len thru end of history_
if q's SerchFullText then set a to do shell script "echo " & quoted form of (history as string) & "|grep -v '^$'|tr -d '^\\r'|tr \\\\r \\\\n|grep '" & q's wPrompt & "'||true" else try set a to do shell script "echo " & quoted form of (history_) & "|grep -v '^$'|tr -d '^\\r'|tr \\\\r \\\\n|tail -n 2|grep -B 1 '" & q's wPrompt & "'||true" end try end if if a is not "" then set q's FailSafe to false exit repeat end if delay 1 end repeat end tell return {LastLine:a} & q end WaitPrompt |
こんな感じで作ったSCPTファイルからアプレットに書き出しして、ファイルをドロップするとデスクトップにYoutubeのファイルがダウンロードされて保管されます。 もちろん最初に実行するときにシステムから許可の確認が来ます。 Dropboxフォルダへのアクセス。 System Eventsの使用の許可、Terminalへのアクセスの許可ですね。 一度やってしまえばその後はシステムに記録されますのでその後は聞かれることは無いでしょう。
自分的にはYoutubeの見たい映像のURLファイル weblocファイルをドロップしておいて、まとめてドラッグドロップして全部まとめてダウンロードして使ってます。





















