Media

[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変換詳細
010https://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
014use AppleScript version "2.8"
015use framework "Foundation"
016use scripting additions
017
018property refMe : a reference to current application
019
020set appFileManager to refMe's NSFileManager's defaultManager()
021
022#############################
023###ダイアログ ファイル選択
024set strName to (name of current application) as text
025if strName is "osascript" then
026  tell application "Finder" to activate
027else
028  tell current application to activate
029end if
030#デスクトップ
031set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
032set ocidDesktopDirPathURL to ocidURLsArray's firstObject()
033set aliasDefaultLocation to (ocidDesktopDirPathURL's absoluteURL()) as alias
034#ダイアログ設定
035set listUTI to {"public.audio"}
036set strMes to ("サウンドファイルを選んでください") as text
037set strPrompt to ("サウンドファイルを選んでください") as text
038try
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
041on error
042  log "エラーしました"
043  return "エラーしました"
044end try
045if listAliasFilePath is {} then
046  log "選んでください"
047  doResExec()
048end if
049
050#############################
051###フォーマット(変換後)選択
052set 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#キーでリスト
057set ocidExtensionDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
058ocidExtensionDict's setDictionary:(recordExtension)
059set ocidDataKeys to ocidExtensionDict's allKeys()
060#ソートして
061set listDataKeys to (ocidDataKeys's sortedArrayUsingSelector:("localizedStandardCompare:")) as list
062#
063set strTitle to ("選んでください") as text
064set strPrompt to ("変換後のフォーマットを選んでください") as text
065try
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
067on error
068  log "エラーしました"
069  return "エラーしました"
070end try
071if (item 1 of listResponse) is false then
072  return "キャンセルしましたA"
073else if (item 1 of listResponse) is "キャンセル" then
074  return "キャンセルしましたB"
075else
076  set strDataType to (item 1 of listResponse) as text
077end if
078set strExtension to (ocidExtensionDict's objectForKey:(strDataType)) as text
079
080###
081set 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
082set ocidFormatDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
083ocidFormatDict's setDictionary:(recordExtension)
084set listDataFormat to (ocidFormatDict's objectForKey:(strDataType)) as list
085#
086set strTitle to ("選んでください") as text
087set strPrompt to ("変換後のデータ・フォーマットを選んでください") as text
088try
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
090on error
091  log "エラーしました"
092  return "エラーしました"
093end try
094if (item 1 of listResponse) is false then
095  return "キャンセルしましたA"
096else if (item 1 of listResponse) is "キャンセル" then
097  return "キャンセルしましたB"
098else
099  set strDataFormat to (item 1 of listResponse) as text
100end if
101###
102set 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
103set ocidOutPutDataFormat to refMe's NSDictionary's alloc()'s initWithDictionary:(recordOutPutDataFormat)
104set strOutPutDataFormat to (ocidOutPutDataFormat's valueForKey:(strDataType))
105
106repeat 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
137end repeat
138
139##############################
140#####自分自身を再実行
141##############################
142to 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
149end doResExec
AppleScriptで生成しました

|

オーディオの入出力切り替え


あくまでも参考にしてください

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

サンプルソース(参考)
行番号ソース
001#! /usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003(*
004参考
005https://www.macscripter.net/t/switchaudio-a-command-line-tool-to-change-the-audio-input-and-output-device/75630
006バイナリーへの直リンク
007https://klieme.ch/pub/SwitchAudio.dmg
008*)
009# com.cocolog-nifty.quicktimer.icefloe
010----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
011use AppleScript version "2.8"
012use framework "Foundation"
013use framework "AppKit"
014use scripting additions
015
016property refMe : a reference to current application
017#バイナリーへのパス
018set strBinPath to ("~/bin/SwitchAudio/SwitchAudio") as text
019set ocidBinPathStr to refMe's NSString's stringWithString:(strBinPath)
020set ocidBinPath to ocidBinPathStr's stringByStandardizingPath()
021#リスト取得
022set strCommandText to ("\"" & ocidBinPath & "\"  -l") as text
023set strResponse to (do shell script strCommandText) as text
024#入出力でわける
025set strDelim to AppleScript's text item delimiters
026set AppleScript's text item delimiters to "\r\r"
027set listResponse to every text item of strResponse as list
028set AppleScript's text item delimiters to strDelim
029#
030set strResponeInput to (item 1 of listResponse) as text
031set strResponeOutput to (item 2 of listResponse) as text
032#個別項目へ
033set strDelim to AppleScript's text item delimiters
034set AppleScript's text item delimiters to "\r"
035set listInput to every text item of strResponeInput as list
036set listOutput to every text item of strResponeOutput as list
037set listChooser to every text item of strResponse as list
038set AppleScript's text item delimiters to strDelim
039#ダイアログ
040set strName to (name of current application) as text
041if strName is "osascript" then
042  tell application "Finder" to activate
043else
044  tell current application to activate
045end if
046set strTitle to ("選んでください") as text
047set strPrompt to ("切り替えるデバイスを選んでください") as text
048try
049  set listChoose to (choose from list listChooser with title strTitle with prompt strPrompt default items (item 2 of listChooser) OK button name "OK" cancel button name "キャンセル" without multiple selections allowed and empty selection allowed) as list
050on error
051  log "エラーしました"
052  return "エラーしました"
053end try
054if (item 1 of listChoose) is false then
055  return "キャンセルしましたA"
056else if (item 1 of listChoose) is "キャンセル" then
057  return "キャンセルしましたB"
058else
059  set strChoose to (item 1 of listChoose) as text
060end if
061if strChoose is "Input Devices:" then
062  log "デバイス以外を選択"
063  doReload()
064else if strChoose is "Output Devices:" then
065  log "デバイス以外を選択"
066  doReload()
067else if strChoose is "" then
068  log "デバイス以外を選択"
069  doReload()
070else if listInput contains strChoose then
071  log "入力デバイスが選択されていました"
072  set strCommandText to ("\"" & ocidBinPath & "\"  -i \"" & strChoose & "\"") as text
073  try
074    do shell script strCommandText
075  end try
076else if listOutput contains strChoose then
077  log "出力デバイスが選択されていました"
078  set strCommandText to ("\"" & ocidBinPath & "\"  -o \"" & strChoose & "\"") as text
079  try
080    do shell script strCommandText
081  end try
082end if
083
084
085###デバイス以外を選択したら
086to doReload()
087  #自分自信を再実行
088  tell application "Finder"
089    set aliasPathToMe to (path to me) as alias
090  end tell
091  run script aliasPathToMe with parameters "再実行"
092end doReload
AppleScriptで生成しました

|

その他のカテゴリー

Accessibility Acrobat Acrobat 2020 Acrobat AddOn Acrobat Annotation Acrobat ARMDC Acrobat AV2 Acrobat BookMark Acrobat Classic Acrobat DC Acrobat Dialog Acrobat Distiller Acrobat Form Acrobat JS Acrobat Manifest Acrobat Menu Acrobat Open Acrobat Plugin Acrobat Preferences Acrobat Preflight Acrobat python Acrobat Reader Acrobat SCA Acrobat SCA Updater Acrobat Sequ Acrobat Sign Acrobat Stamps Acrobat Watermark Acrobat Windows Acrobat Windows Reader Admin Admin Account Admin Apachectl Admin configCode Admin Device Management Admin LaunchServices Admin Locationd Admin loginitem Admin Maintenance Admin Mobileconfig Admin Permission Admin Pkg Admin Power Management Admin Printer Admin SetUp Admin SMB Admin Support Admin System Information Admin Tools Admin Users Admin Volumes Adobe Adobe FDKO Adobe RemoteUpdateManager AppKit Apple AppleScript AppleScript do shell script AppleScript List AppleScript ObjC AppleScript Osax AppleScript PDF AppleScript Pictures AppleScript record AppleScript Script Editor AppleScript Script Menu AppleScript Shortcuts AppleScript Shortcuts Events AppleScript System Events AppleScript System Events Plist AppleScript Video Applications AppStore Archive Attributes Automator BackUp Barcode Barcode QR Barcode QR Decode Bash Basic Basic Path Bluetooth BOX Browser Calendar CD/DVD Choose Chrome CIImage CityCode CloudStorage Color com.apple.LaunchServices.OpenWith Console Contacts CotEditor CURL current application Date&Time delimiters Desktop Device Diff Disk Dock DropBox Droplet eMail Encode % Encode Decode Encode UTF8 Error EXIFData ffmpeg File Finder Firefox Folder FolderAction Fonts GIF github Guide HTML HTML Entity Icon Illustrator Image Events Image2PDF ImageOptim iPhone iWork Javascript Jedit Json Label Leading Zero List locationd LRC lsappinfo LSSharedFileList m3u8 Mail MakePDF Map Math Media Media AVAsset Media AVconvert Media AVFoundation Media AVURLAsset Media Movie Media Music Memo Messages Microsoft Microsoft Edge Microsoft Excel Mouse Music NetWork Notes NSArray NSArray Sort NSBezierPath NSBitmapImageRep NSBundle NSCFBoolean NSCharacterSet NSColor NSColorList NSData NSDecimalNumber NSDictionary NSError NSEvent NSFileAttributes NSFileManager NSFileManager enumeratorAtURL NSFont NSFontManager NSGraphicsContext NSImage NSIndex NSKeyedArchiver NSKeyedUnarchiver NSLocale NSMutableArray NSMutableDictionary NSMutableString NSNotFound NSNumber NSOpenPanel NSPasteboard NSpoint NSPredicate NSPrintOperation NSRange NSRect NSRegularExpression NSRunningApplication NSScreen NSSize NSString NSString stringByApplyingTransform NSStringCompareOptions NSTask NSTimeZone NSURL NSURL File NSURLBookmark NSURLComponents NSURLResourceKey NSURLSession NSUserDefaults NSUUID NSView NSWorkspace Numbers OAuth OneDrive PDF PDFAnnotation PDFAnnotationWidget PDFContext PDFDisplayBox PDFDocumentPermissions PDFImageRep PDFKit PDFnUP PDFOutline perl Photoshop PlistBuddy pluginkit postalcode PostScript prefPane Preview Python QuickLook QuickTime ReadMe Regular Expression Reminders ReName Repeat RTF Safari SaveFile ScreenCapture ScreenSaver SF Symbols character id SF Symbols Entity sips Skype Slack Sound Spotlight sqlite SRT StandardAdditions Swift System Settings TCC TemporaryItems Terminal Text Text CSV Text MD Text TSV TextEdit Tools Translate Trash Twitter Typography UI Unit Conversion UTType valueForKeyPath Video VisionKit Visual Studio Code VMware Fusion Wacom webarchive webp Wifi Windows XML XML EPUB XML OPML XML Plist XML RSS XML savedSearch XML SVG XML TTML XML webloc XML XMP YouTube zoom