20260523

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

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

NOTE記事一覧ですnote.com
 

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

ビデオのpxサイズ取得AVFoundation.scpt.scpt
ソース
001#!/usr/bin/env osascript
002#coding: utf-8
003----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
004(*
005
006ビデオのpxサイズを取得する
007AVURLAssetを使っているが非同期にはなっていない
008
009
010com.cocolog-nifty.quicktimer.icefloe *)
011----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
012use AppleScript version "2.8"
013use framework "Foundation"
014use framework "AVFoundation"
015use framework "AppKit"
016use framework "UniformTypeIdentifiers"
017use scripting additions
018property refMe : a reference to current application
019
020
021#############################
022###入力ファイル
023#############################
024tell current application
025   set strName to name as text
026end tell
027if strName is "osascript" then
028   tell application "Finder" to activate
029else
030   tell current application to activate
031end if
032#デスクトップ
033set aliasDesktopDirPath to (path to desktop from user domain) as alias
034set listUTI to {"public.movie"} as list
035set strTitle to ("ファイル選択") as text
036set strPrompt to ("ムービーファイルを選んでください") as text
037tell application "SystemUIServer"
038   activate
039   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
040end tell
041
042#NSURLのArrayを作成して
043set ocidFileURLArrayM to refMe's NSMutableArray's alloc()'s init()
044
045repeat with itemAliasFilePath in listAliasFilePath
046   set aliasFilePath to itemAliasFilePath as alias
047   set strFilePath to (POSIX path of aliasFilePath) as text
048   set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePath))
049   set ocidFilePathStr to ocidFilePathStr's precomposedStringWithCanonicalMapping()
050   set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
051   set ocidFilePath to ocidFilePath's stringByExpandingTildeInPath()
052   set ocidFilePathURL to (refMe's NSURL's fileURLWithPath:(ocidFilePath) isDirectory:(false))
053   (ocidFileURLArrayM's addObject:(ocidFilePathURL))
054end repeat
055
056#ソート
057set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:("path") ascending:(yes) selector:("localizedStandardCompare:")
058set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor)
059set ocidFileURLArrayS to ocidFileURLArrayM's sortedArrayUsingDescriptors:(ocidDescriptorArray)
060
061#Arrayのアイテム数
062set numCntArray to ocidFileURLArrayS's |count|()
063
064repeat with itemIntNo from 0 to (numCntArray - 1) by 1
065   #開くムービーのエイリアス・パスをエイリアスリストから取得して
066   set ocidItemFileURL to (ocidFileURLArrayS's objectAtIndex:(itemIntNo))
067   ###縦横PXサイズ取得
068   set ocidOptionDict to refMe's NSMutableDictionary's alloc()'s init()
069   (ocidOptionDict's setObject:(true) forKey:(refMe's AVURLAssetPreferPreciseDurationAndTimingKey))
070   (ocidOptionDict's setObject:(true) forKey:(refMe's AVURLAssetURLRequestAttributionKey))
071   set ocidReadAsset to (refMe's AVURLAsset's alloc()'s initWithURL:(ocidItemFileURL) options:(ocidOptionDict))
072   #
073   set ocidVideoAssetTrack to (ocidReadAsset's tracksWithMediaType:(refMe's AVMediaTypeVideo))
074   set ocidReadAssetTrackArray to ocidReadAsset's tracks()
075   set ocidVideoTrack to ocidReadAssetTrackArray's firstObject()
076   if ocidVideoTrack = (missing value) then
077      log ocidFilePathURL's |path| as text
078      return "構造が不正なビデオがあります"
079   end if
080   #サイズを取得
081   set sizeTrack to ocidVideoTrack's naturalSize()
082   set numWpx to sizeTrack's width() as integer
083   set numHpx to sizeTrack's height() as integer
084   
085   log numWpx
086   log numHpx
087   
088end repeat
089
090
091return
AppleScriptで生成しました