20260523

[applescript] ビデオのpxサイズ取得AVFoundation + SWIFT

[applescript] ビデオのpxサイズ取得AVFoundation + SWIFT

NOTE記事一覧ですnote.com

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

ビデオのpxサイズ取得AVFoundationSWIFT.scpt.scpt
ソース
001#!/usr/bin/env osascript
002#coding: utf-8
003----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
004(*
005
006ちょっとエスケープに工夫した
007
008com.cocolog-nifty.quicktimer.icefloe *)
009----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
010use AppleScript version "2.8"
011use framework "Foundation"
012use scripting additions
013property refMe : a reference to current application
014
015
016#############################
017###入力ファイル
018#############################
019tell current application
020   set strName to name as text
021end tell
022if strName is "osascript" then
023   tell application "Finder" to activate
024else
025   tell current application to activate
026end if
027#デスクトップ
028set aliasDesktopDirPath to (path to desktop from user domain) as alias
029set listUTI to {"public.movie"} as list
030set strTitle to ("ファイル選択") as text
031set strPrompt to ("ムービーファイルを選んでください") as text
032tell application "SystemUIServer"
033   activate
034   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
035end tell
036
037#NSURLのArrayを作成して
038set ocidFileURLArrayM to refMe's NSMutableArray's alloc()'s init()
039
040repeat with itemAliasFilePath in listAliasFilePath
041   set aliasFilePath to itemAliasFilePath as alias
042   set strFilePath to (POSIX path of aliasFilePath) as text
043   set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePath))
044   set ocidFilePathStr to ocidFilePathStr's precomposedStringWithCanonicalMapping()
045   set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
046   set ocidFilePath to ocidFilePath's stringByExpandingTildeInPath()
047   set ocidFilePathURL to (refMe's NSURL's fileURLWithPath:(ocidFilePath) isDirectory:(false))
048   (ocidFileURLArrayM's addObject:(ocidFilePathURL))
049end repeat
050
051#ソート
052set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:("path") ascending:(yes) selector:("localizedStandardCompare:")
053set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor)
054set ocidFileURLArrayS to ocidFileURLArrayM's sortedArrayUsingDescriptors:(ocidDescriptorArray)
055
056#Arrayのアイテム数
057set numCntArray to ocidFileURLArrayS's |count|()
058
059repeat with itemIntNo from 0 to (numCntArray - 1) by 1
060   #開くムービーのエイリアス・パスをエイリアスリストから取得して
061   set ocidItemFileURL to (ocidFileURLArrayS's objectAtIndex:(itemIntNo))
062   set ocidItemFile to ocidItemFileURL's |path|()
063   set ocidItemFileENC to doPathEscape(ocidItemFile)
064   #SWIFT
065   set strSwiftCode to ("#!/usr/bin/env swift\n//coding: utf-8\nimport Foundation\nimport AVFoundation\nlet strFilePath = \\\"" & ocidItemFileENC & "\\\"\nlet urlFilePath: URL = URL(fileURLWithPath: strFilePath)\nlet refAsset: AVURLAsset = AVURLAsset(url: urlFilePath)\nTask {\nlet arrayTracks: [AVAssetTrack] = try await refAsset.loadTracks(withMediaType: AVMediaType.video)\nif arrayTracks.isEmpty {\nprint(\\\"false\\\")\n} else {\nlet videoTrack: AVAssetTrack = arrayTracks[0]\nlet sizeVideo: CGSize = try await videoTrack.load(.naturalSize)\nlet actualWidth: CGFloat  = abs(sizeVideo.width)\nlet actualHeight: CGFloat = abs(sizeVideo.height)\nprint(\\\"\\(Int(actualWidth))\\t\\(Int(actualHeight))\\\")\n}\n}\nThread.sleep(forTimeInterval: 1.0)") as text
066   #実行する
067   set strCmd to ("/bin/echo \"" & strSwiftCode & "\" | /usr/bin/swift - ")
068   set strStdOut to (do shell script strCmd) as text
069   
070   set strDelim to AppleScript's text item delimiters
071   set AppleScript's text item delimiters to tab
072   set listSize to every text item of strStdOut
073   set AppleScript's text item delimiters to strDelim
074   set numWpx to (first item of listSize) as integer
075   set numHpx to (last item of listSize) as integer
076   log numWpx
077   log numHpx
078end repeat
079
080return
081
082
083
084
085########################
086#パスのエスケープ
087to doPathEscape(strFilePath)
088   set strFilePath to strFilePath as text
089   set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
090   set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
091   set ocidFilePathURL to refMe's NSURL's fileURLWithPath:(ocidFilePath) isDirectory:(false)
092   
093   set ocidFilePath to (ocidFilePath's stringByReplacingOccurrencesOfString:("\\") withString:("\\\\\\"))
094   set ocidFilePath to (ocidFilePath's stringByReplacingOccurrencesOfString:("$") withString:("\\$"))
095   set ocidFilePath to (ocidFilePath's stringByReplacingOccurrencesOfString:("`") withString:("\\`"))
096   set ocidFilePath to (ocidFilePath's stringByReplacingOccurrencesOfString:("\"") withString:("\\\\\\\""))
097   set ocidFilePath to (ocidFilePath's stringByReplacingOccurrencesOfString:("!") withString:("\"'!'\""))
098   
099   return ocidFilePath
100end doPathEscape
AppleScriptで生成しました