目录
| FFmpeg命令行工具学习(一):查看媒体文件头信息工具ffprobe |
| FFmpeg命令行工具学习(二):播放媒体文件的工具ffplay |
| FFmpeg命令行工具学习(三):媒体文件转换工具ffmpeg |
一、简述
ffmpeg是一个非常强大的工具,它可以转换任何格式的媒体文件,并且还可以用自己的AudioFilter以及VideoFilter进行处理和编辑。有了它,我们就可以对媒体文件做很多我们想做的事情了。
二、命令行参数
1. 通用参数
-f fmt : 指定格式
-i filename:指定输入文件名
-y:覆盖已有文件
-t duration:指定时长
-fs limit_size:设置文件大小的上限
-ss time_off: 从指定的时间开始
-re:代表按照真绿发送,尤其在作为推流工具的时候一定要加上该参数,否则ffpmeg会按照最高速率向流媒体不停的发送数据。
-map:指定输出文件的流映射关系。例如:“-map 1:0 -map 1:1”要求按照第二个输入的文件的第一个流和第二个流写入输出文件。如果没有设置此项,则ffpmeg采用默认的映射关系。
2. 视频参数
-b:指定比特率(bit/s),ffmpeg默认采用的是VBR的,若指定的该参数,则使用平均比特率。
-bitexact:使用标准比特率。
-vb:指定视频比特率(bit/s)
-r rate:帧速率(fps)
-s size:指定分辨率(320x240)
-aspect aspect:设置视频长宽比(4:3、16:9或1.33333、1.77777)
-croptop size:设置顶部切除尺寸(in pixels)
-cropleft size:设置左切除尺寸(in pixels)
-cropbottom size:设置地步切除尺寸(in pixels)
-cropright size:设置右切除尺寸(in pixels)
-padtop size:设置顶部补齐尺寸(in pixels)
-padleft size:设置左补齐尺寸(in pixels)
-padbottom size:设置地步补齐尺寸(in pixels)
-padright size:设置右补齐尺寸(in pixels)
-padcolor color:设置补齐颜色
-vn:取消视频的输出
-vcodec codec:强制使用codec编码方式
3. 音频参数
-ab:设置比特率(bit/s),对于MP3的格式,想要听到较高品质的声音,建议设置160Kbit/s(单声道80Kbit/s)以上。
-aq quality:设置音频质量
-ar ratre:设置音频采样率(Hz)
-ac channels:设置声道数,1就是单声道,2就是立体声
-an:取消音频输出
-acodec codec:强制使用codec编码方式
-vol volume:设置录制音量大小
以上就是在日常开发中经常用到的音视频参数及通用参数。下面会针对常见的开发场景进行实践和说明。
三、实践学习
1. 列出ffmpeg支持的所有格式
相关命令:
ffmpeg -formats
输出结果:

