20260523

[applescript] ビデオのpxサイズ取得 python3 スクリプトを使う

[applescript] ビデオのpxサイズ取得 python3 スクリプトを使う

NOTE記事一覧ですnote.com
 

【スクリプトエディタで開く】 |

ビデオのpxサイズ取得AVFoundationPy.scpt.scpt
ソース
001#!/usr/bin/env osascript
002#coding: utf-8
003----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
004(*
005python3版
006ちょっとエスケープに工夫した
007
008
009モジュールは、ご自身のpy環境にあわせて実行してください
010モジュールインストールローカルインストール(ローカル=要管理者権限)
011/usr/bin/sudo /usr/bin/python3 -m pip install --upgrade --no-warn-script-location opencv-python
012
013ユーザーインストール(ユーザー=管理者権限不要)
014/usr/bin/python3 -m pip install --upgrade --user --no-warn-script-location opencv-python
015
016com.cocolog-nifty.quicktimer.icefloe *)
017----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
018use AppleScript version "2.8"
019use framework "Foundation"
020use scripting additions
021property refMe : a reference to current application
022
023
024#############################
025###入力ファイル
026#############################
027tell current application
028   set strName to name as text
029end tell
030if strName is "osascript" then
031   tell application "Finder" to activate
032else
033   tell current application to activate
034end if
035#デスクトップ
036set aliasDesktopDirPath to (path to desktop from user domain) as alias
037set listUTI to {"public.movie"} as list
038set strTitle to ("ファイル選択") as text
039set strPrompt to ("ムービーファイルを選んでください") as text
040tell application "SystemUIServer"
041   activate
042   set listAliasFilePath to (choose file with prompt (strPrompt) default location (aliasDesktopDirPath) of type (listUTI) with invisibles, showing package contents and multiple selections allowed) as list
043end tell
044
045#NSURLのArrayを作成して
046set ocidFileURLArrayM to refMe's NSMutableArray's alloc()'s init()
047
048repeat with itemAliasFilePath in listAliasFilePath
049   set aliasFilePath to itemAliasFilePath as alias
050   set strFilePath to (POSIX path of aliasFilePath) as text
051   set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePath))
052   set ocidFilePathStr to ocidFilePathStr's precomposedStringWithCanonicalMapping()
053   set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
054   set ocidFilePath to ocidFilePath's stringByExpandingTildeInPath()
055   set ocidFilePathURL to (refMe's NSURL's fileURLWithPath:(ocidFilePath) isDirectory:(false))
056   (ocidFileURLArrayM's addObject:(ocidFilePathURL))
057end repeat
058
059#ソート
060set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:("path") ascending:(yes) selector:("localizedStandardCompare:")
061set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor)
062set ocidFileURLArrayS to ocidFileURLArrayM's sortedArrayUsingDescriptors:(ocidDescriptorArray)
063
064#Arrayのアイテム数
065set numCntArray to ocidFileURLArrayS's |count|()
066
067repeat with itemIntNo from 0 to (numCntArray - 1) by 1
068   #開くムービーのエイリアス・パスをエイリアスリストから取得して
069   set ocidItemFileURL to (ocidFileURLArrayS's objectAtIndex:(itemIntNo))
070   set ocidItemFile to ocidItemFileURL's |path|()
071   set ocidItemFileENC to doPathEscape(ocidItemFile)
072   #SWIFT
073   set strPyCode to ("#!/usr/bin/env python3\n#coding: utf-8\nimport cv2\nstr_file_path = \\\"" & ocidItemFileENC & "\\\"\ncap_video = cv2.VideoCapture(str_file_path)\nbool_is_opened = cap_video.isOpened()\nif not bool_is_opened:\n    print(\\\"動画ファイルを開けませんでした\\\")\nelse:\n    float_width  = cap_video.get(cv2.CAP_PROP_FRAME_WIDTH)\n    float_height = cap_video.get(cv2.CAP_PROP_FRAME_HEIGHT)\n    int_width  = int(float_width)\n    int_height = int(float_height)\n    cap_video.release()\n    print(f\\\"{int_width}\\t{int_height}\\\")") as text
074   #実行する
075   set strCmd to ("/usr/bin/python3 -c \"" & strPyCode & "\"")
076   set strStdOut to (do shell script strCmd) as text
077   
078   set strDelim to AppleScript's text item delimiters
079   set AppleScript's text item delimiters to tab
080   set listSize to every text item of strStdOut
081   set AppleScript's text item delimiters to strDelim
082   set numWpx to (first item of listSize) as integer
083   set numHpx to (last item of listSize) as integer
084   log numWpx
085   log numHpx
086end repeat
087
088return
089
090
091
092
093########################
094#パスのエスケープ
095to doPathEscape(strFilePath)
096   set strFilePath to strFilePath as text
097   set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
098   set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
099   set ocidFilePathURL to refMe's NSURL's fileURLWithPath:(ocidFilePath) isDirectory:(false)
100   
101   set ocidFilePath to (ocidFilePath's stringByReplacingOccurrencesOfString:("\\") withString:("\\\\\\"))
102   set ocidFilePath to (ocidFilePath's stringByReplacingOccurrencesOfString:("$") withString:("\\$"))
103   set ocidFilePath to (ocidFilePath's stringByReplacingOccurrencesOfString:("`") withString:("\\`"))
104   set ocidFilePath to (ocidFilePath's stringByReplacingOccurrencesOfString:("\"") withString:("\\\\\\\""))
105   set ocidFilePath to (ocidFilePath's stringByReplacingOccurrencesOfString:("!") withString:("\"'!'\""))
106   
107   return ocidFilePath
108end doPathEscape
AppleScriptで生成しました