什么是ffmpeg

1.1 简介

FFmpeg是一个开源免费跨平台的视频和音频流方案,属于自由软件,采用LGPL或GPL许可证(依据你选择的组件)。它提供了录制、转换以及流化音视频的完整解决方案。它包含了非常先进的音频/视频编解码库libavcodec,为了保证高可移植性和编解码质量,libavcodec里很多codec都是从头开发的。

FFmpeg提供了强大的命令行工具,非常方便用户使用以及二次开发。

官方网站:http://ffmpeg.org/

1.2 组件

FFmpeg项目由以下几部分组成:

1.FFmpeg视频文件转换命令行工具,也支持经过实时电视卡抓取和编码成视频文件;

2.ffserver基于HTTP、RTSP用于实时广播的多媒体服务器.也支持时间平移;

3.ffplay用 SDL和FFmpeg库开发的一个简单的媒体播放器;

4.libavcodec一个包含了所有FFmpeg音视频编解码器的库。为了保证最优性能和高可复用性,大多数编解码器从头开发的;

5.libavformat一个包含了所有的普通音视格式的解析器和产生器的库。

安装

下载免编译版本:xxxx-static.tar.gz 版本中包含static是免编译版本,

免编译版本下载地址:https://johnvansickle.com/ffmpeg/

安装步骤如下:

 $ cd /home/john
$ wget https://johnvansickle.com/ffmpeg/builds/ffmpeg-git-64bit-static.tar.xz
$ wget https://johnvansickle.com/ffmpeg/builds/ffmpeg-git-64bit-static.tar.xz.md5
$ md5sum -c ffmpeg-git-64bit-static.tar.xz.md5
ffmpeg-git-64bit-static.tar.xz: OK
$ xz -d ffmpeg-git-64bit-static.tar.xz
$ tar -cvf ffmpeg-git-64bit-static.tar
$ ls ffmpeg-git--64bit-static
ffmpeg ffmpeg-10bit ffprobe GPLv3.txt manpages model qt-faststart readme.txt
$ pwd
/home/john $ ./ffmpeg-git--64bit-static/ffmpeg
ffmpeg version N--ge3d946b3f4-static https://johnvansickle.com/ffmpeg/ Copyright (c) 2000-2018 the FFmpeg developers
built with gcc 6.4. (Debian 6.4.-)
(snipped output to save space)

常用的集合选项

–y表示覆盖输出文件;

–i表示输入文件;
-t duration 设置纪录时间 hh:mm:ss[.xxx]格式的记录时间也支持
-ss position 搜索到指定的时间 [-]hh:mm:ss[.xxx]的格式也支持

其他更加详细以及高级用法参考

https://wuyuans.com/2011/11/ffmpeg-syntax

剪切视频

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

批量处理视频

由于视频很大,我想切割成一个一个小的视频文件保存起来

脚本如下

 #!/usr/local/src/Python-2.7./python
