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.html
简单的说,ffmpeg乃江湖神器。它能帮你将视频一帧帧录成图片
,能帮你自由转换视频的格式
,什么加字幕加音轨啦,什么录制屏幕啦,都很easylinux
ffmpeg
命令都是长的吓人,可是基本原理仍是简单的git
一张图~web
基本上就是这样的流程:输入文件==>被分离器分离的数据包==>被解码的数据帧==>被编码的数据包==>输出文件ubuntu
首先,咱们能够给视频加上字幕~vim
好比说我有一步电影信息以下bash
cecil@cecil-Aspire-4750 /media/cecil/FreeAgent GoFlex Drive/豆瓣250 ffprobe \[这个男人来自地球\].Man.From.Earth.DVDRip.Xvid.TFE.cht\&eng.mkv avprobe 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 inaccurate Input #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)
能够清晰的看到,这个电影只有两个流:一个是视频流,一个是音频流,尚未字幕呢。架构
而后我加一个字幕app
✘ 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 这个男人来自地球.mkv ffmpeg 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.100 Input #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: subrip File '这个男人来自地球.mkv' already exists. Overwrite ? [y/N] y Output #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: subrip Stream mapping: Stream #0:0 -> #0:0 (copy) Stream #1:0 -> #0:1 (copy) Press [q] to stop, [?] for help frame=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 表示拷贝流ide
混入后。。
cecil@cecil-Aspire-4750 /media/cecil/FreeAgent GoFlex Drive/豆瓣250 ffprobe 这个男人来自地球.mkv avprobe 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 inaccurate Input #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
没有就是一个比较悲剧的事情,须要点这里编译了
编译完成以后运行
✘ 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.png else ffmpeg -ss $3 -i $1 -t $4 -vf scale=${2}:-1 -r 20 png/snapshot%05d.png fi #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%%.*}.gif rm -rf png
这里我用scale
这个filter
来调整截成的图片的大小。
随便作几张影史经典的动图
搏击俱乐部 -- 带着妹子一块儿看炸大楼
肖申克的救赎 -- 逃出监狱,感觉自由
有但愿的男人 -- 呵呵
没了。