[afconvert]オーディオファイル変換
あくまでも参考にしてください
行番号 | ソース |
---|---|
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 | #com.cocolog-nifty.quicktimer.icefloe |
013 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
014 | use AppleScript version "2.8" |
015 | use framework "Foundation" |
016 | use scripting additions |
017 | |
018 | property refMe : a reference to current application |
019 | |
020 | set appFileManager to refMe's NSFileManager's defaultManager() |
021 | |
022 | ############################# |
023 | ###ダイアログ ファイル選択 |
024 | set strName to (name of current application) as text |
025 | if strName is "osascript" then |
026 | tell application "Finder" to activate |
027 | else |
028 | tell current application to activate |
029 | end if |
030 | #デスクトップ |
031 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask)) |
032 | set ocidDesktopDirPathURL to ocidURLsArray's firstObject() |
033 | set aliasDefaultLocation to (ocidDesktopDirPathURL's absoluteURL()) as alias |
034 | #ダイアログ設定 |
035 | set listUTI to {"public.audio"} |
036 | set strMes to ("サウンドファイルを選んでください") as text |
037 | set strPrompt to ("サウンドファイルを選んでください") as text |
038 | try |
039 | ### ファイル選択時 |
040 | 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 |
041 | on error |
042 | log "エラーしました" |
043 | return "エラーしました" |
044 | end try |
045 | if listAliasFilePath is {} then |
046 | log "選んでください" |
047 | doResExec() |
048 | end if |
049 | |
050 | ############################# |
051 | ###フォーマット(変換後)選択 |
052 | 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 |
053 | |
054 | ##MP3等変換できないものがある |
055 | #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 |
056 | #キーでリスト |
057 | set ocidExtensionDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
058 | ocidExtensionDict's setDictionary:(recordExtension) |
059 | set ocidDataKeys to ocidExtensionDict's allKeys() |
060 | #ソートして |
061 | set listDataKeys to (ocidDataKeys's sortedArrayUsingSelector:("localizedStandardCompare:")) as list |
062 | # |
063 | set strTitle to ("選んでください") as text |
064 | set strPrompt to ("変換後のフォーマットを選んでください") as text |
065 | try |
066 | 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 |
067 | on error |
068 | log "エラーしました" |
069 | return "エラーしました" |
070 | end try |
071 | if (item 1 of listResponse) is false then |
072 | return "キャンセルしましたA" |
073 | else if (item 1 of listResponse) is "キャンセル" then |
074 | return "キャンセルしましたB" |
075 | else |
076 | set strDataType to (item 1 of listResponse) as text |
077 | end if |
078 | set strExtension to (ocidExtensionDict's objectForKey:(strDataType)) as text |
079 | |
080 | ### |
081 | 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 |
082 | set ocidFormatDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
083 | ocidFormatDict's setDictionary:(recordExtension) |
084 | set listDataFormat to (ocidFormatDict's objectForKey:(strDataType)) as list |
085 | # |
086 | set strTitle to ("選んでください") as text |
087 | set strPrompt to ("変換後のデータ・フォーマットを選んでください") as text |
088 | try |
089 | 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 |
090 | on error |
091 | log "エラーしました" |
092 | return "エラーしました" |
093 | end try |
094 | if (item 1 of listResponse) is false then |
095 | return "キャンセルしましたA" |
096 | else if (item 1 of listResponse) is "キャンセル" then |
097 | return "キャンセルしましたB" |
098 | else |
099 | set strDataFormat to (item 1 of listResponse) as text |
100 | end if |
101 | ### |
102 | 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 |
103 | set ocidOutPutDataFormat to refMe's NSDictionary's alloc()'s initWithDictionary:(recordOutPutDataFormat) |
104 | set strOutPutDataFormat to (ocidOutPutDataFormat's valueForKey:(strDataType)) |
105 | |
106 | repeat with itemFilePath in listAliasFilePath |
107 | #入力ファイル |
108 | set aliasFilePath to itemFilePath as alias |
109 | set strFilePath to (POSIX path of aliasFilePath) as text |
110 | set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePath)) |
111 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
112 | set ocidFileName to ocidFilePath's lastPathComponent() |
113 | #BaseFilePath |
114 | set ocidBaseFilePath to ocidFilePath's stringByDeletingPathExtension() |
115 | set ocidSaveFilePath to (ocidBaseFilePath's stringByAppendingPathExtension:(strExtension)) as text |
116 | # |
117 | set boolDirExists to (appFileManager's fileExistsAtPath:(ocidSaveFilePath) isDirectory:(true)) |
118 | if boolDirExists = true then |
119 | return "上書きになるので中止します" |
120 | else if boolDirExists = false then |
121 | log (ocidFileName as text) & ":処理開始" |
122 | end if |
123 | |
124 | ######## |
125 | if strDataFormat is "m4af" then |
126 | #ラウドネス情報付き |
127 | 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 |
128 | else |
129 | set strCommandText to ("/usr/bin/afconvert -v \"" & ocidFilePath & "\" -o \"" & ocidSaveFilePath & "\" -f '" & strOutPutDataFormat & "' -d '" & strDataFormat & "' --quality 127 --src-quality 127") as text |
130 | end if |
131 | log strCommandText |
132 | try |
133 | do shell script strCommandText |
134 | on error |
135 | return strFilePath & "\nでエラーしました" |
136 | end try |
137 | end repeat |
138 | |
139 | ############################## |
140 | #####自分自身を再実行 |
141 | ############################## |
142 | to doResExec() |
143 | if button returned of recordResult is "再実行" then |
144 | tell application "Finder" |
145 | set aliasPathToMe to (path to me) as alias |
146 | end tell |
147 | run script aliasPathToMe with parameters "再実行" |
148 | end if |
149 | end doResExec |
AppleScriptで生成しました |
| 固定リンク