什么是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. ITSA(IT Strategy and Architecture)方法介绍

    Architecture Capability – At a Glance Architectural coherence part1 Architectural coherence part2 SA ...

  2. 【Redis】LRU算法和Redis的LRU实现

    LRU原理 在一般标准的操作系统教材里,会用下面的方式来演示 LRU 原理,假设内存只能容纳3个页大小,按照 7 0 1 2 0 3 0 4 的次序访问页.假设内存按照栈的方式来描述访问时间,在上面的 ...

  3. MongoDB学习(配置用户账户和访问控制)

    理解admin数据库 安装MongoDB时,会自动创建admin数据库,这是一个特殊的库.有些用户账户角色赋予用户操作多个数据库的权限,而这些用户只能在admin数据库中创建.要创建有权操作所有数据库 ...

  4. CODING 如何使用 CODING 研发管理系统来敏捷开发

    之前我们分享过<CODING 如何使用 CODING 开发 CODING>的文章,时过境迁,现在 CODING 研发管理系统已经上线了如持续集成.缺陷管理.测试管理等 DevOps 中的重 ...

  5. python的学习笔记01_4基础数据类型列表 元组 字典 集合 其他其他(for,enumerate,range)

    列表 定义:[]内以逗号分隔,按照索引,存放各种数据类型,每个位置代表一个元素 特性: 1.可存放多个值 2.可修改指定索引位置对应的值,可变 3.按照从左到右的顺序定义列表元素,下标从0开始顺序访问 ...

  6. Play vue.js with constant value in SailsJS

    SailsJS supplies a utility module called parasails, which defines two elements, <ajax-form> an ...

  7. Android视频录制从不入门到入门系列教程(三)————视频方向

    运行Android视频录制从不入门到入门系列教程(二)————显示视频图像中的Demo后,我们应该能发现视频的方向是错误的. 由于Android中,Camera给我们的视频图片的原始方向是下图这个样子 ...

  8. error C1189: #error : Building MFC application with /MD[d] (CRT dll version) requires MFC shared dll version. Please #define _AFXDLL or do not use /MD[d]

    今天在开发过程中遇到了C1189 error.找了好久解决办法,最后自己解决了...... 方法:工程右键->属性 编辑预处理器定义: 再次运行,就解决了.

  9. 链表倒数第k个节点

    1.一种较笨的办法是先将链表元素入栈,然后出栈找到倒数第k个节点值,再拿着值遍历链表去找到对于节点. 时间复杂度:O(n) (3n 遍历-出栈-遍历) 空间复杂度:O(n) (一个栈) 2.快慢指针, ...

  10. MyDAL - .UpdateAsync() 之 .Set() 使用

    索引: 目录索引 一.API 列表 1.Set<M, F>(Expression<Func<M, F>> propertyFunc, F newVal) 如: .S ...