Last active
October 21, 2024 10:16
-
-
Save wangjiezhe/576a3820b2519d8684aab39859340cb9 to your computer and use it in GitHub Desktop.
Convert video to 25M for wechat
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
command -v mediainfo >/dev/null 2>&1 || { echo >&2 "mediainfo 命令不存在,请先安装 mediainfo。"; exit 1; } | |
command -v ffmpeg >/dev/null 2>&1 || { echo >&2 "ffmpeg 命令不存在,请先安装 ffmpeg。"; exit 1; } | |
out_size=24 | |
length=$(mediainfo --Inform="Video;%Duration%" "$1") | |
bitrate_all=$(echo "${out_size}*8192/(${length}/1000)" | bc) | |
bitrate_audio_orig=$(mediainfo --Inform="Audio;%BitRate%" "$1") | |
if [ -z ${bitrate_audio_orig} ]; then | |
bitrate_audio_orig=$(mediainfo --Inform="Audio;%BitRate_Nominal%" "$1") | |
fi | |
if [ ${bitrate_audio_orig} -gt 150000 ]; then | |
convert_audio=true | |
bitrate_audio=128 | |
else | |
bitrate_audio=$(echo "${bitrate_audio_orig}/1000" | bc) | |
fi | |
if [ ${bitrate_all} -lt 250 ]; then | |
convert_audio=true | |
bitrate_audio=48 | |
fi | |
bitrate_video=$(echo "${bitrate_all}-${bitrate_audio}" | bc) | |
in_file="$1" | |
out_file=$(echo ${in_file} | sed -e 's/.mp4/_25M.mp4/') | |
ffmpeg -y -i ${in_file} -c:v libx265 -b:v ${bitrate_video}k -x265-params pass=1 -an -f mp4 /dev/null | |
if [ ${convert_audio} = true ]; then | |
ffmpeg -i "$1" -c:v libx265 -b:v ${bitrate_video}k -x265-params pass=2 -c:a aac -b:a ${bitrate_audio}k ${out_file} | |
else | |
ffmpeg -i "$1" -c:v libx265 -b:v ${bitrate_video}k -x265-params pass=2 -c:a copy ${out_file} | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment