ffmpeg工具的简单使用

======================

分为三部分:

1.ffmpeg介绍,及简单使用

2.ffprobe介绍,及简单使用

3.ffplay介绍,及简单使用

======================

在很多情况下转码使用的是ffmpeg。具体怎么操作,我们可以通过ffmpeg --help查看帮助文档来进行操作。

ffmpeg --help              //获得基础的帮助信息

ffmpeg --help  long    //获得高级参数部分

ffmpeg --help  full       //获得全部的帮助信息

usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...
.... 
Print help / information / capabilities:
-L                  show license
-h topic            show help
-? topic            show help
-help topic         show help
--help topic        show help
-version            show version
-buildconf          show build configuration
-formats            show available formats
-devices            show available devices
-codecs             show available codecs
-decoders           show available decoders
-encoders           show available encoders
-bsfs               show available bit stream filters
-protocols          show available protocols
-filters            show available filters
-pix_fmts           show available pixel formats
-layouts            show standard channel layouts
-sample_fmts        show available audio sample formats
-colors             show available color names
-sources device     list sources of the input device
-sinks device       list sinks of the output device
-hwaccels           show available HW acceleration methods

ffmpeg --help  full

....

ffmpeg --help  long

....

ffmpeg的基本转码原理:

ffmpeg  -i output.avi -vcodec mpeg4 -b:v 200k -r 15 -an output.mp4

 ffmpeg version 3.0 Copyright (c) 2000-2016 the FFmpeg developers
  built with gcc 4.8.5 (GCC) 20150623 (Red Hat 4.8.5-28)
      ......
      ......
  libswresample   2.  0.101 /  2.  0.101
  libpostproc    54.  0.100 / 54.  0.100
Input #0, avi, from 'output.avi':
  Metadata:
    encoder         : Lavf57.25.100
  Duration: 00:15:09.06, start: 0.000000, bitrate: 402 kb/s
    Stream #0:0: Video: mpeg4 (Simple Profile) (FMP4 / 0x34504D46), yuv420p, 1152x720 [SAR 1:1 DAR 8:5], 204 kb/s, 5 fps, 5 tbr, 5 tbn, 5 tbc
    Stream #0:1: Audio: ac3 ([0] [0][0] / 0x2000), 44100 Hz, stereo, fltp, 192 kb/s
Output #0, mp4, to 'output.mp4':
  Metadata:
    encoder         : Lavf57.25.100
    Stream #0:0: Video: mpeg4 ( [0][0][0] / 0x0020), yuv420p, 1152x720 [SAR 1:1 DAR 8:5], q=2-31, 200 kb/s, 15 fps, 15360 tbn, 15 tbc
    Metadata:
      encoder         : Lavc57.24.102 mpeg4
    ....... 

从上面的信息可以看出:

1>转封装格式从AVI格式到MP4
2>视频编码是mpeg4,没变
3>视频的码率从204k专成了200k
4>视频的祯率从5fps转成了15fps
5>(-an)转码后没有了音频

整个流程依然是: 解封装->解码(音频,视频)->编码(音频,视频)->封装。

=======================================================================================

ffprobe多么媒体信息查看工具的介绍:

ffprobe --help查看相应的帮助信息。

usage: ffprobe [OPTIONS] [INPUT_FILE]

Main options:
-L                  show license
-h topic            show help
-? topic            show help
-help topic         show help
--help topic        show help
-version            show version
-buildconf          show build configuration
-formats            show available formats
-devices            show available devices
-codecs             show available codecs
-decoders           show available decoders
-encoders           show available encoders
-bsfs               show available bit stream filters
-protocols          show available protocols
-filters            show available filters
-pix_fmts           show available pixel formats
-layouts            show standard channel layouts
-sample_fmts        show available audio sample formats
-colors             show available color names
-loglevel loglevel  set logging level
-v loglevel         set logging level
-report             generate a report
-max_alloc bytes    set maximum size of a single allocated block
-cpuflags flags     force specific cpu flags
-hide_banner hide_banner  do not show program banner
-sources device     list sources of the input device
-sinks device       list sinks of the output device
-f format           force format
-unit               show unit of the displayed values
-prefix             use SI prefixes for the displayed values
-byte_binary_prefix  use binary prefixes for byte units
-sexagesimal        use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units
-pretty             prettify the format of displayed values, make it more human readable
-print_format format  set the output printing format (available formats are: default, compact, csv, flat, ini, json, xml)
-of format          alias for -print_format
-select_streams stream_specifier  select the specified streams
-sections           print sections structure and section information, and exit
-show_data          show packets data
-show_data_hash     show packets data hash
-show_error         show probing error
-show_format        show format/container info
-show_frames        show frames info
-show_format_entry entry  show a particular entry from the format/container info
-show_entries entry_list  show a set of specified entries
-show_packets       show packets info
-show_programs      show programs info
-show_streams       show streams info
-show_chapters      show chapters info
-count_frames       count the number of frames per stream
-count_packets      count the number of packets per stream
-show_program_version  show ffprobe version
-show_library_versions  show library versions
-show_versions      show program and library versions
-show_pixel_formats  show pixel format descriptions
-show_private_data  show private data
-private            same as show_private_data
-bitexact           force bitexact output
-read_intervals read_intervals  set read intervals
-default            generic catch all option
-i input_file       read specified file

ffprobe -show_packets 111.mp4可以查看多媒体数据包信息:

[PACKET]           //为其中一个音频数据包的内容
codec_type=audio                            //codec_type  类型 :视频包,音频包
stream_index=1                              //stream索引
pts=40087459                                //pts   多媒体的显示时间
pts_time=909.012676
dts=40087459                                //dts 多媒体解码时间值
dts_time=909.012676                         //
duration=1024                               //多媒体包占用的时间
duration_time=0.023220
convergence_duration=N/A                    
convergence_duration_time=N/A
size=348                                    //size  多媒体包的大小
pos=31528832                                //pos   多媒体包杂文件偏移的位置
flags=K                                     //flags  多媒体包标记,如关键包与非关键包的标记
[/PACKET]
[PACKET]          //其中一个视频数据包
codec_type=video
stream_index=0
pts=4493000
pts_time=898.600000
dts=4493000
dts_time=898.600000
duration=1000
duration_time=0.200000
convergence_duration=N/A
convergence_duration_time=N/A
size=27135
pos=30760905
flags=_
[/PACKET]

ffprobe -show_data  -show_packets 111.mp4   可以查看包中的具体数据。

linux下查看十六进制数据:xxd 111.mp4

ffprobe -show_format 111.mp4 可以查看多媒体的封装格式,是使用FORMAT标签括起来的:

[FORMAT]
filename=111.mp4                    //文件名
nb_streams=2                        //媒体中包含的流的个数
nb_programs=0                       //节目数
format_name=mov,mp4,m4a,3gp,3g2,mj2 //使用的封装模块的名字
format_long_name=QuickTime / MOV    //封装的完整名称
start_time=0.000000                 //文件的起始时间
duration=909.038000                 //文件的总时长
size=31529180                       //文件的大小  字节
bit_rate=277472                     //文件的码率  bit/s
probe_score=100
TAG:major_brand=mp42
TAG:minor_version=0
TAG:compatible_brands=isommp42
TAG:creation_time=2015-05-05 19:37:53
TAG:artist=
TAG:description=
TAG:title=
[/FORMAT]
因此我们可以使用该命令查看比如flv  ,   wmv ,  avi等格式的封装数据。


ffprobe -show_frames  111.mp4 查看视频文件中的帧信息;FRAME标签括起来

[FRAME]
media_type=video
stream_index=0
key_frame=1                                //是否为关键帧
pkt_pts=4544000
pkt_pts_time=908.800000
pkt_dts=4544000
pkt_dts_time=908.800000
best_effort_timestamp=4544000
best_effort_timestamp_time=908.800000
pkt_duration=1000
pkt_duration_time=0.200000
pkt_pos=31454676
pkt_size=58465
width=1152
height=720
pix_fmt=yuv420p
sample_aspect_ratio=1:1
pict_type=I                                //帧的类型  I  ,P  ,B 帧
coded_picture_number=4544
display_picture_number=0
interlaced_frame=0
top_field_first=0
repeat_pict=0
[/FRAME]

ffprobe -show_streams  111.mp4 查看视频文件中的流信息;STREAM标签括起来

[STREAM]
index=0
codec_name=h264
codec_long_name=H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10
profile=Main
codec_type=video
codec_time_base=1/10
codec_tag_string=avc1
codec_tag=0x31637661
width=1152
height=720
coded_width=1152
coded_height=720
has_b_frames=0
sample_aspect_ratio=1:1
display_aspect_ratio=8:5
pix_fmt=yuv420p
level=31
color_range=tv
color_space=unknown
color_transfer=unknown
color_primaries=unknown
chroma_location=left
timecode=N/A
refs=3
is_avc=true
nal_length_size=4
id=N/A
r_frame_rate=5/1
avg_frame_rate=5/1
time_base=1/5000
start_pts=0
start_time=0.000000
duration_ts=4545000
duration=909.000000
bit_rate=150173
max_bit_rate=N/A
bits_per_raw_sample=8
nb_frames=4545
nb_read_frames=N/A
nb_read_packets=N/A
....
TAG:creation_time=2015-05-05 19:37:54
TAG:language=eng
TAG:handler_name=Mainconcept MP4 Video Media Handler
TAG:encoder=AVC Coding
[/STREAM]

另外介绍几个输出对应格式的指令:

ffprobe   -of   xml  -show_streams  111.mp4得到xml的输出格式

ffprobe   -of   ini  -show_streams  111.mp4 得到INI的格式输出

ffprobe   -of   flat -show_streams  111.mp4 得到FLAT的格式输出

ffprobe   -of  json  -show_streams  111.mp4 得到JSON的格式输出

ffprobe   -of  csv  -show_streams  111.mp4 得到CSV的格式输出,输出后可以使用对应软件绘制表格和相应的图形。

======================================================================================

ffplay工具常见使用方法介绍:

在编译ffpmpeg的时候,时需要SDL的,才能够使用ffplay。

ffplay的用途比较多,做视频播放器。做音视频的分析器的工具也很常见。

首先,通样是从帮助文档开始。ffplay --help

usage: ffplay [options] input_file

Main options:
-L                  show license
-h topic            show help
-? topic            show help
-help topic         show help
--help topic        show help
-version            show version
-buildconf          show build configuration
-formats            show available formats
-devices            show available devices
-codecs             show available codecs
-decoders           show available decoders
-encoders           show available encoders
-bsfs               show available bit stream filters
-protocols          show available protocols
-filters            show available filters
-pix_fmts           show available pixel formats
-layouts            show standard channel layouts
-sample_fmts        show available audio sample formats
-colors             show available color names
-loglevel loglevel  set logging level
-v loglevel         set logging level
-report             generate a report
-max_alloc bytes    set maximum size of a single allocated block
-sources device     list sources of the input device
-sinks device       list sinks of the output device
-x width            force displayed width
-y height           force displayed height
-s size             set frame size (WxH or abbreviation)
-fs                 force full screen
-an                 disable audio
-vn                 disable video
-sn                 disable subtitling
-ss pos             seek to a given position in seconds
-t duration         play  "duration" seconds of audio/video
-bytes val          seek by bytes 0=off 1=on -1=auto
-nodisp             disable graphical display
-f fmt              force format
-window_title window title  set window title
-af filter_graph    set audio filters
-showmode mode      select show mode (0 = video, 1 = waves, 2 = RDFT)
-i input_file       read specified file
-codec decoder_name  force decoder
-autorotate         automatically rotate video

Advanced options:
-cpuflags flags     force specific cpu flags
-hide_banner hide_banner  do not show program banner
-ast stream_specifier  select desired audio stream
-vst stream_specifier  select desired video stream
-sst stream_specifier  select desired subtitle stream
-pix_fmt format     set pixel format
-stats              show status
-fast               non spec compliant optimizations
-genpts             generate pts
-drp                let decoder reorder pts 0=off 1=on -1=auto
-lowres             
-sync type          set audio-video sync. type (type=audio/video/ext)
-autoexit           exit at the end
-exitonkeydown      exit on key down
-exitonmousedown    exit on mouse down
-loop loop count    set number of times the playback shall be looped
-framedrop          drop frames when cpu is too slow
-infbuf             don't limit the input buffer size (useful with realtime streams)
-vf filter_graph    set video filters
-rdftspeed msecs    rdft speed
-default            generic catch all option
-acodec decoder_name  force audio decoder
-scodec decoder_name  force subtitle decoder
-vcodec decoder_name  force video decoder

如一些简单的例子:

ffplay   -ss   30   -t   10   111.mp4

ffplay   -window_title "hello world"   111.mp4

ffplay   -window_title  "播放测试"   rtmp://https://v.youku.com/v_show/id_XMzcxMDMzNTY0MA==.html?from=miniplay-1.0.0&f=&firstTime=

ffplay高级参数,见上面表的:Advanced options,列举出的内容。

例如:

ffplay -vcodec h264 111.mp4     强制使用H.264解码MPEG4的视频等。

////////////////////////////////////

如果希望使用ffplay给视频加入字幕文件,你可以通过加载ASS或者SRT字幕文件来实现。比如,以SRT的字幕来实验。

1.我们首先需要编辑SRT字幕文件:

1
00:00:01.000--> 00:00:30.000
test by zhangsan

2
00:00:30.001 --> 00:00:60.000
test by lisi

3
00:01:01.000  --> 00:01:30.000
test by wangwu

2.通过filter将字幕加载到播放器数据中:

ffplay -window_title  "test_srt"  -vf  "subtitles=111.mp4.srt"   111.mp4

然后就可以播放了。

关于字幕部分,会用到--enable-libass,要在编译的时候需要libass这个库

可以参考这篇博客 https://blog.csdn.net/evsqiezi/article/details/52396986

////////////////////////////////////////////

外挂字幕与内嵌字幕

外挂字幕相对于内嵌字幕来说对视频的质量损害就会小很多,外挂的意思就是 在视频之外单独运行的一种字幕文件,对视频本身的分辨率损害很小甚至为零。而内嵌的字面意思就是将视频连带外挂字幕用专有的录制软件重新将视频录制一遍,成为一个新的视频;这种方法虽然解决了视频体积过大和播放器不兼容等问题,但是在重新录制视频过程当中会无意识的损害原视频本身的码率,使重新录制出来的 视频分辨率大大不如原视频,所以在选择外挂与内嵌字幕时需结合自身情况考虑视频需要进行选择。

//////////////////////////////////////////////

ffplay -showmode 1 111.mp4

ffplay -vismv pf  111.mp4        查看B帧和P帧预测信息

猜你喜欢

转载自blog.csdn.net/lailaiquququ11/article/details/80958431
今日推荐