File formats:
D. = Demuxing supported
.E = Muxing supported --
D 3dostr 3DO STR
E 3g2 3GP2 (3GPP2 file format)
E 3gp 3GP (3GPP file format)
D 4xm 4X Technologies
E a64 a64 - video for Commodore 64
D aa Audible AA format files
D aac raw ADTS AAC (Advanced Audio Coding)
DE ac3 raw AC-3
D acm Interplay ACM
D act ACT Voice file format
D adf Artworx Data Format
D adp ADP
D ads Sony PS2 ADS
E adts ADTS AAC (Advanced Audio Coding)
DE adx CRI ADX
D aea MD STUDIO audio
D afc AFC
DE aiff Audio IFF
D aix CRI AIX
DE alaw PCM A-law
D alias_pix Alias/Wavefront PIX image
DE amr 3GPP AMR
D anm Deluxe Paint Animation
D apc CRYO APC
D ape Monkey's Audio DE apng Animated Portable Network Graphics
D aqtitle AQTitle subtitles
DE asf ASF (Advanced / Active Streaming Format)
D asf_o ASF (Advanced / Active Streaming Format)
E asf_stream ASF (Advanced / Active Streaming Format)
DE ass SSA (SubStation Alpha) subtitle
DE ast AST (Audio Stream)
DE au Sun AU
D avfoundation AVFoundation input device
DE avi AVI (Audio Video Interleaved)
E avm2 SWF (ShockWave Flash) (AVM2)
D avr AVR (Audio Visual Research)
D avs AVS
D bethsoftvid Bethesda Softworks VID
D bfi Brute Force & Ignorance
D bfstm BFSTM (Binary Cafe Stream)
D bin Binary text
D bink Bink
DE bit G.729 BIT file format
D bmp_pipe piped bmp sequence
D bmv Discworld II BMV
D boa Black Ops Audio
D brender_pix BRender PIX image
D brstm BRSTM (Binary Revolution Stream)
D c93 Interplay C93
DE caf Apple CAF (Core Audio Format)
DE cavsvideo raw Chinese AVS (Audio Video Standard) video
D cdg CD Graphics
D cdxl Commodore CDXL video
D cine Phantom Cine
D concat Virtual concatenation script
E crc CRC testing
E dash DASH Muxer
DE data raw data
DE daud D-Cinema audio
D dcstr Sega DC STR
D dds_pipe piped dds sequence
D dfa Chronomaster DFA
DE dirac raw Dirac
DE dnxhd raw DNxHD (SMPTE VC-3)
D dpx_pipe piped dpx sequence
D dsf DSD Stream File (DSF)
D dsicin Delphine Software International CIN
D dss Digital Speech Standard (DSS)
DE dts raw DTS
D dtshd raw DTS-HD
DE dv DV (Digital Video)
D dvbsub raw dvbsub
D dvbtxt dvbtxt
E dvd MPEG-2 PS (DVD VOB)
D dxa DXA
D ea Electronic Arts Multimedia
D ea_cdata Electronic Arts cdata
DE eac3 raw E-AC-3
D epaf Ensoniq Paris Audio File
D exr_pipe piped exr sequence
DE f32be PCM 32-bit floating-point big-endian
DE f32le PCM 32-bit floating-point little-endian
E f4v F4V Adobe Flash Video
DE f64be PCM 64-bit floating-point big-endian
DE f64le PCM 64-bit floating-point little-endian
DE ffm FFM (FFserver live feed)
DE ffmetadata FFmpeg metadata in text
E fifo FIFO queue pseudo-muxer
D film_cpk Sega FILM / CPK
DE filmstrip Adobe Filmstrip
DE fits Flexible Image Transport System
DE flac raw FLAC
D flic FLI/FLC/FLX animation
DE flv FLV (Flash Video)
E framecrc framecrc testing
E framehash Per-frame hash testing
E framemd5 Per-frame MD5 testing
D frm Megalux Frame
D fsb FMOD Sample Bank
DE g722 raw G.722
DE g723_1 raw G.723.1
DE g726 raw big-endian G.726 ("left-justified")
DE g726le raw little-endian G.726 ("right-justified")
D g729 G.729 raw format demuxer
D gdv Gremlin Digital Video
D genh GENeric Header
DE gif GIF Animation
DE gsm raw GSM
DE gxf GXF (General eXchange Format)
DE h261 raw H.261
DE h263 raw H.263
DE h264 raw H.264 video
E hash Hash testing
E hds HDS Muxer
DE hevc raw HEVC video
E hls Apple HTTP Live Streaming
D hls,applehttp Apple HTTP Live Streaming
D hnm Cryo HNM v4
DE ico Microsoft Windows ICO
D idcin id Cinematic
D idf iCE Draw File
D iff IFF (Interchange File Format)
DE ilbc iLBC storage
DE image2 image2 sequence
DE image2pipe piped image2 sequence
D ingenient raw Ingenient MJPEG
D ipmovie Interplay MVE
E ipod iPod H.264 MP4 (MPEG-4 Part 14)
DE ircam Berkeley/IRCAM/CARL Sound Format
E ismv ISMV/ISMA (Smooth Streaming)
D iss Funcom ISS
D iv8 IndigoVision 8000 video
DE ivf On2 IVF
D ivr IVR (Internet Video Recording)
D j2k_pipe piped j2k sequence
DE jacosub JACOsub subtitle format
D jpeg_pipe piped jpeg sequence
D jpegls_pipe piped jpegls sequence
D jv Bitmap Brothers JV
E latm LOAS/LATM
D lavfi Libavfilter virtual input device
D live_flv live RTMP FLV (Flash Video)
D lmlm4 raw lmlm4
D loas LOAS AudioSyncStream
DE lrc LRC lyrics
D lvf LVF
D lxf VR native stream (LXF)
DE m4v raw MPEG-4 video
E matroska Matroska
D matroska,webm Matroska / WebM
E md5 MD5 testing
D mgsts Metal Gear Solid: The Twin Snakes
DE microdvd MicroDVD subtitle format
DE mjpeg raw MJPEG video
D mjpeg_2000 raw MJPEG 2000 video
E mkvtimestamp_v2 extract pts as timecode v2 format, as defined by mkvtoolnix
DE mlp raw MLP
D mlv Magic Lantern Video (MLV)
D mm American Laser Games MM
DE mmf Yamaha SMAF
E mov QuickTime / MOV
D mov,mp4,m4a,3gp,3g2,mj2 QuickTime / MOV
E mp2 MP2 (MPEG audio layer 2)
DE mp3 MP3 (MPEG audio layer 3)
E mp4 MP4 (MPEG-4 Part 14)
D mpc Musepack
D mpc8 Musepack SV8
DE mpeg MPEG-1 Systems / MPEG program stream
E mpeg1video raw MPEG-1 video
E mpeg2video raw MPEG-2 video
DE mpegts MPEG-TS (MPEG-2 Transport Stream)
D mpegtsraw raw MPEG-TS (MPEG-2 Transport Stream)
D mpegvideo raw MPEG video
DE mpjpeg MIME multipart JPEG
D mpl2 MPL2 subtitles
D mpsub MPlayer subtitles
D msf Sony PS3 MSF
D msnwctcp MSN TCP Webcam stream
D mtaf Konami PS2 MTAF
D mtv MTV
DE mulaw PCM mu-law
D musx Eurocom MUSX
D mv Silicon Graphics Movie
D mvi Motion Pixels MVI
DE mxf MXF (Material eXchange Format)
E mxf_d10 MXF (Material eXchange Format) D-10 Mapping
E mxf_opatom MXF (Material eXchange Format) Operational Pattern Atom
D mxg MxPEG clip
D nc NC camera feed
D nistsphere NIST SPeech HEader REsources
D nsv Nullsoft Streaming Video
E null raw null video
DE nut NUT
D nuv NuppelVideo
E oga Ogg Audio
DE ogg Ogg
E ogv Ogg Video
DE oma Sony OpenMG audio
E opus Ogg Opus
D paf Amazing Studio Packed Animation File
D pam_pipe piped pam sequence
D pbm_pipe piped pbm sequence
D pcx_pipe piped pcx sequence
D pgm_pipe piped pgm sequence
D pgmyuv_pipe piped pgmyuv sequence
D pictor_pipe piped pictor sequence
D pjs PJS (Phoenix Japanimation Society) subtitles
D pmp Playstation Portable PMP
D png_pipe piped png sequence
D ppm_pipe piped ppm sequence
D psd_pipe piped psd sequence
E psp PSP MP4 (MPEG-4 Part 14)
D psxstr Sony Playstation STR
D pva TechnoTrend PVA
D pvf PVF (Portable Voice Format)
D qcp QCP
D qdraw_pipe piped qdraw sequence
D r3d REDCODE R3D
DE rawvideo raw video
D realtext RealText subtitle format
D redspark RedSpark
D rl2 RL2
DE rm RealMedia
DE roq raw id RoQ
D rpl RPL / ARMovie
D rsd GameCube RSD
DE rso Lego Mindstorms RSO
DE rtp RTP output
E rtp_mpegts RTP/mpegts output format
DE rtsp RTSP output
DE s16be PCM signed 16-bit big-endian
DE s16le PCM signed 16-bit little-endian
DE s24be PCM signed 24-bit big-endian
DE s24le PCM signed 24-bit little-endian
DE s32be PCM signed 32-bit big-endian
DE s32le PCM signed 32-bit little-endian
D s337m SMPTE 337M
DE s8 PCM signed 8-bit
D sami SAMI subtitle format
DE sap SAP output
D sbg SBaGen binaural beats script
DE scc Scenarist Closed Captions
D sdp SDP
D sdr2 SDR2
D sds MIDI Sample Dump Standard
D sdx Sample Dump eXchange
E segment segment
D sgi_pipe piped sgi sequence
D shn raw Shorten
D siff Beam Software SIFF
E singlejpeg JPEG single image
D sln Asterisk raw pcm
DE smjpeg Loki SDL MJPEG
D smk Smacker
E smoothstreaming Smooth Streaming Muxer
D smush LucasArts Smush
D sol Sierra SOL
DE sox SoX native
DE spdif IEC 61937 (used on S/PDIF - IEC958)
E spx Ogg Speex
DE srt SubRip subtitle
D stl Spruce subtitle format
E stream_segment,ssegment streaming segment muxer
D subviewer SubViewer subtitle format
D subviewer1 SubViewer v1 subtitle format
D sunrast_pipe piped sunrast sequence
DE sup raw HDMV Presentation Graphic Stream subtitles
D svag Konami PS2 SVAG
E svcd MPEG-2 PS (SVCD)
D svg_pipe piped svg sequence
DE swf SWF (ShockWave Flash)
D tak raw TAK
D tedcaptions TED Talks captions
E tee Multiple muxer tee
D thp THP
D tiertexseq Tiertex Limited SEQ
D tiff_pipe piped tiff sequence
D tmv 8088flex TMV
DE truehd raw TrueHD
DE tta TTA (True Audio)
D tty Tele-typewriter
D txd Renderware TeXture Dictionary
DE u16be PCM unsigned 16-bit big-endian
DE u16le PCM unsigned 16-bit little-endian
DE u24be PCM unsigned 24-bit big-endian
DE u24le PCM unsigned 24-bit little-endian
DE u32be PCM unsigned 32-bit big-endian
DE u32le PCM unsigned 32-bit little-endian
DE u8 PCM unsigned 8-bit
E uncodedframecrc uncoded framecrc testing
D v210 Uncompressed 4:2:2 10-bit
D v210x Uncompressed 4:2:2 10-bit
D vag Sony PS2 VAG
DE vc1 raw VC-1 video
DE vc1test VC-1 test bitstream
E vcd MPEG-1 Systems / MPEG program stream (VCD)
D vivo Vivo
D vmd Sierra VMD
E vob MPEG-2 PS (VOB)
D vobsub VobSub subtitle format
DE voc Creative Voice
D vpk Sony PS2 VPK
D vplayer VPlayer subtitles
D vqf Nippon Telegraph and Telephone Corporation (NTT) TwinVQ
DE w64 Sony Wave64
DE wav WAV / WAVE (Waveform Audio)
D wc3movie Wing Commander III movie
E webm WebM
E webm_chunk WebM Chunk Muxer
DE webm_dash_manifest WebM DASH Manifest
E webp WebP
D webp_pipe piped webp sequence
DE webvtt WebVTT subtitle
D wsaud Westwood Studios audio
D wsd Wideband Single-bit Data (WSD)
D wsvqa Westwood Studios VQA
DE wtv Windows Television (WTV)
DE wv raw WavPack
D wve Psion 3 audio
D xa Maxis XA
D xbin eXtended BINary text (XBIN)
D xmv Microsoft XMV
D xpm_pipe piped xpm sequence
D xvag Sony PS3 XVAG
D xwma Microsoft xWMA
D yop Psygnosis YOP
DE yuv4mpegpipe YUV4MPEG pipeView Code
2. 剪切一段媒体文件,可以是音频或者视频文件
相关命令:
ffmpeg -i pm.mp4 -ss 00:00:50.0 -codec copy -t 20 output.mp4
命令说明:
表示将文件pm.mp4从第50s开始剪切20s的时间,输出到output.mp4中,其中-ss指定偏移时间(time Offset),-t指定的时长(duration)。
但是直接这样执行命令,固然我们能截取出来音视频的文件,但是当我们播放的时候,我们会发现虽然ffmepg剪切视频,很方便,但是也有很大缺陷:
(1). 剪切时间点不精确
(2). 有时剪切的视频开头有黑屏
造成这些问题的原因是ffmpeg无法seek到非关键帧上。
命令层面定位的话就是如果把-ss, -t参数放在-i参数之后,是对输出文件执行的seek操作
输入文件会逐帧解码,直到-ss设置的时间点为止,这么操作会很慢,虽然时间点是准确的,但是很容易出现黑屏问题。
所以:我们优化了一下上面的那个命令,让视频的剪切更加精确:
ffmpeg -ss 10 -t 15 -accurate_seek -i pm.mp4 -codec copy output.mp4
注意:accurate_seek必须放在-i参数之前
3. 提取一个视频文件中的音频,并保存为文件
ffmpeg -i pm.mp4 -vn -acodec copy output.m4a
4. 将视频中的音频静音,只保留视频
ffmpeg -i pm.mp4 -an -vcodec copy output.mp4
5. 从mp4文件中抽取视频流导出为裸H264数据:
ffmpeg -i pm.mp4 -an -vcodec copy -bsf:v h264_mp4toannexb output.h264
验证播放的话,可以使用ffplay命令。说明一下上面的命令:
在指令中,我们舍弃了音频数据(-an),视频数据使用mp4toannexb这个bitstreasm filter来转换为原始的H264数据。注:同一编码也会有不同的封装格式。
6. 将一段视频推送到流媒体服务器上:
ffmpeg -re -i pm.mp4 -acodec copy -vcodec copy -f flv rtmp://127.0.0.1/rh/mylive
7. 将流媒体服务器上的流dump到本地:
ffmpeg -i rtmp://127.0.0.1/rh/mylive -acodec copy -vcodec copy -f flv test.flv
共同學習,寫下你的評論
評論加載中...
作者其他優質文章