zoukankan      html  css  js  c++  java
  • Linux Bash/Shell调用ffmpeg转换wmv视频文件为mp4文件,Cygwin测试可用

    首先确保你的电脑系统(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
    "
        echo -e "	转换wmv视频文件为mp4文件。"
    	echo -e "	Notice1:支持截取某个时间区间片段,即指定ffmpeg的 \`-ss\` 和 \`-to\` 参数;"
    	echo -e "	Notice2:支持自定义ffmpeg使用的滤镜、编码器等命令参数,如果不指定则使用脚本内置的参数选项;"
    	echo -e "		(ffmpeg默认使用参数:-c:v libx264 -crf 23 -c:a aac -q:a 100)"
    	echo -e "Usage:"
        echo -e "	wmv2mp4 [-ss start-time -to stop-time] [custom ffmpeg options] Input-WMV-File Output-MP4-File"
    	echo -e "Example:"
    	echo -e "	最简洁用法:wmv2mp4 input.wmv out.mp4 <注意输入文件名在先,输出文件名在后!>"
    	echo -e "	wmv2mp4 input.wmv out.mp4"
    	echo -e "	wmv2mp4 -ss 00:22 -to 00:50 input.wmv out.mp4"
        echo -e "	wmv2mp4 -ss 00:01:22 -to 00:02:26 input.wmv out.mp4"
    	echo -e "	wmv2mp4 -c:v libx264 -crf 23 -c:a aac -q:a 100 input.wmv out.mp4"
    	echo -e "	wmv2mp4 -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 "33[42;33m${1}33[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"
    

    本文来自博客园,作者:晴云孤魂,转载请注明原文链接:https://www.cnblogs.com/cnhack/p/15423129.html

  • 相关阅读:
    20201112 装饰器之函数即变量
    2月13日:毕业设计进度
    2月12日:毕业设计进度
    2月11日:毕业设计进度
    2月10日:毕业设计进度
    2月9日:毕业设计进度
    2月8日:毕业设计进度
    2月7日:毕业设计进度
    2月6日:毕业设计进度
    2月5日:毕业设计进度
  • 原文地址:https://www.cnblogs.com/cnhack/p/15423129.html
Copyright © 2011-2022 走看看