« [Git-HUB]applescript-stdlib | トップページ | [URL]クエリー部分の置き換え »

[Youtube]ダウンロード・ヘルパー

WEBインスペクタを利用したダウンロードのヘルパー
ダウンロード
APIを利用してタイトルを取得
再エンコードまで実施
必須 ffmpegとYoutubeAPIキー



20220701160256_1628x842x144_0

20220701160306_1670x840x1440


#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
(*

videoplayback

*)
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions


property objMe : a reference to current application
property objNSString : a reference to objMe's NSString
property objNSMutableString : a reference to objMe's NSMutableString

property objNSURL : a reference to objMe's NSURL
property objNSDictionary : a reference to objMe's NSDictionary

property objNSArray : a reference to objMe's NSArray
property objNSMutableArray : a reference to objMe's NSMutableArray

property objNSJSONSerialization : a reference to objMe's NSJSONSerialization


#############################################
####設定項目
set strPathToFFMPEG to "echo $HOME/bin/ffmpeg.5.0.2/ffmpeg" as text
set strPathToFFMPEG to (do shell script strPathToFFMPEG)


#############################################
tell application "System Events"
set listAppList to title of (every process where background only is false)
end tell
repeat with objAppList in listAppList
set strAppList to objAppList as text
if strAppList is "スクリプトエディタ" then
tell application "Script Editor"
if frontmost is true then
try
tell application "System Events" to click menu item "ログを表示" of menu "表示" of menu bar item "表示" of menu bar 1 of application process "Script Editor"
end try
end if
end tell
end if
end repeat
#############################################


property strAPIkey : "AIzaSyCeh3883bLyCSumkUcdQWjD20uuiZw0dIo" as text

#############################################
###URL取得 Safari
try
tell application "Safari"
activate
set the strURL to the URL of document 1 as text
end tell
if strURL does not contain "youtube.com" then
error "" number -128
end if
on error
###URL取得Chrome
tell application "Google Chrome"
activate
tell window 1
tell active tab
set strURL to URL as text
end tell
end tell
end tell
if strURL does not contain "youtube.com" then
error "" number -128
end if
end try

#############################################
set AppleScript's text item delimiters to "/"
set listURL to every text item of strURL as list
set AppleScript's text item delimiters to ""
set strQuery to last text item of listURL as text
#############################################
set toQuery to strQuery as text
####watchがある=閲覧画面のVideoID
set AppleScript's text item delimiters to "&"
set listQuery to every text item of toQuery as list
set AppleScript's text item delimiters to ""
repeat with objQuery in listQuery
set AppleScript's text item delimiters to "="
set listObjQuery to every text item of objQuery as list
set AppleScript's text item delimiters to ""
if (text item 1 of listObjQuery as text) is "watch?v" then
####VideoID
set strVideoID to (text item 2 of listObjQuery as text)
else if (text item 1 of listObjQuery as text) is "v" then
####VideoID
set strVideoID to (text item 2 of listObjQuery as text)
end if
if (text item 1 of listObjQuery as text) is "list" then
####プレイリスト再生の場合のプレイリストID
set strPlayListID to (text item 2 of listObjQuery as text)
end if
end repeat

log "strPlayListID:" & strPlayListID
log "strVideoID:" & strVideoID

#############################################
#####ビデオIDJSON取得
set ocidVideoContents to doGetVideoID2Contents(strVideoID)
###### pageInfo
set ocidPageInfo to |pageInfo| of ocidVideoContents
###### totalResults
set ocidTotalResults to |totalResults| of ocidPageInfo
###### items
set ocidItemes to |items| of ocidVideoContents
###### snippet
set ocidSnippet to snippet of ocidItemes
#######タイトル
set ocidTitle to title of ocidSnippet
set strTitle to ocidTitle as text

#############################################
log strTitle as text

set aliasIconPath to POSIX file "/Applications/Safari.app/Contents/Resources/AppIcon.icns" as alias
tell application "Finder"
set theResponse to the clipboard as text
end tell
try
activate
set objResponse to (display dialog "詳しく" with title "短め" default answer theResponse buttons {"OK", "キャンセル"} default button "OK" cancel button "キャンセル" with icon aliasIconPath giving up after 30 without hidden answer)
on error
log "エラーしました"
return "エラーしました"
error number -128
end try
if true is equal to (gave up of objResponse) then
return "時間切れですやりなおしてください"
error number -128
end if
if "OK" is equal to (button returned of objResponse) then
set strResponse to (text returned of objResponse) as text
else
log "エラーしました"
return "エラーしました"
error number -128
end if

#############################################

set AppleScript's text item delimiters to "&"
set listURL to every text item of strResponse
set AppleScript's text item delimiters to ""


