Last active
April 4, 2024 17:24
-
-
Save hexiyou/41e07e70519acefc43f47aebb00814c1 to your computer and use it in GitHub Desktop.
bash/shell调用ffmpeg转wmv为mp4
This file contains 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
#!/bin/bash | |
#调用ffmpeg,转换wmv视频文件为mp4文件 | |
#资料参考来源:https://superuser.com/questions/73529/how-to-convert-wmv-to-mp4 | |
SCRIPTPATH=$(realpath $0) | |
display_usage() { | |
echo -e "$SCRIPTPATH\n" | |
echo -e "\t转换wmv视频文件为mp4文件。" | |
echo -e "\tNotice1:支持截取某个时间区间片段,即指定ffmpeg的 \`-ss\` 和 \`-to\` 参数;" | |
echo -e "\tNotice2:支持自定义ffmpeg使用的滤镜、编码器等命令参数,如果不指定则使用脚本内置的参数选项;" | |
echo -e "\t\t(ffmpeg默认使用参数:-c:v libx264 -crf 23 -c:a aac -q:a 100)" | |
echo -e "Usage:" | |
echo -e "\twmv2mp4 [-ss start-time -to stop-time] [custom ffmpeg options] Input-WMV-File Output-MP4-File" | |
echo -e "Example:" | |
echo -e "\t最简洁用法:wmv2mp4 input.wmv out.mp4 <注意输入文件名在先,输出文件名在后!>" | |
echo -e "\twmv2mp4 input.wmv out.mp4" | |
echo -e "\twmv2mp4 -ss 00:22 -to 00:50 input.wmv out.mp4" | |
echo -e "\twmv2mp4 -ss 00:01:22 -to 00:02:26 input.wmv out.mp4" | |
echo -e "\twmv2mp4 -c:v libx264 -crf 23 -c:a aac -q:a 100 input.wmv out.mp4" | |
echo -e "\twmv2mp4 -ss 00:01:22 -to 00:02:26 -c:v libx264 -crf 23 -c:a aac -q:a 100 input.wmv out.mp4" | |
} | |
# if less than two arguments supplied, display usage | |
if [ $# -lt 1 ] | |
then | |
display_usage | |
exit 1 | |
fi | |
# check whether user had supplied -h or --help . If yes display usage | |
if [[ ( $* == "--help") || $* == "-h" ]] | |
then | |
display_usage | |
exit 0 | |
fi | |
#检查时间参数格式是否正确 | |
checkTimeFormatter() { | |
if [[ "$1" =~ ^([0-9]{1,2}:)?[0-9]{1,2}:[0-5][0-9](\.[0-9]{3})?$ ]] | |
then | |
return 0 | |
fi | |
return 1 | |
} | |
printMessage() { | |
echo -e "\033[42;33m${1}\033[0m" | |
[ "$2" = 1 ] && { | |
display_usage | |
exit 1 | |
} | |
} | |
mapfile -t options <<<"" | |
mapfile -t timePrefix <<<"" | |
#Ffmpeg缺省情况下默认使用的命令行参数,资料来源:https://superuser.com/questions/73529/how-to-convert-wmv-to-mp4 | |
optionsPrepare="-c:v libx264 -crf 23 -c:a aac -q:a 100" | |
inputFile="" | |
outputFile="" | |
while (($#)) | |
do | |
case "$1" in | |
"-ss"|"-to") | |
checkTimeFormatter "$2" | |
if [ $? -eq 0 ] | |
then | |
timePrefix+=("$1") | |
timePrefix+=("$2") | |
shift 2 | |
else | |
if [ "$1" = "-ss" ] | |
then | |
timeDesc="起始时间" | |
else | |
timeDesc="终止时间" | |
fi | |
printMessage "${1}:${timeDesc}格式错误!" 1 | |
fi | |
;; | |
*) | |
if [ $# -eq 2 ] | |
then | |
[ ! -f "$1" ] && printMessage "要转换的源文件 “${1}” 不存在!" 1 | |
inputFile="$1" | |
elif [ $# -eq 1 ] | |
then | |
outputFile="$1" | |
else | |
options+=("$1") | |
fi | |
shift | |
;; | |
esac | |
done | |
[ ${#options[@]} -lt 2 ] && options=("$optionsPrepare") | |
#执行格式转换操作 | |
PATH="/v/mediadeps/ffmpeg/bin:/v/mediadeps/rtmpdump:$PATH" | |
echo ffmpeg ${timePrefix[@]} -i "$inputFile" ${options[@]} "$outputFile" | |
ffmpeg ${timePrefix[@]} -i "$inputFile" ${options[@]} "$outputFile" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
逆向操作:mp4转为wmv视频文件(为方便FastStone对视频文件进行二次编辑);