[swift]AVAssetでのビデオファイルからのメタ情報の取得
swiftにURLを投げてswiftで値を取得してもらう方法
他だと
ffmpegを使う
QuickTimeを使う等があるけど決め手にかけるので迷走中
AppleScript サンプルコード
行番号 | ソース |
---|---|
001 | #!/usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | # |
004 | # |
005 | # com.cocolog-nifty.quicktimer.icefloe |
006 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
007 | use AppleScript version "2.8" |
008 | use framework "Foundation" |
009 | use framework "AVFoundation" |
010 | use framework "AppKit" |
011 | use framework "UniformTypeIdentifiers" |
012 | use scripting additions |
013 | |
014 | property refMe : a reference to current application |
015 | set appFileManager to refMe's NSFileManager's defaultManager() |
016 | |
017 | ############################# |
018 | ###ダイアログ |
019 | set strName to (name of current application) as text |
020 | if strName is "osascript" then |
021 | tell application "Finder" to activate |
022 | else |
023 | tell current application to activate |
024 | end if |
025 | set appFileManager to refMe's NSFileManager's defaultManager() |
026 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask)) |
027 | set ocidDesktopDirPathURL to ocidURLsArray's firstObject() |
028 | set aliasDefaultLocation to (ocidDesktopDirPathURL's absoluteURL()) as alias |
029 | set listUTI to {"public.movie"} as list |
030 | set strMes to ("ファイルを選んでください") as text |
031 | set strPrompt to ("ファイルを選んでください") as text |
032 | try |
033 | set aliasFilePath to (choose file strMes with prompt strPrompt default location (aliasDefaultLocation) of type listUTI with invisibles and showing package contents without multiple selections allowed) as alias |
034 | on error |
035 | log "エラーしました" |
036 | return "エラーしました" |
037 | end try |
038 | |
039 | set strFilePath to (POSIX path of aliasFilePath) as text |
040 | set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath) |
041 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
042 | set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false) |
043 | set strFilePath to ocidFilePathURL's |path|() as text |
044 | |
045 | ############################# |
046 | ### swiftパス |
047 | ############################# |
048 | set aliasPathToMe to (path to me) as alias |
049 | set strPathToMe to (POSIX path of aliasPathToMe) as text |
050 | set ocidPathToMeStr to refMe's NSString's stringWithString:(strPathToMe) |
051 | set ocidPathToMe to ocidPathToMeStr's stringByStandardizingPath() |
052 | set ocidPathToMeURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidPathToMe) isDirectory:(false) |
053 | set ocidContainerDirPathURL to ocidPathToMeURL's URLByDeletingLastPathComponent() |
054 | set ocidBinDirPathURL to ocidContainerDirPathURL's URLByAppendingPathComponent:("bin") isDirectory:(true) |
055 | #不可視ファイル無視 |
056 | set ocidOption to (refMe's NSDirectoryEnumerationSkipsHiddenFiles) |
057 | #収集キー |
058 | set ocidKeyArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0) |
059 | ocidKeyArray's addObject:(refMe's NSURLPathKey) |
060 | ocidKeyArray's addObject:(refMe's NSURLIsSymbolicLinkKey) |
061 | ocidKeyArray's addObject:(refMe's NSURLIsDirectoryKey) |
062 | ocidKeyArray's addObject:(refMe's NSURLContentTypeKey) |
063 | #URLの収集 |
064 | set listResponse to (appFileManager's contentsOfDirectoryAtURL:(ocidBinDirPathURL) includingPropertiesForKeys:(ocidKeyArray) options:(ocidOption) |error| :(reference)) |
065 | set ocidSubPathURLArray to (item 1 of listResponse) |
066 | set listFileName to {} as list |
067 | repeat with itemURL in ocidSubPathURLArray |
068 | #ファイル名を |
069 | set strItemFileName to itemURL's lastPathComponent() as text |
070 | copy strItemFileName to beginning of listFileName |
071 | end repeat |
072 | ############################# |
073 | ### ダイアログ |
074 | ############################# |
075 | set strName to (name of current application) as text |
076 | if strName is "osascript" then |
077 | tell application "Finder" to activate |
078 | else |
079 | tell current application to activate |
080 | end if |
081 | ### |
082 | set strTitle to ("ムービーファイルを選んでください") as text |
083 | set strPrompt to ("ムービーファイルを選んでください") as text |
084 | try |
085 | set objResponse to (choose from list listFileName with title strTitle with prompt strPrompt default items (item 1 of listFileName) OK button name "OK" cancel button name "キャンセル" with empty selection allowed without multiple selections allowed) |
086 | on error |
087 | log "エラーしました" |
088 | return "エラーしました" |
089 | end try |
090 | # log class of objResponse |
091 | if (class of objResponse) is boolean then |
092 | return "キャンセルしましたA" |
093 | else if (class of objResponse) is list then |
094 | if objResponse is {} then |
095 | return "キャンセルしましたB" |
096 | else |
097 | set strResponse to (item 1 of objResponse) as text |
098 | end if |
099 | end if |
100 | |
101 | |
102 | ############################# |
103 | ### SWIFTファイルの情報取得 |
104 | ############################# |
105 | set ocidSwiftFilePathURL to ocidBinDirPathURL's URLByAppendingPathComponent:(strResponse) isDirectory:(false) |
106 | set strSwiftFilePath to ocidSwiftFilePathURL's |path| as text |
107 | |
108 | ################################ |
109 | ##タスク実行 |
110 | ################################ |
111 | log "開始:" & doGetDateNo("yyyyMMdd hhmmss") |
112 | |
113 | set ocidStartTime to refMe's NSDate's |date|() |
114 | |
115 | ################## |
116 | #ログディレクトリ |
117 | set appFileManager to refMe's NSFileManager's defaultManager() |
118 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSLibraryDirectory) inDomains:(refMe's NSUserDomainMask)) |
119 | set ocidLibraryDirPathURL to ocidURLsArray's firstObject() |
120 | set ocidLogDirPathURL to ocidLibraryDirPathURL's URLByAppendingPathComponent:("Logs") isDirectory:(true) |
121 | set ocidLogSaveDirPathURL to ocidLogDirPathURL's URLByAppendingPathComponent:("AppleScript") isDirectory:(true) |
122 | # |
123 | set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
124 | ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions) |
125 | set listBoolMakeDir to appFileManager's createDirectoryAtURL:(ocidLogSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error| :(reference) |
126 | |
127 | ################## |
128 | #ログファイル |
129 | set ocidCurrentDate to refMe's NSDate's |date|() |
130 | set appFormatter to refMe's NSDateFormatter's alloc()'s init() |
131 | appFormatter's setDateFormat:("yyyyMMddHHmmss") |
132 | set ocidNowDate to appFormatter's stringFromDate:(ocidCurrentDate) |
133 | set strNowDate to ocidNowDate as text |
134 | set strLogFileName to ("scriptlog." & strNowDate & ".log") as text |
135 | set ocidLogFilePathURL to ocidLogSaveDirPathURL's URLByAppendingPathComponent:(strLogFileName) isDirectory:(false) |
136 | set ocidLogFilePath to ocidLogFilePathURL's |path| |
137 | set strLogFilePath to ocidLogFilePath as text |
138 | |
139 | ################## |
140 | #ログファイル生成 |
141 | set ocidNulString to refMe's NSString's alloc()'s init() |
142 | set listDone to ocidNulString's writeToURL:(ocidLogFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error| :(reference) |
143 | |
144 | |
145 | ################## |
146 | #ログファイルのアクセス権 644 |
147 | ocidAttrDict's setValue:(420) forKey:(refMe's NSFilePosixPermissions) |
148 | set listDone to appFileManager's setAttributes:(ocidAttrDict) ofItemAtPath:(ocidLogFilePath) |error| :(reference) |
149 | |
150 | ################## |
151 | #実行時のカレントディレクトリ |
152 | set ocidCurrentDirPathURL to ocidLibraryDirPathURL's URLByAppendingPathComponent:("Containers/com.cocolog-nifty.quicktimer/Data/tmp/TemporaryItems") isDirectory:(true) |
153 | set listBoolMakeDir to appFileManager's createDirectoryAtURL:(ocidCurrentDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error| :(reference) |
154 | |
155 | ################## |
156 | #コマンド実行 |
157 | set ocidTermTask to refMe's NSTask's alloc()'s init() |
158 | ocidTermTask's setLaunchPath:("/usr/bin/swift") |
159 | set ocidArgumentsArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0) |
160 | ocidArgumentsArray's addObject:(strSwiftFilePath) |
161 | ocidArgumentsArray's addObject:(strFilePath) |
162 | ocidTermTask's setArguments:(ocidArgumentsArray) |
163 | set ocidOutPut to refMe's NSPipe's pipe() |
164 | set ocidError to refMe's NSPipe's pipe() |
165 | ocidTermTask's setStandardOutput:(ocidOutPut) |
166 | ocidTermTask's setStandardError:(ocidError) |
167 | ocidTermTask's setCurrentDirectoryURL:(ocidCurrentDirPathURL) |
168 | set listDoneReturn to ocidTermTask's launchAndReturnError:(reference) |
169 | if (item 1 of listDoneReturn) is (false) then |
170 | log "エラーコード:" & (item 2 of listDoneReturn)'s code() as text |
171 | log "エラードメイン:" & (item 2 of listDoneReturn)'s domain() as text |
172 | log "Description:" & (item 2 of listDoneReturn)'s localizedDescription() as text |
173 | log "FailureReason:" & (item 2 of listDoneReturn)'s localizedFailureReason() as text |
174 | end if |
175 | ################## |
176 | #終了待ち |
177 | ocidTermTask's waitUntilExit() |
178 | |
179 | log "終了:" & doGetDateNo("yyyyMMdd hhmmss") |
180 | log "経過時間: " & doGetTimeInderval(ocidStartTime) |
181 | |
182 | ################## |
183 | #標準出力をログに |
184 | set ocidOutPutData to ocidOutPut's fileHandleForReading() |
185 | set listResponse to ocidOutPutData's readDataToEndOfFileAndReturnError:(reference) |
186 | set ocidStdOut to (item 1 of listResponse) |
187 | set ocidStdOut to refMe's NSString's alloc()'s initWithData:(ocidStdOut) encoding:(refMe's NSUTF8StringEncoding) |
188 | set listDone to ocidStdOut's writeToURL:(ocidLogFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error| :(reference) |
189 | set ocidStdOut to (ocidStdOut's stringByReplacingOccurrencesOfString:("\n") withString:("")) |
190 | |
191 | ################## |
192 | #戻り値チェック |
193 | set numReturnNo to ocidTermTask's terminationStatus() as integer |
194 | if numReturnNo = 0 then |
195 | log ocidStdOut as text |
196 | return ocidStdOut as text |
197 | else |
198 | ################## |
199 | #エラーをログに |
200 | set ocidErrorData to ocidError's fileHandleForReading() |
201 | set listResponse to ocidErrorData's readDataToEndOfFileAndReturnError:(reference) |
202 | set ocidErrorOutData to (item 1 of listResponse) |
203 | set ocidErrorOutString to refMe's NSString's alloc()'s initWithData:(ocidErrorOutData) encoding:(refMe's NSUTF8StringEncoding) |
204 | set listResponse to refMe's NSFileHandle's fileHandleForWritingToURL:(ocidLogFilePathURL) |error| :(reference) |
205 | set ocidReadHandle to (item 1 of listResponse) |
206 | set listDone to ocidReadHandle's seekToEndReturningOffset:(0) |error| :(reference) |
207 | log (item 1 of listDone) as boolean |
208 | set listDone to ocidReadHandle's writeData:(ocidErrorOutData) |error| :(reference) |
209 | log (item 1 of listDone) as boolean |
210 | set listDone to ocidReadHandle's closeAndReturnError:(reference) |
211 | log (item 1 of listDone) as boolean |
212 | end if |
213 | |
214 | if numReturnNo = 1 then |
215 | return "1: スクリプトエラー" |
216 | else if numReturnNo = 2 then |
217 | return "2: 引数エラー" |
218 | else if numReturnNo = 126 then |
219 | return "126: アクセス権エラー" |
220 | else if numReturnNo = 127 then |
221 | return "127: スクリプトファイルが見つかりません" |
222 | else if numReturnNo = 130 then |
223 | return "130: 強制終了" |
224 | else if numReturnNo = 133 then |
225 | return "133: 異常終了" |
226 | end if |
227 | |
228 | |
229 | |
230 | ############################## |
231 | ### 今の日付日間 テキスト |
232 | ############################## |
233 | to doGetDateNo(argDateFormat) |
234 | ####日付情報の取得 |
235 | set ocidDate to current application's NSDate's |date|() |
236 | ###日付のフォーマットを定義 |
237 | set ocidNSDateFormatter to current application's NSDateFormatter's alloc()'s init() |
238 | set ocidLocale to current application's NSLocale's localeWithLocaleIdentifier:("ja_JP_POSIX") |
239 | ocidNSDateFormatter's setLocale:(ocidLocale) |
240 | set ocidTimeZone to current application's NSTimeZone's alloc()'s initWithName:("Asia/Tokyo") |
241 | ocidNSDateFormatter's setTimeZone:(ocidTimeZone) |
242 | ocidNSDateFormatter's setDateFormat:(argDateFormat) |
243 | set ocidDateAndTime to ocidNSDateFormatter's stringFromDate:(ocidDate) |
244 | set strDateAndTime to ocidDateAndTime as text |
245 | return strDateAndTime |
246 | end doGetDateNo |
247 | |
248 | |
249 | ################################ |
250 | ##経過時間計算 |
251 | ################################ |
252 | to doGetTimeInderval(argStartTime) |
253 | #今の時間 |
254 | set ocidEndTime to refMe's NSDate's |date|() |
255 | #渡されたスタート時間からの間隔を取得 |
256 | set numIntervalSec to ocidEndTime's timeIntervalSinceDate:(argStartTime) |
257 | #戻り値が秒数値なので時間にする |
258 | set numH to (numIntervalSec / 3600) as integer |
259 | set numM to ((numIntervalSec - (numH * 3600)) / 60) as integer |
260 | set numS to (numIntervalSec - (numH * 3600) - (numM * 60)) as integer |
261 | #2桁テキストにして |
262 | set appFormatter to refMe's NSNumberFormatter's alloc()'s init() |
263 | appFormatter's setMinimumIntegerDigits:(2) |
264 | appFormatter's setPaddingCharacter:("0") |
265 | appFormatter's setPaddingPosition:(refMe's NSNumberFormatterPadBeforePrefix) |
266 | set strH to (appFormatter's stringFromNumber:(numH)) as text |
267 | set strM to (appFormatter's stringFromNumber:(numM)) as text |
268 | set strS to (appFormatter's stringFromNumber:(numS)) as text |
269 | #戻り値用整形 |
270 | set strInterval to (strH & ":" & strM & ":" & strS) as text |
271 | return strInterval |
272 | end doGetTimeInderval |
273 | |
AppleScriptで生成しました |
| 固定リンク