美文网首页视频音频
用ffmpeg命令处理mp4剪切与合并

用ffmpeg命令处理mp4剪切与合并

作者: 南风无影 | 来源:发表于2016-11-22 09:35 被阅读373次

    1. 剪切:

      ./ffmpeg -ss 00:00:06 -t 00:00:12 -i input.mp4 -vcodec copy -acodec copy output.mp4
    

    意思是从截取从6秒开始,时长为12秒的视频,格式不变,
    输入为input.mp4
    输出为output.mp4
    -vcodec copy -acodec copy : 编码格式不变

    2. 合并:

    mp4的合并最好是先转成无损质量的ts,再合并(如果直接合并,会有些小bug)

      ./ffmpeg -i 1.mp4 -vcodec copy -acodec copy -vbsf h264_mp4toannexb 1.ts
      ./ffmpeg -i 2.mp4 -vcodec copy -acodec copy -vbsf h264_mp4toannexb 2.ts
      ./ffmpeg -i "concat:1.ts|2.ts" -acodec copy -vcodec copy -absf aac_adtstoasc output.mp4
    

    还有就是关于-s选项的解释:

    ‘-ss position (input/output)’
    When used as an input option (before -i), seeks in this input file to position. When used as an output option (before an output filename), decodes but discards input until the timestamps reach position. This is slower, but more accurate.
    position may be either in seconds or in hh:mm:ss[.xxx] form.
    意思就是如果要把-ss作为输入选项的话要放在-i之前,当做输出选项的话放在输出文件之前。我们这是要截取一段视频,应该当做输入选项,所以-ss要在-i之前才有效,不然会花费很长一段时间来寻找-ss。

    3. 批处理(python脚本实现)

    主要是实现用自动化工具,将视频切割成比如2分钟的很多小视频文件;
    事情的由来是我看到一个youtube的视频不错,因为墙的原因不能分享,但是原文件比较大(有200多M)不能发微信(限定是20M以内),所以造了这个轮子;

    1.png
    # coding=gb2312
    import string
    import os 
    import time
    import re
    import math
    import sys
    from optparse import OptionParser
    
    print "Test by gongjia start..."
    
    
    parser = OptionParser()
    parser.add_option("-i", "--input", dest="input",action="store_true",help="input x y for each file by user")
    parser.add_option("-q", "--quality", dest="q",action="store",help="input xvid q arg",default="24")
    parser.add_option("-v", "--vcodec", dest="vcodec",action="store",help="input video codec",default="x264")
    parser.add_option("-n", "--noaudio", dest="an",action="store_true",help="no audio")
    parser.add_option("-p", "--preset", dest="preset",action="store",help="",default="")
    parser.add_option("-m", "--maxWidth", dest="maxWidth",action="store",help="input max width for output video",default="")
    parser.add_option("-f", "--fileType", dest="fileType",action="store",help="",default="mp4")
    parser.add_option("-o", "--ogg", dest="ogg",action="store_true",help="user ogg instead of aac",default="")
    parser.add_option("-3", "--mp3", dest="mp3",action="store_true",help="user mp3 instead of aac",default="")
    parser.add_option("-1", "--pad", dest="pad",action="store_true",help="pad to 16:9",default="")
    parser.add_option("-s", "--src", dest="srcD",action="store",help="source dir",default="/home/gongjia/external/ffmpeg/ffmpeg-x86/videoin")
    parser.add_option("-t", "--target", dest="targetD",action="store",help="target dir",default="/home/gongjia/external/ffmpeg/ffmpeg-x86/videoout")
    parser.add_option("-w", "--workdir", dest="workdir",action="store",help="work dir",default="/home/gongjia/external/ffmpeg/ffmpeg-x86/video")
    parser.add_option("-e", "--split", dest="split",action="store_true",help="split to multiple file with size")
    parser.add_option("-d", "--splitsize", dest="splitsize",action="store",help="split to multiple file with size",default="2")#Minutes
    parser.add_option("-j", "--prefix", dest="prefix",action="store",help="target file name prefix",default="")
    
    (options, args) = parser.parse_args()
    
    if options.srcD==None or options.srcD[0:1]=='-':
        print 'srcD Err, quit'
        exit() 
    if options.targetD==None or options.targetD[0:1]=='-':
        print 'targetD Err, quit'
        exit() 
    if options.fileType==None or options.fileType[0:1]=='-':
        print 'fileType Err, quit'
        exit() 
    if options.workdir==None or options.workdir[0:1]=='-':
        print 'workdir Err, quit'
        exit() 
        
    #遍历videoin下的文件
    for root,dirs,files in os.walk(options.srcD): 
        for name in files:
            name= name.replace('[','''\[''')#对文件名中的[进行转义
            newname =name[0: name.rindex('.')]      
            print "Test newname: " + newname
            print "Test name: " + name
    
            
            
    #运行
            cmd ='cd '+options.workdir+';mkdir -p ffm;  rm -f ffm/ffm.txt ; csh -c "(ffmpeg -i '+options.srcD+'/' +name+ ' >& ffm/ffm.txt)"; grep Duration ffm/ffm.txt'
            print cmd
            (si, so, se) = os.popen3(cmd)
            t=so.readlines()
            reg='''Duration\:\s(\d+)\:(\d+)\:([\d\.]+)'''
            duration=0#second
            for line in t:
                result = re.compile(reg).findall(line)
                for c in result:
                    print 'split file to',options.splitsize,'minutes, Duration:',c[0],c[1],c[2]
                    duration = int(c[0])*3600 + int(c[1])*60+float(c[2])
                    nameLength=int(math.log(int(duration / (int(options.splitsize)*60)) )/math.log(10)) + 1
                    for i in range(int(duration / (int(options.splitsize)*60)) + 1):
                        print i
                        _t = ''
                        if duration>int(options.splitsize)*60*(i+1):
                            _t = str(int(options.splitsize)*60)
                        else:
                            _t = str(duration-int(options.splitsize)*60*i)
                        cmd ='csh -c "' + "cd "+options.workdir+";touch ffm/output.log;(ffmpeg -y -i "+options.srcD+"/"+name+" -codec: copy -ss "+str(i*int(options.splitsize)*60)+" -t "+_t+" "+options.targetD+"/"+options.prefix+newname+'_'+string.replace(('%'+str(nameLength)+'s')%str(i),' ','0')+"."+options.fileType + ' >>& ffm/output.log)"'
                        print cmd
                        (si, so, se) = os.popen3(cmd)
                        for line in se.readlines() :#打印输出
                            print line
    
        
    

    相关文章

      网友评论

        本文标题:用ffmpeg命令处理mp4剪切与合并

        本文链接:https://www.haomeiwen.com/subject/ynjppttx.html