ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

Linux Bash/Shell调用ffmpeg转换wmv视频文件为mp4文件,Cygwin测试可用

2021-10-19 03:00:24  阅读:260  来源: 互联网

标签:Shell ffmpeg wmv echo usage mp4 视频文件 mwv


首先确保你的电脑系统(PC或Linux服务器均可,Windows下用Cygwin测试通过)正确安装和配置ffmpeg
ffmpeg默认使用的参数为:-c:v libx264 -crf 23 -c:a aac -q:a 100
简单用法:wmv2mp4 1.wmv 2.mp4
参考资料:https://superuser.com/questions/73529/how-to-convert-wmv-to-mp4

#!/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.mwv out.mp4 <注意输入文件名在先,输出文件名在后!>"
	echo -e "\twmv2mp4 input.mwv out.mp4"
	echo -e "\twmv2mp4 -ss 00:22 -to 00:50 input.mwv out.mp4"
    echo -e "\twmv2mp4 -ss 00:01:22 -to 00:02:26 input.mwv out.mp4"
	echo -e "\twmv2mp4 -c:v libx264 -crf 23 -c:a aac -q:a 100 input.mwv 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.mwv 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"

标签:Shell,ffmpeg,wmv,echo,usage,mp4,视频文件,mwv
来源: https://www.cnblogs.com/cnhack/p/15423129.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有