| 001 | #!/usr/bin/env osascript |
| 002 | #coding: utf-8 |
| 003 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
| 004 | (* |
| 005 |
|
| 006 | FFMEPGでメタデータをセットする |
| 007 | https://note.com/quicktimer/n/n50092fdb55fc |
| 008 |
|
| 009 | FFMPEGのインストールはこちらをみてください |
| 010 | https://note.com/quicktimer/n/n1712711c41cd |
| 011 |
|
| 012 |
|
| 013 |
|
| 014 |
|
| 015 | com.cocolog-nifty.quicktimer.icefloe *) |
| 016 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
| 017 | use AppleScript version "2.8" |
| 018 | use framework "Foundation" |
| 019 | use framework "AppKit" |
| 020 | use scripting additions |
| 021 | property refMe : a reference to current application |
| 022 | set appFileManager to refMe's NSFileManager's defaultManager() |
| 023 |
|
| 024 | ################################ |
| 025 | #設定項目 |
| 026 | #一般的にはこちら |
| 027 | #set strFfmpegFilePath to ("/usr/local/bin/ffmpeg") as text |
| 028 | ################################# |
| 029 | #ffmpeg へのパス |
| 030 | set strFfmpegFilePath to ("~/Library/Application Support/bin/ffmpeg/ffmpeg") as text |
| 031 | set ocidFfmpegFilePathStr to refMe's NSString's stringWithString:(strFfmpegFilePath) |
| 032 | set ocidFfmpegFilePathStr to ocidFfmpegFilePathStr's precomposedStringWithCanonicalMapping() |
| 033 | set ocidFfmpegFilePath to ocidFfmpegFilePathStr's stringByStandardizingPath() |
| 034 | set ocidFfmpegFilePath to ocidFfmpegFilePath's stringByExpandingTildeInPath() |
| 035 | set ocidFfmpegFilePathURL to refMe's NSURL's fileURLWithPath:(ocidFfmpegFilePath) isDirectory:(false) |
| 036 | set strFfmpegFilePath to ocidFfmpegFilePathURL's |path|() |
| 037 |
|
| 038 | ################################# |
| 039 | #ffprobe へのパス |
| 040 | #一般的にはこちら |
| 041 | #set strFfprobeFilePath to ("/usr/local/bin/ffprobe") as text |
| 042 | set strFfprobeFilePath to ("~/Library/Application Support/bin/ffmpeg/ffprobe") as text |
| 043 | set ocidFfprobeFilePathStr to refMe's NSString's stringWithString:(strFfprobeFilePath) |
| 044 | set ocidFilePathStr to ocidFfprobeFilePathStr's precomposedStringWithCanonicalMapping() |
| 045 | set ocidFfprobeFilePath to ocidFfprobeFilePathStr's stringByStandardizingPath() |
| 046 | set ocidFfprobeFilePath to ocidFfprobeFilePath's stringByExpandingTildeInPath() |
| 047 | set ocidFfprobeFilePathURL to refMe's NSURL's fileURLWithPath:(ocidFfprobeFilePath) isDirectory:(false) |
| 048 | set ocidFfprobeFilePath to ocidFfprobeFilePathURL's |path|() |
| 049 |
|
| 050 |
|
| 051 | ################################ |
| 052 | ####BINPATH ffmpegのパス |
| 053 | ################################ |
| 054 | #有無チェック |
| 055 | set boolExists to appFileManager's fileExistsAtPath:(ocidFfmpegFilePath) isDirectory:(false) |
| 056 | if boolExists is true then |
| 057 | log "【OK】指定のパスにFFMPEGがあります" |
| 058 | else if boolExists is false then |
| 059 | return "【NG】指定のパスにFFMPEGがありません" |
| 060 | end if |
| 061 |
|
| 062 | ################################ |
| 063 | #####書き出し先パス Moviesフォルダ内へ |
| 064 | ################################ |
| 065 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSMoviesDirectory) inDomains:(refMe's NSUserDomainMask)) |
| 066 | set ocidMoviesDirPathURL to ocidURLsArray's firstObject() |
| 067 | set ocidSaveDirPathURL to (ocidMoviesDirPathURL's URLByAppendingPathComponent:("_FFMPEG書出")) |
| 068 | set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s init() |
| 069 | ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions) |
| 070 | set listBoolMakeDir to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference) |
| 071 | ################################ |
| 072 | #####ダイアログを前面に |
| 073 | ################################ |
| 074 | tell current application |
| 075 | set strName to name as text |
| 076 | end tell |
| 077 | if strName is "osascript" then |
| 078 | tell application "Finder" to activate |
| 079 | else |
| 080 | tell current application to activate |
| 081 | end if |
| 082 |
|
| 083 | tell application "Finder" |
| 084 | set aliasDefaultLocation to container of (path to me) as alias |
| 085 | end tell |
| 086 |
|
| 087 | tell application "SystemUIServer" |
| 088 | activate |
| 089 | set aliasFilePath to (choose file with prompt "ファイルを選んでください" default location (aliasDefaultLocation) of type {"public.movie"} with invisibles and showing package contents without multiple selections allowed) as alias |
| 090 | end tell |
| 091 | ## |
| 092 | set strFilePath to (POSIX path of aliasFilePath) as text |
| 093 | set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath) |
| 094 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
| 095 | set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false) |
| 096 | #保存先 |
| 097 | set ocidFileName to ocidFilePathURL's lastPathComponent() |
| 098 | set ocidSaveFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:(ocidFileName) |
| 099 | set ocidSaveFilePath to ocidSaveFilePathURL's |path|() |
| 100 | ##ファイル名を取得(拡張子を除く) |
| 101 | set ocidBaseFilePathURL to ocidFilePathURL's URLByDeletingPathExtension() |
| 102 | set strFileName to ocidBaseFilePathURL's lastPathComponent() as text |
| 103 | # |
| 104 | set strSetBinFilePath to doPathEscape(strFfmpegFilePath) |
| 105 | set strSetFilePath to doPathEscape(strFilePath) |
| 106 | set strSetSaveFilePathURL to doPathEscape(ocidSaveFilePath) |
| 107 | set strSetFileName to doPathEscape(strFileName) |
| 108 | ######################################## |
| 109 | #タイトルを入力して行う場合(全てハイフンを入れてメタを削除するサンプル) |
| 110 | set strMetaTitle to ("-metadata title=\"-\"") as text |
| 111 | set strSetMeta to ("" & strMetaTitle & " " & "-metadata artist=\"-\"") as text |
| 112 | set strSetMeta to ("" & strSetMeta & " " & "-metadata album=\"-\"") as text |
| 113 | set strSetMeta to ("" & strSetMeta & " " & "-metadata copyright=\"-\"") as text |
| 114 | set strSetMeta to ("" & strSetMeta & " " & "-metadata comment=\"-\"") as text |
| 115 | set strSetMeta to ("" & strSetMeta & " " & "-metadata author=\"-\"") as text |
| 116 | set strSetMeta to ("" & strSetMeta & " " & "-metadata genre=\"-\"") as text |
| 117 | set strSetMeta to ("" & strSetMeta & " " & "-metadata description=\"-\"") as text |
| 118 | ##入力して設定する場合 |
| 119 | set strCmd to ("\"" & strSetBinFilePath & "\" -i \"" & strSetFilePath & "\" " & strSetMeta & " -codec:v copy -codec:a copy \"" & strSetSaveFilePathURL & "\"") as text |
| 120 |
|
| 121 | do shell script strCmd |
| 122 |
|
| 123 | #保存先を開く |
| 124 | set appShardWorkspace to refMe's NSWorkspace's sharedWorkspace() |
| 125 | set listDone to appShardWorkspace's openURL:(ocidSaveDirPathURL) |
| 126 | set appFinderArray to refMe's NSRunningApplication's runningApplicationsWithBundleIdentifier:("com.apple.finder") |
| 127 | set appFinder to appFinderArray's firstObject() |
| 128 | set boolDone to appFinder's activateWithOptions:(refMe's NSApplicationActivateIgnoringOtherApps) |
| 129 | return boolDone |
| 130 |
|
| 131 |
|
| 132 | ######################## |
| 133 | #パスのエスケープ |
| 134 | to doPathEscape(strFilePath) |
| 135 | set strFilePath to strFilePath as text |
| 136 | set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath) |
| 137 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
| 138 | set ocidFilePathURL to refMe's NSURL's fileURLWithPath:(ocidFilePath) isDirectory:(false) |
| 139 | set listEscChar to {"\\", "\"", "$", "`"} as list |
| 140 | repeat with itemEscChar in listEscChar |
| 141 | set ocidFilePath to (ocidFilePath's stringByReplacingOccurrencesOfString:(itemEscChar) withString:("\\" & itemEscChar & "")) |
| 142 | end repeat |
| 143 | set ocidFilePath to (ocidFilePath's stringByReplacingOccurrencesOfString:("!") withString:("\"'!'\"")) |
| 144 | |
| 145 | return ocidFilePath |
| 146 | end doPathEscape |