Media

[afconvert]オーディオファイル変換

[afconvert]オーディオファイル変換.scpt

AppleScript サンプルコード

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

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変換詳細
010https://developer.apple.com/library/archive/documentation/MusicAudio/Conceptual/CoreAudioOverview/SupportedAudioFormatsMacOSX/SupportedAudioFormatsMacOSX.html
011
01220240522 v1 初回作成
01320250213 v2 少し修正
014*)
015#com.cocolog-nifty.quicktimer.icefloe
016----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
017use AppleScript version "2.8"
018use framework "Foundation"
019use framework "AppKit"
020use scripting additions
021
022property refMe : a reference to current application
023
024set appFileManager to refMe's NSFileManager's defaultManager()
025
026#############################
027###ダイアログ ファイル選択
028set strName to (name of current application) as text
029if strName is "osascript" then
030  tell application "Finder" to activate
031else
032  tell current application to activate
033end if
034#デスクトップ
035set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
036set ocidDesktopDirPathURL to ocidURLsArray's firstObject()
037set aliasDefaultLocation to (ocidDesktopDirPathURL's absoluteURL()) as alias
038#ダイアログ設定
039set listUTI to {"public.audio"}
040set strMes to ("サウンドファイルを選んでください") as text
041set strPrompt to ("サウンドファイルを選んでください") as text
042try
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
045on error
046  log "エラーしました"
047  return "エラーしました"
048end try
049if listAliasFilePath is {} then
050  log "選んでください"
051  doResExec()
052end if
053
054#############################
055###フォーマット(変換後)選択
056set 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#キーでリスト
061set ocidExtensionDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
062ocidExtensionDict's setDictionary:(recordExtension)
063set ocidDataKeys to ocidExtensionDict's allKeys()
064#ソートして
065set listDataKeys to (ocidDataKeys's sortedArrayUsingSelector:("localizedStandardCompare:")) as list
066#
067set strTitle to ("選んでください") as text
068set strPrompt to ("変換後のフォーマットを選んでください") as text
069try
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
071on error
072  log "エラーしました"
073  return "エラーしました"
074end try
075if (item 1 of listResponse) is false then
076  return "キャンセルしましたA"
077else if (item 1 of listResponse) is "キャンセル" then
078  return "キャンセルしましたB"
079else
080  set strDataType to (item 1 of listResponse) as text
081end if
082set strExtension to (ocidExtensionDict's objectForKey:(strDataType)) as text
083
084###
085set 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
086set ocidFormatDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
087ocidFormatDict's setDictionary:(recordExtension)
088set listDataFormat to (ocidFormatDict's objectForKey:(strDataType)) as list
089#
090set strTitle to ("選んでください") as text
091set strPrompt to ("変換後のデータ・フォーマットを選んでください") as text
092try
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
094on error
095  log "エラーしました"
096  return "エラーしました"
097end try
098if (item 1 of listResponse) is false then
099  return "キャンセルしましたA"
100else if (item 1 of listResponse) is "キャンセル" then
101  return "キャンセルしましたB"
102else
103  set strDataFormat to (item 1 of listResponse) as text
104end if
105###
106set 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
107set ocidOutPutDataFormat to refMe's NSDictionary's alloc()'s initWithDictionary:(recordOutPutDataFormat)
108set strOutPutDataFormat to (ocidOutPutDataFormat's valueForKey:(strDataType))
109
110repeat 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
143end repeat
144
145set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
146set boolDone to appSharedWorkspace's openURL:(ocidContainerDirPathURL)
147
148
149
150##############################
151#####自分自身を再実行
152##############################
153to 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
160end 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 2024 Acrobat AddOn Acrobat Annotation Acrobat AppleScript Acrobat ARMDC Acrobat AV2 Acrobat BookMark Acrobat Classic Acrobat DC Acrobat Dialog Acrobat Distiller Acrobat Form Acrobat GentechAI Acrobat JS Acrobat JS Word Search Acrobat Maintenance Acrobat Manifest Acrobat Menu Acrobat Merge Acrobat Open Acrobat PDFPage Acrobat Plugin Acrobat Preferences Acrobat Preflight Acrobat Print Acrobat Python Acrobat Reader Acrobat Reader Localized Acrobat Reference Acrobat Registered Products Acrobat SCA Acrobat SCA Updater Acrobat Sequ Acrobat Sign Acrobat Stamps Acrobat URL List Mac Acrobat URL List Windows Acrobat Watermark Acrobat Windows Acrobat Windows Reader Admin Admin Account Admin Apachectl Admin ConfigCode Admin configCode Admin Device Management Admin LaunchServices Admin Locationd Admin loginitem Admin Maintenance Admin Mobileconfig Admin NetWork Admin Permission Admin Pkg Admin Power Management Admin Printer Admin Printer Basic Admin Printer Custompapers Admin SetUp Admin SMB Admin softwareupdate Admin Support Admin System Information Admin TCC Admin Tools Admin Umask Admin Users Admin Volumes Admin XProtect Adobe Adobe AUSST Adobe Bridge Adobe Documents Adobe FDKO Adobe Fonts Adobe Reference Adobe RemoteUpdateManager Adobe Sap Code AppKit Apple AppleScript AppleScript Duplicate AppleScript entire contents AppleScript List AppleScript ObjC AppleScript Osax AppleScript PDF AppleScript Pictures AppleScript record AppleScript Video Applications AppStore Archive Archive Keka Attributes Automator BackUp Barcode Barcode Decode Barcode QR Bash Basic Basic Path Bluetooth BOX Browser Calendar CD/DVD Choose Chrome Chromedriver CIImage CityCode CloudStorage Color Color NSColor Color NSColorList com.apple.LaunchServices.OpenWith Console Contacts CotEditor CURL current application Date&Time delimiters Desktop Desktop Position Device Diff Disk do shell script Dock Dock Launchpad DropBox Droplet eMail Encode % Encode Decode Encode HTML Entity Encode UTF8 Error EXIFData exiftool ffmpeg File File Name Finder Finder Window Firefox Folder FolderAction Font List FontCollections Fonts Fonts Asset_Font Fonts ATS Fonts Emoji Fonts Maintenance Fonts Morisawa Fonts Python Fonts Variable Foxit GIF github Guide HTML Icon Icon Assets.car Illustrator Image Events ImageOptim Input Dictionary iPhone iWork Javascript Jedit Ω Json Label Language Link locationd lsappinfo m3u8 Mail Map Math Media Media AVAsset Media AVconvert Media AVFoundation Media AVURLAsset Media Movie Media Music Memo Messages Microsoft Microsoft Edge Microsoft Excel Microsoft Fonts Microsoft Office Microsoft Office Link Microsoft OneDrive Microsoft Teams Mouse Music Node Notes NSArray NSArray Sort NSAttributedString NSBezierPath NSBitmapImageRep NSBundle NSCFBoolean NSCharacterSet NSData NSDecimalNumber NSDictionary NSError NSEvent NSFileAttributes NSFileManager NSFileManager enumeratorAtURL NSFont NSFontManager NSGraphicsContext NSGraphicsContext Crop NSImage NSIndex NSKeyedArchiver NSKeyedUnarchiver NSLocale NSMetadataItem NSMutableArray NSMutableDictionary NSMutableString NSNotFound NSNumber NSOpenPanel NSPasteboard NSpoint NSPredicate NSRange NSRect NSRegularExpression NSRunningApplication NSScreen NSSet NSSize NSString NSString stringByApplyingTransform NSStringCompareOptions NSTask NSTimeZone NSUbiquitous NSURL NSURL File NSURLBookmark NSURLComponents NSURLResourceKey NSURLSession NSUserDefaults NSUUID NSView NSWorkspace Numbers OAuth PDF PDF Image2PDF PDF MakePDF PDF nUP PDF Pymupdf PDF Pypdf PDFContext PDFDisplayBox PDFImageRep PDFKit PDFKit Annotation PDFKit AnnotationWidget PDFKit DocumentPermissions PDFKit OCR PDFKit Outline PDFKit Start PDFPage PDFPage Rotation PDFView perl Photoshop PlistBuddy pluginkit plutil postalcode PostScript PowerShell prefPane Preview Python Python eyed3 Python pip QuickLook QuickTime ReadMe Regular Expression Reminders ReName Repeat RTF Safari SaveFile ScreenCapture ScreenSaver Script Editor Script Menu SF Symbols character id SF Symbols Entity Shortcuts Shortcuts Events sips Skype Slack Sound Spotlight sqlite StandardAdditions StationSearch Subtitles LRC Subtitles SRT Subtitles VTT Swift swiftDialog System Events System Settings 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 Weather webarchive webp Wifi Windows XML XML EPUB XML HTML XML LSSharedFileList XML LSSharedFileList sfl2 XML LSSharedFileList sfl3 XML objectsForXQuery XML OPML XML Plist XML Plist System Events XML RSS XML savedSearch XML SVG XML TTML XML webloc XML xmllint XML XMP YouTube Zero Padding zoom