Skip to content

Instantly share code, notes, and snippets.

@zqhong
Last active June 24, 2021 05:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zqhong/fe8f55bf85e18263306cd1379980c0a8 to your computer and use it in GitHub Desktop.
Save zqhong/fe8f55bf85e18263306cd1379980c0a8 to your computer and use it in GitHub Desktop.
ffmpeg 批量对 find 匹配后的视频文件进行处理
#!/bin/bash
: '
用法:
find-exec-ffmpeg path file_ext
例子:
find-exec-ffmpeg /home/ mp4:【转换 /home 目录下的 mp4 视频文件】
说明:
脚本用于将某目录下符合条件的视频文件,转换为 H.264 视频编码,AAC 音频编码。期间,尝试使用 Videotoolbox(macOS) 视频加速。
转换成功后的文件,文件结尾添加【_batch】。示例:源文件 1.mp4,转换后的文件则为 1_batch.mp4。
脚本依赖 FFmpeg,使用前请下载安装:https://ffmpeg.org/download.html
(建议使用编译成功的二进制文件,手动编译较为耗时。)
注意:
为了避免重复处理,脚本默认忽略处理【_batch.ext(如:_batch.mp4)】的文件。
'
ffmpeg_file() {
local loop_file
loop_file=$1
local new_file
new_file="${loop_file/.$search_file_ext/$append_filename.$search_file_ext}"
echo "ffmpeg -in $loop_file -out $new_file..."
ffmpeg -y -nostdin -hide_banner -loglevel error -i "$loop_file" -allow_sw 1 -vcodec h264_videotoolbox -preset veryfast -acodec aac "$new_file"
}
search_path="."
search_file_ext="mp4"
append_filename="_batch"
if test -n "$1"; then
search_path=$1
fi
if test -n "$2"; then
search_file_ext=$2
fi
find "$search_path" ! -name "*$append_filename.$search_file_ext" -name "*.$search_file_ext" | while read -r file; do ffmpeg_file "$file"; done
@zqhong
Copy link
Author

zqhong commented Jun 23, 2021

安装:
打开终端.app ,分别复制、粘贴和运行下面两行命令。建议运行下了解下命令的内容。

curl "https://gist.githubusercontent.com/zqhong/fe8f55bf85e18263306cd1379980c0a8/raw/a6e99b2b4ac02ace56584868d2b58d6b199becc8/find-exec-ffmpeg.sh" -o /usr/local/bin/find-exec-ffmpeg && chmod +x /usr/local/bin/find-exec-ffmpeg

curl "https://evermeet.cx/ffmpeg/ffmpeg-102787-gec8e95296e.zip" -o ffmpeg.zip && unzip -o ffmpeg.zip -d /usr/local/bin && rm ffmpeg.zip && chmod +x /usr/local/bin/ffmpeg

其中,第一条命令作用:
下载 find-exec-ffmpeg.sh ,并将该脚本放到 /usr/local/bin/find-exec-ffmpeg

第二条命令作用:
下载 macOS FFmpeg 二进制文件,并将 ffmpeg 放到 /usr/local/bin/ffmpeg

使用 Tonyliu2ca 朋友的建议,使用 curl 替代 wget。

使用:
find-exec-ffmpeg . mp4

转换当前目录下的 mp4 文件。

@Tonyliu2ca
Copy link

As "wget" doesn't come with any macOS system, to make the installation process the most commonly available, how about using "curl" to download files? like below:
curl "https://gist.githubusercontent.com/zqhong/fe8f55bf85e18263306cd1379980c0a8/raw/a6e99b2b4ac02ace56584868d2b58d6b199becc8/find-exec-ffmpeg.sh" -o /usr/local/bin/find-exec-ffmpeg && chmod +x /usr/local/bin/find-exec-ffmpeg
curl "https://evermeet.cx/ffmpeg/ffmpeg-102787-gec8e95296e.zip" -o ffmpeg.zip && unzip -o ffmpeg.zip -d /usr/local/bin && rm ffmpeg.zip && chmod +x /usr/local/bin/ffmpeg

@zqhong
Copy link
Author

zqhong commented Jun 24, 2021

As "wget" doesn't come with any macOS system, to make the installation process the most commonly available, how about using "curl" to download files? like below:
curl "https://gist.githubusercontent.com/zqhong/fe8f55bf85e18263306cd1379980c0a8/raw/a6e99b2b4ac02ace56584868d2b58d6b199becc8/find-exec-ffmpeg.sh" -o /usr/local/bin/find-exec-ffmpeg && chmod +x /usr/local/bin/find-exec-ffmpeg
curl "https://evermeet.cx/ffmpeg/ffmpeg-102787-gec8e95296e.zip" -o ffmpeg.zip && unzip -o ffmpeg.zip -d /usr/local/bin && rm ffmpeg.zip && chmod +x /usr/local/bin/ffmpeg

Yes, thank you for reminding me.
是的,谢谢你的提醒。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment