[afconvert]オーディオファイル変換
AppleScript サンプルコード
行番号 | ソース |
---|---|
001 | #!/usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | # オーディオファイル変換 |
004 | (* |
005 | ヘルプ |
006 | /usr/bin/afconvert --help |
007 | フォーマットヘルプ |
008 | /usr/bin/afconvert --help-formats |
009 | 変換詳細 |
010 | https://developer.apple.com/library/archive/documentation/MusicAudio/Conceptual/CoreAudioOverview/SupportedAudioFormatsMacOSX/SupportedAudioFormatsMacOSX.html |
011 | |
012 | 20240522 v1 初回作成 |
013 | 20250213 v2 少し修正 |
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 | |
022 | property refMe : a reference to current application |
023 | |
024 | set appFileManager to refMe's NSFileManager's defaultManager() |
025 | |
026 | ############################# |
027 | ###ダイアログ ファイル選択 |
028 | set strName to (name of current application) as text |
029 | if strName is "osascript" then |
030 | tell application "Finder" to activate |
031 | else |
032 | tell current application to activate |
033 | end if |
034 | #デスクトップ |
035 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask)) |
036 | set ocidDesktopDirPathURL to ocidURLsArray's firstObject() |
037 | set aliasDefaultLocation to (ocidDesktopDirPathURL's absoluteURL()) as alias |
038 | #ダイアログ設定 |
039 | set listUTI to {"public.audio"} |
040 | set strMes to ("サウンドファイルを選んでください") as text |
041 | set strPrompt to ("サウンドファイルを選んでください") as text |
042 | try |
043 | ### ファイル選択時 |
044 | set listAliasFilePath to (choose file strMes with prompt strPrompt default location aliasDefaultLocation of type listUTI with invisibles, multiple selections allowed and showing package contents) as list |
045 | on error |
046 | log "エラーしました" |
047 | return "エラーしました" |
048 | end try |
049 | if listAliasFilePath is {} then |
050 | log "選んでください" |
051 | doResExec() |
052 | end if |
053 | |
054 | ############################# |
055 | ###フォーマット(変換後)選択 |
056 | set recordExtension to {|3GP Audio|:"3gp", |3GPP-2|:"3gp2", |AAC ADTS|:"adts", |AIFC|:"aifc", |AIFF|:"aiff", |Apple MPEG-4 Audio|:"m4a", |Apple MPEG-4 AudioBooks|:"m4b", |Core Audio Format|:"caf", |FLAC|:"flac", |LOAS|:"loas", |LATM|:"latm", |MPEG-4 Audio|:"mp4", |WAVE|:"wav", |WAVE-BW64|:"wav", |WAVE-RF64|:"wav", |WAVE-W64F|:"w64"} as record |
057 | |
058 | ##MP3等変換できないものがある |
059 | #set recordExtension to {|3GP Audio|:"3gp", |3GPP-2|:"3gp2", |AAC ADTS|:"adts", |AC3|:"ac3", |AIFC|:"aifc", |AIFF|:"aiff", |AMR|:"amr", |Apple MPEG-4 Audio|:"m4a", |Apple MPEG-4 AudioBooks|:"m4b", |Core Audio Format|:"caf", |EC3|:"eac3", |FLAC|:"flac", |LOAS|:"loas", |LATM|:"latm", |MPEG Layer 1|:"mp1", |MPEG Layer 2|:"m2a", |MPEG Layer 3|:"mp3", |MPEG-4 Audio|:"mp4", |NeXT/Sun|:"au", |Sound Designer II|:"sd2", |WAVE|:"wav", |WAVE-BW64|:"wav", |WAVE-RF64|:"wav", |WAVE-W64F|:"w64"} as record |
060 | #キーでリスト |
061 | set ocidExtensionDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
062 | ocidExtensionDict's setDictionary:(recordExtension) |
063 | set ocidDataKeys to ocidExtensionDict's allKeys() |
064 | #ソートして |
065 | set listDataKeys to (ocidDataKeys's sortedArrayUsingSelector:("localizedStandardCompare:")) as list |
066 | # |
067 | set strTitle to ("選んでください") as text |
068 | set strPrompt to ("変換後のフォーマットを選んでください") as text |
069 | try |
070 | set listResponse to (choose from list listDataKeys with title strTitle with prompt strPrompt default items (item 8 of listDataKeys) OK button name "OK" cancel button name "キャンセル" without multiple selections allowed and empty selection allowed) as list |
071 | on error |
072 | log "エラーしました" |
073 | return "エラーしました" |
074 | end try |
075 | if (item 1 of listResponse) is false then |
076 | return "キャンセルしましたA" |
077 | else if (item 1 of listResponse) is "キャンセル" then |
078 | return "キャンセルしましたB" |
079 | else |
080 | set strDataType to (item 1 of listResponse) as text |
081 | end if |
082 | set strExtension to (ocidExtensionDict's objectForKey:(strDataType)) as text |
083 | |
084 | ### |
085 | set recordExtension to {|3GP Audio|:{"aac", "aace", "aacf", "aacg", "aach", "aacl", "aacp", "samr"}, |3GPP-2|:{"aac", "aace", "aacf", "aacg", "aach", "aacl", "aacp", "samr"}, |AAC ADTS|:{"aac ", "aach", "aacp"}, |AC3|:{"ac-3"}, |AIFC|:{"I8", "BEI16", "BEI24", "BEI32", "BEF32", "BEF64", "UI8", "ulaw", "alaw", "MAC3", "MAC6", "ima4", "QDMC", "QDM2", "Qclp", "agsm"}, |AIFF|:{"I8", "BEI16", "BEI24", "BEI32"}, |AMR|:{"samr", "sawb"}, |Apple MPEG-4 Audio|:{".mp1", ".mp2", ".mp3", "aac ", "aace", "aacf", "aacg", "aach", "aacl", "aacp", "ac-3", "alac", "ec-3", "flac", "paac", "pac3", "pec3"}, |Apple MPEG-4 AudioBooks|:{"aac ", "aace", "aacf", "aacg", "aach", "aacl", "aacp", "paac"}, |Core Audio Format|:{".mp1", ".mp2", ".mp3", "aac ", "aace", "aacf", "aacg", "aach", "aacl", "aacp", "ac-3", "alac", "alaw", "apac", "dvi8", "ec-3", "flac", "ilbc", "ima4", "opus", "paac", "pac3", "pach", "pacp", "pec3", "pusa", "qaac", "qac3", "qach", "qacp", "qapc", "qec3", "qflc", "qlac", "qusa", "samr", "ulaw", "usac", "zaac", "zac3", "zach", "zacp", "zec3", "zlac", "I8", "BEI16", "BEI24", "BEI32", "BEF32", "BEF64", "UI8"}, |EC3|:{"ec-3"}, |FLAC|:{"flac"}, |LOAS|:{"aac ", "aace", "aacf", "aacg", "aach", "aacl", "aacp", "usac"}, |LATM|:{"aac ", "aace", "aacf", "aacg", "aach", "aacl", "aacp", "usac"}, |MPEG Layer 1|:{".mp1"}, |MPEG Layer 2|:{".mp2"}, |MPEG Layer 3|:{".mp3"}, |MPEG-4 Audio|:{".mp1", ".mp2", ".mp3", "aac ", "aace", "aacf", "aacg", "aach", "aacl", "aacp", "ac-3", "alac", "apac", "ec-3", "flac", "opus", "paac", "pac3", "pach", "pacp", "pec3", "pusa", "qaac", "qac3", "qach", "qacp", "qapc", "qec3", "qflc", "qlac", "qusa", "usac", "LEF32", "BEF32", "LEF64", "BEF64", "LEI32", "BEI32", "UI0", "UI0", "UI0", "UI0"}, |NeXT/Sun|:{"I8", "BEI16", "BEI24", "BEI32", "BEF32", "BEF64", "ulaw", "alaw"}, |Sound Designer II|:{"I8", "BEI16", "BEI24", "BEI32"}, |WAVE|:{"UI8", "LEI16", "LEI24", "LEI32", "LEF32", "LEF64", "ulaw", "alaw"}, |WAVE-BW64|:{"UI8", "LEI16", "LEI24", "LEI32", "LEF32", "LEF64", "ulaw", "alaw"}, |WAVE-RF64|:{"UI8", "LEI16", "LEI24", "LEI32", "LEF32", "LEF64", "ulaw", "alaw"}, |WAVE-W64F|:{"UI8", "LEI16", "LEI24", "LEI32", "LEF32", "LEF64", "ulaw", "alaw"}} as record |
086 | set ocidFormatDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
087 | ocidFormatDict's setDictionary:(recordExtension) |
088 | set listDataFormat to (ocidFormatDict's objectForKey:(strDataType)) as list |
089 | # |
090 | set strTitle to ("選んでください") as text |
091 | set strPrompt to ("変換後のデータ・フォーマットを選んでください") as text |
092 | try |
093 | set listResponse to (choose from list listDataFormat with title strTitle with prompt strPrompt default items (item 1 of listDataFormat) OK button name "OK" cancel button name "キャンセル" without multiple selections allowed and empty selection allowed) as list |
094 | on error |
095 | log "エラーしました" |
096 | return "エラーしました" |
097 | end try |
098 | if (item 1 of listResponse) is false then |
099 | return "キャンセルしましたA" |
100 | else if (item 1 of listResponse) is "キャンセル" then |
101 | return "キャンセルしましたB" |
102 | else |
103 | set strDataFormat to (item 1 of listResponse) as text |
104 | end if |
105 | ### |
106 | set recordOutPutDataFormat to {|3GP Audio|:"3gpp", |3GPP-2|:"3gp2", |AAC ADTS|:"adts", |AC3|:"ac-3", |AIFC|:"AIFC", |AIFF|:"AIFF", |AMR|:"amrf", |Apple MPEG-4 Audio|:"m4af", |Apple MPEG-4 AudioBooks|:"m4bf", |Core Audio Format|:"caff", |EC3|:"ec-3", |FLAC|:"flac", |LOAS|:"loas", |LATM|:"loas", |MPEG Layer 1|:"MPG1", |MPEG Layer 2|:"MPG2", |MPEG Layer 3|:"MPG3", |MPEG-4 Audio|:"mp4f", |NeXT/Sun|:"NeXT", |Sound Designer II|:"Sd2f", |WAVE|:"WAVE", |WAVE-BW64|:"BW64", |WAVE-RF64|:"RF64", |WAVE-W64F|:"W64f"} as record |
107 | set ocidOutPutDataFormat to refMe's NSDictionary's alloc()'s initWithDictionary:(recordOutPutDataFormat) |
108 | set strOutPutDataFormat to (ocidOutPutDataFormat's valueForKey:(strDataType)) |
109 | |
110 | repeat with itemFilePath in listAliasFilePath |
111 | #入力ファイル |
112 | set aliasFilePath to itemFilePath as alias |
113 | set strFilePath to (POSIX path of aliasFilePath) as text |
114 | set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePath)) |
115 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
116 | set ocidFileName to ocidFilePath's lastPathComponent() |
117 | set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFileName) isDirectory:(false)) |
118 | set ocidContainerDirPathURL to ocidFilePathURL's URLByDeletingLastPathComponent() |
119 | #BaseFilePath |
120 | set ocidBaseFilePath to ocidFilePath's stringByDeletingPathExtension() |
121 | set ocidSaveFilePath to (ocidBaseFilePath's stringByAppendingPathExtension:(strExtension)) as text |
122 | # |
123 | set boolDirExists to (appFileManager's fileExistsAtPath:(ocidSaveFilePath) isDirectory:(true)) |
124 | if boolDirExists = true then |
125 | return "上書きになるので中止します" |
126 | else if boolDirExists = false then |
127 | log (ocidFileName as text) & ":処理開始" |
128 | end if |
129 | |
130 | ######## |
131 | if strDataFormat is "m4af" then |
132 | #ラウドネス情報付き |
133 | set strCommandText to ("/usr/bin/afconvert -v \"" & ocidFilePath & "\" -o \"" & ocidSaveFilePath & "\" -f '" & strOutPutDataFormat & "' -d '" & strDataFormat & "' --quality 127 --src-quality 127 --soundcheck-generate --media-kind \"Music\"") as text |
134 | else |
135 | set strCommandText to ("/usr/bin/afconvert -v \"" & ocidFilePath & "\" -o \"" & ocidSaveFilePath & "\" -f '" & strOutPutDataFormat & "' -d '" & strDataFormat & "' --quality 127 --src-quality 127") as text |
136 | end if |
137 | log strCommandText |
138 | try |
139 | do shell script strCommandText |
140 | on error |
141 | return strFilePath & "\nでエラーしました" |
142 | end try |
143 | end repeat |
144 | |
145 | set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace() |
146 | set boolDone to appSharedWorkspace's openURL:(ocidContainerDirPathURL) |
147 | |
148 | |
149 | |
150 | ############################## |
151 | #####自分自身を再実行 |
152 | ############################## |
153 | to doResExec() |
154 | if button returned of recordResult is "再実行" then |
155 | tell application "Finder" |
156 | set aliasPathToMe to (path to me) as alias |
157 | end tell |
158 | run script aliasPathToMe with parameters "再実行" |
159 | end if |
160 | end doResExec |
AppleScriptで生成しました |
| 固定リンク