Safariで表示中のYouTubeのムービーをローカルのMoviesフォルダにダウンロードして音声部分のみ抽出するAppleScriptです。
YouTubeからのダウンロードを行う「YouTubeDLLib」およびShane StanleyのAppleScript Libraries「BridgePlus」を~/Libraries/AppleScript Librariesフォルダに、XAttribute.frameworkを~/Libraries/Frameworksフォルダにインストールして実行してください。
YouTubeDLLib内部には「youtube-dl」を含んでおり、これ単体でアップデートが必要な場合もあります。急に動かなくなったような場合には、新バージョンが出ていないかチェックしてみてください。
Script Menuから実行することを前提としています。
AppleScript名:表示中のYouTubeのムービーをローカルにダウンロードして音声のみ抽出 |
use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "AVFoundation" use framework "XAttribute" –https://github.com/rylio/OTMXAttribute use BridgePlus : script "BridgePlus" –https://www.macosxautomation.com/applescript/apps/BridgePlus.html use youtubeLib : script "YouTubeDLLib" set dlFolder to POSIX path of (path to movies folder) –ダウンロード先のフォルダ set aURL to getPageURLOfFrontmostWindow() of me set aList to aURL load framework –指定URLのYouTubeのムービーをダウンロードする set aRes to downLoadMovieToFile(aURL, dlFolder) of youtubeLib –オーディオ部分のみ抽出 set dlFullPath to dlFolder & aRes –ダウンロードしたMovieからXattrを削除する set xRes to removeXAttrFromFile(dlFullPath, "com.apple.quarantine") my convertMovieAt:dlFullPath ToType:"AVAssetExportPresetAppleM4A" withExtension:"m4a" deleteOriginal:true on getPageURLOfFrontmostWindow() tell application "Safari" if (count every window) = 0 then return false tell window 1 set aProp to properties end tell set aDoc to document of aProp set aText to URL of aDoc end tell return aText end getPageURLOfFrontmostWindow on getPageTitleOfFrontmostWindow() tell application "Safari" if (count every window) = 0 then return false tell window 1 set aProp to properties end tell set aDoc to document of aProp set aText to name of aDoc end tell return aText end getPageTitleOfFrontmostWindow –指定のムービーを、指定の書き出しプリセットで、指定のファイル形式で、オリジナルファイルを削除するかどうか指定しつつ書き出し on convertMovieAt:(posixPath as string) ToType:(assetTypeStr as string) withExtension:(aExt as string) deleteOriginal:(deleteFlag as boolean) set theURL to current application’s |NSURL|’s fileURLWithPath:posixPath set aPreset to current application’s NSString’s stringWithString:assetTypeStr — set destination to use same path, different extension set destURL to theURL’s URLByDeletingPathExtension()’s URLByAppendingPathExtension:aExt set theAsset to current application’s AVAsset’s assetWithURL:theURL — check asset can be converted set allowedPresets to current application’s AVAssetExportSession’s exportPresetsCompatibleWithAsset:theAsset if (allowedPresets’s containsObject:aPreset) as boolean is false then error "Can’t export this file as an ." & aExt & " file." end if — set up export session set fileTypeUTIstr to retFileFormatUTI(aExt) of me –ファイル拡張子からFile Type UTIを求める set theSession to current application’s AVAssetExportSession’s exportSessionWithAsset:theAsset presetName:aPreset theSession’s setOutputFileType:fileTypeUTIstr theSession’s setOutputURL:destURL — begin export and poll for completion theSession’s exportAsynchronouslyWithCompletionHandler:(missing value) repeat set theStatus to theSession’s status() as integer if theStatus < 3 then delay 0.2 else exit repeat end if end repeat — throw error if it failed if theStatus = (current application’s AVAssetExportSessionStatusFailed) as integer then error (theSession’s |error|()’s localizedDescription() as text) end if — delete original if required if deleteFlag then current application’s NSFileManager’s defaultManager()’s removeItemAtURL:theURL |error|:(missing value) end if end convertMovieAt:ToType:withExtension:deleteOriginal: on retFileFormatUTI(aExt as string) return (current application’s SMSForder’s UTIForExtension:aExt) end retFileFormatUTI on removeXAttrFromFile(aFile, anXattr) –Get Xattr String set anAttribute to (current application’s OTMXAttribute’s stringAttributeAtPath:aFile |name|:anXattr |error|:(missing value)) if anAttribute = missing value then return true –There is no use to remove xattr –Remove Xattr set xRes to (current application’s OTMXAttribute’s removeAttributeAtPath:aFile |name|:anXattr |error|:(missing value)) if xRes = missing value then return false return (xRes as boolean) end removeXAttrFromFile |
More from my site
(Visited 42 times, 1 visits today)