import string import os
import time
import re
import math
import sys
from optparse import OptionParser print "Test by zzz 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="")
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="/usr/local/src/test/videoin")
parser.add_option("-t", "--target", dest="targetD",action="store",help="target dir",default="/usr/local/src/test/videoout")
parser.add_option("-w", "--workdir", dest="workdir",action="store",help="work dir",default="/usr/local/src/test/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="")#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[:]=='-':
print 'srcD Err, quit'
exit()
if options.targetD==None or options.targetD[:]=='-':
print 'targetD Err, quit'
exit()
if options.fileType==None or options.fileType[:]=='-':
print 'fileType Err, quit'
exit()
if options.workdir==None or options.workdir[:]=='-':
print 'workdir Err, quit'
exit() #获取视频文件的名字
for root,dirs,files in os.walk(options.srcD):
for name in files:
name= name.replace('[','''\[''')#??????[????
newname =name[: name.rindex('.')]
print "Test newname: " + newname
print "Test name: " + name #??
cmd ='cd '+options.workdir+';mkdir -p ffm; rm -f ffm/ffm.txt ; sh -c "(/usr/local/src/ffmpeg-git-20181015-64bit-static/ffmpeg -i '+options.srcD+'/' +name+ ' >& ffm/ffm.txt)"; grep Duration ffm/ffm.txt'
#ffmpeg -i xxx.mp4 能够得到视频文件的时间信息
print cmd
(si, so, se) = os.popen3(cmd)
t=so.readlines() ##[' Duration: 00:26:52.92, start: 0.000000, bitrate: 248 kb/s\n']
print t
reg='''Duration\:\s(\d+)\:(\d+)\:([\d\.]+)'''
duration=#second
for line in t:
result = re.compile(reg).findall(line)##result=[('', '', '52.92')]
for c in result:
print 'split file to',options.splitsize,'minutes, Duration:',c[],c[],c[]
duration = int(c[])* + int(c[])*+float(c[])##获取视频全部时间s
nameLength=int(math.log(int(duration / (int(options.splitsize)*)) )/math.log()) +
print nameLength
for i in range(int(duration / (int(options.splitsize)*)) + ):##获取总计能够截取几份
print i
_t = ''
if duration>int(options.splitsize)**(i+): ##当总时间数大于截取时间数时的取值
_t = str(int(options.splitsize)*)
else:
_t = str(duration-int(options.splitsize)**i)
##当总时间数小于截取时间数时的取值也就是最后一段视频
cmd ='sh -c "' + "cd "+options.workdir+";touch ffm/output.log;(/usr/local/src/ffmpeg-git-20181015-64bit-static/ffmpeg -y -i "+options.srcD+"/"+name+" -codec: copy -ss "+str(i*int(options.splitsize)*)+" -t "+_t+" "+options.targetD+"/"+options.prefix+newname+'_'+string.replace(('%'+str(nameLength)+'s')%str(i),' ','')+"."+options.fileType + ' >& ffm/output.log)"'
print cmd
(si, so, se) = os.popen3(cmd)
for line in se.readlines() :#????
print line

最后截取视频如下,每个视频两分钟:

[root@localhost videoout]# ls -l
total
-rw-r--r--. root root Oct : 02_xxx.ev4_00.mp4
-rw-r--r--. root root Oct : 02_xxx.ev4_01.mp4
-rw-r--r--. root root Oct : 02_xxx.ev4_02.mp4
-rw-r--r--. root root Oct : 02_xxx.ev4_03.mp4
-rw-r--r--. root root Oct : 02_xxx.ev4_04.mp4
-rw-r--r--. root root Oct : 02_xxx.ev4_05.mp4
-rw-r--r--. root root Oct : 02_xxx.ev4_06.mp4
-rw-r--r--. root root Oct : 02_xxx.ev4_07.mp4
-rw-r--r--. root root Oct : 02_xxx.ev4_08.mp4
-rw-r--r--. root root Oct : 02_xxx.ev4_09.mp4
-rw-r--r--. root root Oct : 02_xxx.ev4_10.mp4
-rw-r--r--. root root Oct : 02_xxx.ev4_11.mp4
-rw-r--r--. root root Oct : 02_xxx.ev4_12.mp4
-rw-r--r--. root root Oct : 02_xxx.ev4_13.mp4

常见的一些问题:

-ss 放在-i前后的问题

https://wuyuans.com/2012/04/ffmpeg-split

一些其他用法:

参考:http://blog.51cto.com/yuanhuan/1246370

https://www.jianshu.com/p/2975f4efd808

补充,由于py2.7以及py3以上popen已经废弃,使用subprocess来替代,这里改写了下脚本

 #!/usr/local/src/Python-2.7./python
import string import os
import time
import re
import math
import sys
from optparse import OptionParser
import subprocess
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="")
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="/usr/local/src/test/videoin")
parser.add_option("-t", "--target", dest="targetD",action="store",help="target dir",default="/usr/local/src/test/videoout")
parser.add_option("-w", "--workdir", dest="workdir",action="store",help="work dir",default="/usr/local/src/test/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="")#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[:]=='-':
print 'srcD Err, quit'
exit()
if options.targetD==None or options.targetD[:]=='-':
print 'targetD Err, quit'
exit()
if options.fileType==None or options.fileType[:]=='-':
print 'fileType Err, quit'
exit()
if options.workdir==None or options.workdir[:]=='-':
print 'workdir Err, quit'
exit() #??videoin????
for root,dirs,files in os.walk(options.srcD):
for name in files:
name= name.replace('[','''\[''')#??????[????
newname =name[: name.rindex('.')]
print "Test newname: " + newname
print "Test name: " + name
os.chdir(options.workdir)
cmd ='mkdir -p ffm;rm -f ffm/ffm.txt;/usr/local/src/ffmpeg-git-20181015-64bit-static/ffmpeg -i '+options.srcD+'/' +name+ ' >& ffm/ffm.txt;pwd'
cmd1=['grep','Duration','ffm/ffm.txt']
for i in cmd.split(';'):
print i
time.sleep()
t1=subprocess.Popen(i,shell=True)
obj=subprocess.Popen(cmd1, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(s1,s2)=obj.communicate()
print s1
reg='''Duration\:\s(\d+)\:(\d+)\:([\d\.]+)'''
result = re.compile(reg).findall(s1)
print result
for c in result:
print 'split file to',options.splitsize,'minutes, Duration:',c[],c[],c[]
duration = int(c[])* + int(c[])*+float(c[])##????????s
nameLength=int(math.log(int(duration / (int(options.splitsize)*)) )/math.log()) +
print nameLength
for i in range(int(duration / (int(options.splitsize)*)) + ):##??????????
print i
if duration>int(options.splitsize)**(i+):
_t = str(int(options.splitsize)*)
else:
_t = str(duration-int(options.splitsize)**i)
os.chdir(options.workdir)
cmd = "touch ffm/output.log;/usr/local/src/ffmpeg-git-20181015-64bit-static/ffmpeg -y -i "+options.srcD+"/"+name+" -codec: copy -ss "+str(i*int(options.splitsize)*)+" -t "+_t+" "+options.targetD+"/"+options.prefix+newname+'_'+string.replace(('%'+str(nameLength)+'s')%str(i),' ','')+"."+options.fileType + ' >& ffm/output.log'
for i in cmd.split(';'):
print i
time.sleep()
t1=subprocess.Popen(i,shell=True)
print t1 time.sleep()

python+ffmpeg切割视频的更多相关文章

  1. 利用FFmpeg切割视频

    关键词:FFmpeg,seek,ss,t,to,搜索,定位 介绍 如果你想要从输入文件中切割一部分,需要用到ss选项. 快速定位 需要将ss放在输入文件的前面(即-i的前面) elesos1.jpg ...

  2. ffmpeg切割视频

    using System.Diagnostics; public static void carveVideo() { var inputpath = @"d:\1.mp4"; v ...

  3. 20190728-Python爬取视频&切割视频&视频加水印

    1.视频爬取 1.下载视频的源码如下: import os import requests from bs4 import BeautifulSoup import threading from bj ...

  4. NET 2.0(C#)调用ffmpeg处理视频的方法

    另外:ffmpeg的net封装库 http://www.intuitive.sk/fflib/ NET 2.0 调用FFMPEG,并异步读取输出信息的代码...public void ConvertV ...

  5. 神工鬼斧惟肖惟妙,M1 mac系统深度学习框架Pytorch的二次元动漫动画风格迁移滤镜AnimeGANv2+Ffmpeg(图片+视频)快速实践

    原文转载自「刘悦的技术博客」https://v3u.cn/a_id_201 前段时间,业界鼎鼎有名的动漫风格转化滤镜库AnimeGAN发布了最新的v2版本,一时间街谈巷议,风头无两.提起二次元,目前国 ...

  6. ffmpeg为视频添加时间戳 - 手动编译ffmpeg

    FFMPEG给视频加时间戳水印 项目中需要给视频添加时间戳,理所当然最好用的办法是ffmpeg.在找到正确的做法前,还被网上的答案timecode给水了一下(水的不轻,在这里转了2天),大概是这样写的 ...

  7. 利用FFmpeg生成视频缩略图 2.1.6

    利用FFmpeg生成视频缩略图 1.下载FFmpeg文件包,解压包里的\bin\下的文件解压到 D:\ffmpeg\ 目录下. 下载地址 http://ffmpeg.zeranoe.com/build ...

  8. C# 利用ffmpeg 对视频转换系类操作 (1) 基本分析

    最近公司做一个项目,开发一个视频站点.项目需求中有很多视频转换的需求,如:格式转换(flv,Mp4),视频水印,视频截图,视频合成,获取视频的基本信息(时间戳,视频大小等).经过网络的收集资料以及自己 ...

  9. 使用ffmpeg 对视频截图,和视频转换格式

    //执行CMD命令方法 public static void CmdProcess(string command)//调用CMD        {            //实例化一个进程类      ...

随机推荐

  1. Springboot 系列(八)动态Banner与图片转字符图案的手动实现

    使用过 Springboot 的对上面这个图案肯定不会陌生,Springboot 启动的同时会打印上面的图案,并带有版本号.查看官方文档可以找到关于 banner 的描述 The banner tha ...

  2. 【开源分享】微信营销系统(第三方微信平台)github 开源

    升讯威微信营销系统(微信第三方平台) 在线体验:http://wxcm.eeipo.cn/开源地址GitHub:https://github.com/iccb1013/Sheng.WeixinCons ...

  3. Ubuntu 16.04 nvidia-smi报错(重装Nvidia驱动)

    之前因为学习TensorFlow,所以在自己的Ubuntu上安装了cuda,cudnn以及Nvidia驱动.但可能是由于自己经常不注重正常关闭自己的Ubuntu,这就导致了一个问题: 某天在查看自己的 ...

  4. keepalived介绍

    keepalived介绍 Keepalived是一个基于VRRP协议来实现的服务高可用方案,可以利用其来避免IP单点故障,类似的工具还有heartbeat.corosync.pacemaker.但是它 ...

  5. 吐血bug-- 多个input框接连blur事件导致alert接连弹出

    本来今天想记录一下Flow在vue源码中的应用,结果临时触发了个bug... bug描述: elementUi + Vue 技术 需求:一个表格中有至少两条数据,每条数据都有input框,在失去焦点后 ...

  6. 自动化测试 Appium之Python运行环境搭建 Part1

    Appium之Python运行环境搭建 Part1 by:授客 QQ:1033553122 实践环境 Win7 Python 3.4.0 JAVA JDK 1.8.0_121 node.js8.11. ...

  7. Netty中ByteBuf的引用计数线程安全的实现原理

    原文链接 Netty中ByteBuf的引用计数线程安全的实现原理 代码仓库地址 ByteBuf 实现了ReferenceCounted 接口,实现了引用计数接口,该接口的retain(int) 方法为 ...

  8. RESTful学习及应用

    原文转自前端路上,转载请注明出处:http://refined-x.com/2017/09/22/RESTful学习及应用/ RESTful是什么 RESTful是一种API架构,符合REST设计原则 ...

  9. eclipse中使用Lombok(转)

    原文链接:https://www.cnblogs.com/justuntil/p/7120534.html windows环境 1.下载lombok.jar包https://projectlombok ...

  10. 初窥css---选择器及相关特性

    选择器及相关特性 基础选择器 标签选择器 相当于全选,在我看来局限性较大,也没啥意义的感觉,用处不太大 id选择器 有利于对于某个小盒子的部分属性进行改变,但是若是需要改的小盒子很多的话,就会很麻烦 ...