set strNewURL to "" as text
set numCntRepeat to 0 as number
repeat with objUrl in listURL
set strUrlQuery to objUrl as text
#########mine別に判定しておく
if strUrlQuery is "mime=audio%2Fwebm" then
set strFileType to "AudioWebm" as text
else if strUrlQuery is "mime=video%2Fwebm" then
set strFileType to "VideoWebm" as text
else if strUrlQuery is "mime=audio%2Fmp4" then
set strFileType to "AudioMp4" as text
else if strUrlQuery is "mime=video%2Fmp4" then
set strFileType to "VideoMp4" as text
end if
#########rangeを置き換える
if objUrl starts with "range=" then
set objUrl to "range=0-9999999999999999999999999999999999999" as text

end if
if numCntRepeat = 0 then
set strNewURL to (objUrl as text) as text
else
set strNewURL to ("" & strNewURL & "&" & objUrl & "") as text
end if

set numCntRepeat to numCntRepeat + 1 as number
end repeat

log strNewURL

#################################################
set theComandText to ("echo `date '+%Y%m%d_%H%M%S'`") as text
set theDate to (do shell script theComandText) as text

set theComandText to ("mkdir -pm 777 /tmp/" & theDate & "") as text
do shell script theComandText

#############################################
###### ダウンロードURL
#############################################
#########判定したmime別にコマンドを整形
if strFileType is "AudioWebm" then
set theComandText to ("/usr/bin/curl -L -o '/tmp/" & theDate & "/Audio.webm' '" & strNewURL & "' --connect-timeout 20") as text

else if strFileType is "VideoWebm" then
set theComandText to ("/usr/bin/curl -L -o '/tmp/" & theDate & "/Video.webm' '" & strNewURL & "' --connect-timeout 20") as text

else if strFileType is "AudioMp4" then
set theComandText to ("/usr/bin/curl -L -o '/tmp/" & theDate & "/Audio.mp4' '" & strNewURL & "' --connect-timeout 20") as text

else if strFileType is "VideoMp4" then
set theComandText to ("/usr/bin/curl -L -o '/tmp/" & theDate & "/Video.mp4'' '" & strNewURL & "' --connect-timeout 20") as text

end if
log strFileType
log theComandText

#############################################
##### こっちは終了待たせたいのでapplescriptで実行
do shell script theComandText


(*
tell application "Terminal"
launch
activate
set objWindowID to (do script "\n\n")
delay 1
do script theComandText in objWindowID

end tell

delay 10
*)
#############################################
######webmmp3mp4に変換
#############################################
set strDownLoadFolder to (POSIX path of (path to downloads folder from user domain as alias)) as text
set strAudioDistFilePath to ("" & strDownLoadFolder & strTitle & ".mp3") as text
set strVideDistFilePath to ("" & strDownLoadFolder & strTitle & ".mp4") as text

#########判定したmime別にコマンドを整形
if strFileType is "AudioWebm" then
set strCommandText to ("\"" & strPathToFFMPEG & "\" -i \"/tmp/" & theDate & "/Audio.webm\" -vn -ab 320k -ar 44100 -y \"" & strAudioDistFilePath & "\"") as text
else if strFileType is "VideoWebm" then
set strCommandText to ("\"" & strPathToFFMPEG & "\" -i \"/tmp/" & theDate & "/Video.webm\" -vcodec libx264 \"" & strVideDistFilePath & "\"") as text
else if strFileType is "AudioMp4" then
set strCommandText to ("\"" & strPathToFFMPEG & "\" -i \"/tmp/" & theDate & "/Audio.mp4\" -vn -ab 320k -ar 44100 -y \"" & strAudioDistFilePath & "\"") as text
else if strFileType is "VideoMp4" then
set strCommandText to ("\"" & strPathToFFMPEG & "\" -i \"/tmp/" & theDate & "/Video.mp4\" -vcodec libx264 \"" & strVideDistFilePath & "\"") as text
end if

##### コマンド実行
tell application "Terminal"
launch
activate
set objWindowID to (do script "\n\n")
delay 1
do script strCommandText in objWindowID
end tell

###############################################################
### ビデオのコンテンツJSON
###############################################################

to doGetVideoID2Contents(toVideoID)
set strThumbnailsHTML to ""
set jsonVideoIDcontents to ""
set jsonVideoResponceURL to "https://www.googleapis.com/youtube/v3/videos?id=" & toVideoID & "&part=snippet,contentDetails,liveStreamingDetails,player,recordingDetails,statistics,status,topicDetails&key=" & strAPIkey & "" as text
############コマンド整形
set strCommandText to "/usr/bin/curl -X GET -H 'Content-Type: application/json;charset=UTF-8' \"" & jsonVideoResponceURL & "\" --connect-timeout 20"
####コマンド実行
set jsonVideoIDcontents to (do shell script strCommandText) as text
####戻り値を格納
set ocidVideoIDcontents to (objNSJSONSerialization's JSONObjectWithData:((objNSString's stringWithString:jsonVideoIDcontents)'s dataUsingEncoding:(objMe's NSUTF8StringEncoding)) options:0 |error|:(missing value))
return ocidVideoIDcontents
end doGetVideoID2Contents

|

« [Git-HUB]applescript-stdlib | トップページ | [URL]クエリー部分の置き換え »

YouTube」カテゴリの記事