ffmpeg简单实战
来源:互联网 发布:mac safari下载速度慢 编辑:程序博客网 时间:2024/06/15 22:37
什么是ffmpeg
ffmpeg is a very fast video and audio converter that can also grab from a live audio/video source. It can also convert between arbitrary sample rates and resize video on the fly with a high quality polyphase filter.
简单的说,ffmpeg乃江湖神器。它能帮你将视频一帧帧录成图片
,能帮你自由转换视频的格式
,什么加字幕加音轨啦,什么录制屏幕啦,都很easy
原理
ffmpeg
命令都是长的吓人,但是基本原理还是简单的
一张图~
基本上就是这样的流程:输入文件==>被分离器分离的数据包==>被解码的数据帧==>被编码的数据包==>输出文件
实战
首先,我们可以给视频加上字幕~
比如说我有一步电影信息如下
cecil@cecil-Aspire-4750 /media/cecil/FreeAgent GoFlex Drive/豆瓣250 ffprobe \[这个男人来自地球\].Man.From.Earth.DVDRip.Xvid.TFE.cht\&eng.mkvavprobe version 0.8.10-6:0.8.10-0ubuntu0.12.10.1, Copyright (c) 2007-2013 the Libav developers built on Feb 6 2014 20:57:40 with gcc 4.7.2[matroska,webm @ 0x9895100] Estimating duration from bitrate, this may be inaccurateInput #0, matroska,webm, from '[这个男人来自地球].Man.From.Earth.DVDRip.Xvid.TFE.cht&eng.mkv': Duration: 01:27:08.12, start: 0.000000, bitrate: 640 kb/s Stream #0.0(eng): Video: h264 (High), yuv420p, 1280x720, PAR 1:1 DAR 16:9, 23.98 fps, 23.98 tbr, 1k tbn, 47.95 tbc Stream #0.1: Audio: ac3, 48000 Hz, 5.1, s16, 640 kb/s (default)
可以清晰的看到,这个电影只有两个流:一个是视频流,一个是音频流,还没有字幕呢。
然后我加一个字幕
✘ cecil@cecil-Aspire-4750 /media/cecil/FreeAgent GoFlex Drive/豆瓣250 /media/cecil/51650c88-09b9-4cda-a034-8f7012af8b19/ceclinux/bin/ffmpeg -i \[这个男人来自地球\].Man.From.Earth.DVDRip.Xvid.TFE.cht\&eng.mkv -i \[这个男人来自地球\].Man.From.Earth.DVDRip.Xvid.TFE.chs\&eng.srt -c copy -map 0:0 -map 1:0 这个男人来自地球.mkvffmpeg version 2.2.git Copyright (c) 2000-2014 the FFmpeg developers built on Apr 24 2014 21:31:10 with gcc 4.6 (Ubuntu/Linaro 4.6.3-1ubuntu5) configuration: --prefix=/home/ceclinux/ffmpeg_build --extra-cflags=-I/home/ceclinux/ffmpeg_build/include --extra-ldflags=-L/home/ceclinux/ffmpeg_build/lib --bindir=/home/ceclinux/bin --extra-libs=-ldl --enable-gpl --enable-libass --enable-libfdk-aac --enable-libfreetype --enable-libmp3lame --enable-libopus --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-nonfree --enable-x11grab libavutil 52. 78.100 / 52. 78.100 libavcodec 55. 58.105 / 55. 58.105 libavformat 55. 37.101 / 55. 37.101 libavdevice 55. 13.100 / 55. 13.100 libavfilter 4. 4.100 / 4. 4.100 libswscale 2. 6.100 / 2. 6.100 libswresample 0. 18.100 / 0. 18.100 libpostproc 52. 3.100 / 52. 3.100Input #0, matroska,webm, from '[这个男人来自地球].Man.From.Earth.DVDRip.Xvid.TFE.cht&eng.mkv': Metadata: encoder : libebml v0.7.9 + libmatroska v0.8.1 creation_time : 2010-02-03 01:40:21 Duration: 01:27:08.13, start: 0.000000, bitrate: 7168 kb/s Stream #0:0(eng): Video: h264 (High), yuv420p, 1280x720, SAR 1:1 DAR 16:9, 23.98 fps, 23.98 tbr, 1k tbn, 47.95 tbc Stream #0:1: Audio: ac3, 48000 Hz, 5.1(side), fltp, 640 kb/s (default)Input #1, srt, from '[这个男人来自地球].Man.From.Earth.DVDRip.Xvid.TFE.chs&eng.srt': Duration: N/A, bitrate: N/A Stream #1:0: Subtitle: subripFile '这个男人来自地球.mkv' already exists. Overwrite ? [y/N] yOutput #0, matroska, to '这个男人来自地球.mkv': Metadata: encoder : Lavf55.37.101 Stream #0:0(eng): Video: h264 (H264 / 0x34363248), yuv420p, 1280x720 [SAR 1:1 DAR 16:9], q=2-31, 23.98 fps, 1k tbn, 1k tbc Stream #0:1: Subtitle: subripStream mapping: Stream #0:0 -> #0:0 (copy) Stream #1:0 -> #0:1 (copy)Press [q] to stop, [?] for helpframe=31461 fps=1033 q=-1.0 size= 969127kB time=00:21:54.70 bitrate=6038.7kbits/s
这里 -i 表示输入的文件,map表示流的映射,-map 0:0 -map 1:0
表示将第一个文件的第一个流(视频流)和第二个文件的第一个流(字幕流)进行映射
-c 表示拷贝流
混入后。。
cecil@cecil-Aspire-4750 /media/cecil/FreeAgent GoFlex Drive/豆瓣250 ffprobe 这个男人来自地球.mkvavprobe version 0.8.10-6:0.8.10-0ubuntu0.12.10.1, Copyright (c) 2007-2013 the Libav developers built on Feb 6 2014 20:57:40 with gcc 4.7.2[matroska,webm @ 0x8839100] Estimating duration from bitrate, this may be inaccurateInput #0, matroska,webm, from '这个男人来自地球.mkv': Metadata: ENCODER : Lavf55.37.101 Duration: 01:27:08.18, start: 0.000000, bitrate: N/A Stream #0.0(eng): Video: h264 (High), yuv420p, 1280x720, PAR 1:1 DAR 16:9, 23.98 fps, 23.98 tbr, 1k tbn, 47.95 tbc (default) Stream #0.1: Subtitle: [0][0][0][0] / 0x0000 (default)Unsupported codec with id 94210 for input stream 1
很明显的是,新的合成视频是没有声音的。如果想要有声音怎么办?你懂的
另外,如果你想要用这种方式加入字幕的话,字幕的格式需要注意,最好的utf-8
的,不然加入的字幕在某些播放器上会出现点问题,比如我的gb18030
格式的字幕加入到视频后,smplayer
是乱码,vlc
没问题。。。
有一个转换字符集的命令
cecil@cecil-Aspire-4750 ~ iconv -f gb18030 -t UTF-8 \[这个男人来自地球\].Man.From.Earth.DVDRip.Xvid.TFE.chs\&eng.srt > \[这个男人来自地球\].Man.From.Earth.DVDRip.Xvid.TFE.chs\&eng-utf8.srt
嗯,暂时找不到方便的方法,必须要提供原字符集。如果有更好的请告诉我,谢谢
第二种加入字幕方法
第一种我们使用流的拷贝,第二种。。。直接嵌入视频!
首先,你先要看看自己的ffmpeg
有没有带subtitle
这个filter
cecil@cecil-Aspire-4750 ~ ffmpeg -filters|grep sub
没有就是一个比较悲剧的事情,需要点这里编译了
ffmpeg compilation
编译完成之后运行
✘ cecil@cecil-Aspire-4750 /media/cecil/FreeAgent GoFlex Drive/豆瓣250 /media/cecil/51650c88-09b9-4cda-a034-8f7012af8b19/ceclinux/bin/ffmpeg -i \[这个男人来自地球\].Man.From.Earth.DVDRip.Xvid.TFE.cht\&eng.mkv -vf subtitles='\[这个男人来自地球\].Man.From.Earth.DVDRip.Xvid.TFE.chs\&eng-utf8.srt' ~/这个aa.mkv
如果你去实践过,会发现一个问题:第二种方法实在是太耗时间太耗cpu了,为什么呢?
简单的说,第二种方法将字幕“渲染”进了视频,也就是说,对于每一帧图像,都加上了字幕,把原来该存在的像素点都覆盖成了字幕文字,电脑需要一帧一帧的处理数据。
那么filter
是什么东西
也就是说,原始的ffmpeg
架构,做了一点点手脚,对每一帧处理了下。subtitle filter
就是一个在视频上画字幕的filter
不信的话你可以用ffprobe
探测下新生成的文件,已经没有字幕流了。
但是第一方法那个copy
则不同
Stream copy is a mode selected by supplying the copy parameter to the ‘-codec’ option. It makes ffmpeg omit the decoding and encoding step for the specified stream, so it does only demuxing and muxing. It is useful for changing the container format or modifying container-level metadata. The diagram above will, in this case, simplify to this:
ffmpeg根本不需要做什么加码啊,解码啊。。直接分流和混流就行了,当然快了~
然后是做动图
利用ffmpeg
+ convert
两大杀器,我们可以弄一个从电影里抓一段的转化为gif动图脚本
#!/bin/bash#一个将mkv视屏中的一段做成动态gif的脚本#命令 ./video2gif.sh 视屏路径 横屏分辨率 开始时间 时长mkdir png#scale - If the value for width or height is -1, the scale filter will use, for the respective output size, a value that maintains the aspect ratio of the input image.# r - set the frame rate## -vf scale 送到scale过滤器if [[ $2 == "0" ]];then ffmpeg -ss $3 -i $1 -t $4 -r 20 png/snapshot%05d.pngelse ffmpeg -ss $3 -i $1 -t $4 -vf scale=${2}:-1 -r 20 png/snapshot%05d.pngfi#delay - display the next image after pausing# pattern removal - http://bash.sourceforge.net/Guide/bashguide05.html# 移除后缀convert -layers Optimize -delay 4/100 png/snapshot*.png ${1%%.*}.gifrm -rf png
这里我用scale
这个filter
来调整截成的图片的大小。
随便做几张影史经典的动图
搏击俱乐部 -- 带着妹子一起看炸大楼
肖申克的救赎 -- 逃出监狱,感受自由
有希望的男人 -- 呵呵
没了。
- ffmpeg简单实战
- 实战ios下编译ffmpeg
- 实战ios下编译ffmpeg
- 实战x264,ffmpeg库编译
- 实战FFmpeg编译支持arm64
- ffmpeg的简单应用
- java简单封装ffmpeg
- ffmpeg的简单例子
- ffmpeg简单教程
- ffmpeg简单使用
- FFmpeg简单介绍
- 最简单ffmpeg解码
- FFmpeg简单介绍
- ffmpeg tool 简单介绍
- ffmpeg 简单播放器
- MinGW下简单编译FFmpeg
- FFMPEG+SDL简单播放器
- MinGW下简单编译FFmpeg
- 19.剑指offer-把数组排成最小的数
- Android 认识EventBus轻量级事件总线框架
- Online Learning算法理论与实践
- 蓝桥杯java第八届B组:最大公共子串
- 【转】DESTDIR: GNU Make中的默认约定
- ffmpeg简单实战
- IOS addChildViewController方法使用及注意事项
- 欢迎使用CSDN-markdown编辑器
- JAVA导出excel(JFinal)
- Git 常用命令
- mysql查询5分钟内的数据
- nfs3方式挂载hdfs实现高可用存储
- HDU 6031 Innumerable Ancestors(LCA,树链剖分)
- linux下学习正则的一些小结