macOS上MP4转WAV和M4A转WAV文件

目录

====提出程序要求====

====安装Homebrew和ffmpeg的步骤====

====方法====

====原代码以下====

====原代码以上====

====注意====


====提出程序要求====

在macOS上用Script Editor编写一个程序,使用ffmpeg命令和afconvert工具来把mp4文件或者m4a文件转换成wav文件。虽然afconvert是macOS自带的工具,但是ffmpeg不是,所以需要使用Homebrew手动安装ffmpeg。

====安装Homebrew和ffmpeg的步骤====

打开终端(Terminal)应用。你可以在Spotlight搜索中输入"Terminal"来找到它。
$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"    //安装Homebrew
$ brew install ffmpeg
$ ffmpeg -version            //来检查ffmpeg是否已经成功安装。

====方法====

你可以按照以下步骤来创建这个应用程序:
* 打开AppleScript编辑器(Script Editor)。你可以在Spotlight搜索中输入"Script Editor"来找到它。
* 将上面的AppleScript代码复制并粘贴到编辑器中。
* 选择"File" > "Export...",然后在弹出的对话框中选择"File Format"为"Application"。
* 输入你的应用程序的名称,然后选择一个保存的位置,点击"Save"。
现在,你就可以运行这个应用程序来转换音频文件了。当你运行这个应用程序时,它会首先让你选择一个mp4或m4a文件,然后让你选择一个输出的wav文件的位置。然后,它会使用ffmpeg或afconvert命令来转换音频文件。
请注意,这个应用程序假设你的系统已经安装了ffmpeg和afconvert工具,并且这些工具可以在你的PATH环境变量中找到。如果你的系统没有安装这些工具,或者这些工具不在你的PATH环境变量中,你可能需要修改这个脚本来指定这些工具的完整路径。

====原代码以下====

 选择输入文件
set inputFile to choose file with prompt "请选择要转换的文件:"

-- 选择输出文件
set outputFile to choose file name with prompt "请选择输出的wav文件的位置:"

-- 获取文件的路径
set inputFilePath to POSIX path of inputFile

-- 获取输出文件的路径,并确保它有正确的扩展名
set outputFilePath to POSIX path of outputFile
if outputFilePath does not end with ".wav" then
    set outputFilePath to outputFilePath & ".wav"
end if

-- 获取输入文件的信息
set fileInfo to info for inputFile

-- 检查输入文件的扩展名
set fileExtension to name extension of fileInfo

-- 根据输入文件的类型选择合适的转换工具
if fileExtension is in {"mp4", "MP4"} then
    -- 使用ffmpeg提取mp4文件的音频
    set convertCommand to "/usr/local/bin/ffmpeg -i " & quoted form of inputFilePath & " -vn -acodec pcm_s16le -ar 44100 -ac 2 " & quoted form of outputFilePath
else if fileExtension is in {"m4a", "M4A"} then
    -- 使用afconvert转换m4a文件
    set convertCommand to "/usr/bin/afconvert -f WAVE -d LEI16@44100 -c 2 " & quoted form of inputFilePath & " " & quoted form of outputFilePath
else
    display dialog "Unsupported file type: " & fileExtension
    return
end if

-- 执行命令
do shell script convertCommand

====原代码以上====

====注意====

在这个脚本中,我假设ffmpeg的完整路径是/usr/local/bin/ffmpeg,afconvert的完整路径是/usr/bin/afconvert。你可能需要根据你的系统中这两个工具的实际路径来修改这些路径。

猜你喜欢

转载自blog.csdn.net/cgxcgxcgxcgx/article/details/131756525