当前位置:网站首页>Ffmpeg 2 - use of ffplay, ffprobe, ffmpeg commands

Ffmpeg 2 - use of ffplay, ffprobe, ffmpeg commands

2022-07-23 14:29:00 Yizhi code


Official document Command Line Tools Documentation part , There is a detailed introduction to the command
https://ffmpeg.org/documentation.html

Examples come from the network , To be sorted and updated .


ffplay

Command specification

Basic commands

help

  • -L, show license
  • -h topic, show help
  • -? topic, show help
  • -help topic, show help
  • --help topic, show help
  • -version, show version

display information

  • -buildconf, show build configuration
  • -formats, show available formats
  • -muxers, show available muxers
  • -demuxers, show available demuxers
  • -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
  • -dispositions, show available stream dispositions
  • -colors, show available color names

Set up

  • -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 | Force broadband display
  • -y height, force displayed height | | Force display height
  • -s size, set frame size (WxH or abbreviation)
  • -fs, force full screen | Start in full screen mode
  • -an, disable audio | Disable audio ( No sound )
  • -vn, disable video | Disable video ( No video )
  • -sn, disable subtitling | Disable subtitles ( No subtitles )
  • -ss pos, seek to a given position in seconds Jump to the designated position , Pay attention to the time unit ; such as :
    • 55, 55 seconds
    • 12:03:45, 12 hours, 03 minutes and 45 seconds
    • 23.189, 23.189 second
  • -t duration , play “duration” seconds of audio/video | Set to play video / Audio length , Unit of time, e.g -ss Options
  • -bytes val, seek by bytes 0=off 1=on -1=auto | Jump by byte (0=off 1=on -1=auto)
  • -seek_interval seconds, set seek interval for left/right keys, in seconds Custom left / Right click to locate the drag interval ( In seconds ), The default value is 10 second
  • -nodisp, disable graphical display Close the graphical display window , The video will not show
  • -noborder, borderless window Borderless window
  • -alwaysontop, window always on top
  • -volume volume, set startup volume 0=min 100=max | Set the starting volume . Volume range [0 ~100]
  • -f fmt, force format
  • -window_title window title, set window title | Set the window title ( The default is to enter the file name )
  • -af filter_graph, set audio filters
  • -showmode mode, select show mode (0 = video, 1 = waves, 2 = RDFT)
    Set the display mode , Available mode values :0 Show video ,1 Display audio waveform ,2 Display the audio spectrum .
    Default is 0, If the video does not exist, it will be automatically selected 2
  • -i input_file, read specified file
  • -codec decoder_name, force decoder
  • -autorotate, automatically rotate video

High order

  • -cpuflags flags, force specific cpu flags
    Print multiple playback Statistics , Including display stream duration , Codec parameters , The current position in the stream , And audio / Video synchronization difference .
    Enabled by default , To explicitly disable it, you need to specify -nostats.
  • -cpucount count, force specific cpu count
  • -hide_banner, hide_banner do not show program banner
  • -ast stream_specifier, select desired audio stream
    Specifies the audio stream index , such as -ast 3, The playback stream index is 3 The audio stream of
  • -vst stream_specifier, select desired video stream
    Specifies the video stream index , such as -vst 4, The playback stream index is 4 Video stream of
  • -sst stream_specifier, select desired subtitle stream
    Specifies the caption stream index , such as -sst 5, The playback stream index is 5 Subtitle stream
  • -pix_fmt format, force pixel format
  • -stats, show status
  • -fast, non spec compliant optimizations
    Non standardized multimedia compatibility optimization .
  • -genpts, generate pts | Generate pts.
  • -drp, let decoder reorder pts 0=off 1=on -1=auto
  • -lowres
  • -sync type, force audio-video sync. type (type=audio/video/ext)
    Synchronization type Set the master clock to audio(type=audio),video(type=video) or external(type=ext), The default is audio Master clock .
  • -autoexit, force at the end | Exit after the video is played
  • -exitonkeydown, force on key down | Press any key on the keyboard to exit playback
  • -exitonmousedown, force on mouse down | Press any key with the mouse to quit playing
  • -loop loop count, force number of times the playback shall be looped
  • -framedrop, drop frames when cpu is too slow | If the video is not synchronized, the video frame is discarded . When the master clock is not a video clock, it is turned on by default . To disable, use -noframedrop
  • -infbuf, don’t limit the input buffer size (useful with realtime streams)
    Do not limit the size of the input buffer . Read as much data as possible from the input as soon as possible . Enabled by default when playing live streaming , If the data is not read in time , Data may be discarded .
    This option will not limit the size of the buffer . To disable, use -noinfbuf
  • -left x pos, force the x position for the left of the window
  • -top y pos, force the y position for the top of the window
  • -vf filter_graph, force video filters
  • -rdftspeed msecs, rdft speed
  • -default, generic catch all option
  • -acodec decoder_name, force audio decoder | Force to use the set audio decoder for audio decoding
  • -scodec decoder_name, force subtitle decoder | Forced to use the set caption decoder for caption decoding
  • -vcodec decoder_name, force video decoder | Force the set video decoder to be used for video decoding
  • -find_stream_info, read and decode the streams to fill missing information with heuristics
  • -filter_threads, number of filter threads per graph

Examples of use

Play

#  Play the audio 、 Video file 
ffplay test.mp3

ffplay input.mp4

#  Automatically exit after playing 
ffplay -autoexit input.mp4


#  Loop the file 10 Time 
ffplay  file name  -loop 10

#  Play local files ,  Set title 
ffplay -window_title "test time" -ss 2 -t 10 -autoexit test.mp4

#  Play network stream 
ffplay -window_title "rtmp stream" rtmp://10.0.100.88:443/webcast/bshdlive-pc

#  Disable audio or video 
#  Disable audio :
ffplay test.mp4 -an

#  Disable video :
ffplay test.mp4 -vn

#  Play the first audio stream of video ; If the parameter is 2  It's the second audio stream , If not, it will mute .
ffplay  file name  -ast 1

#  Play the first video stream ; If the parameter is 2  It's the second video stream   There is no black screen 
ffplay  file name  -vst 1

#  Play pcm file   The parameters must be set correctly 
ffplay .pcm file  -f  Format  -channels 2  Track number  -ar  Sampling rate 

# -f rawvideo  Represents the original format 
ffplay -f rawvideo -pixel_format yuv420p -s 480480 .yuv file 

# -s 480480  Wide and high 
-pixel_format yuv420p  Representation format 

#  Play rgb Raw data 
ffplay -f rawvideo -pixel_format rgb24 -s 480*480 .rgb file 

#  Appoint ffplay Use audio as the benchmark for audio and video synchronization. Default ffplay In the same way 
ffplay  file name  -sync audio

#  Appoint ffplay Use video as the benchmark for audio and video synchronization 
ffplay  file name  -sync video

#  Appoint ffplay Use the external clock as the benchmark for audio and video synchronization 
ffplay  file name  -ext video

#  Force decoder 
# mpeg4 decoder 
ffplay -vcodec mpeg4 test.mp4

# h264 decoder :
ffplay -vcodec h264 test.mp4 

#  Play YUV data 
ffplay -pixel_format yuv420p -video_size 320x240 -framerate 5 yuv420p_320x240.yuv

#  Play RGB data 
ffplay -pixel_format rgb24 -video_size 320x240 -i rgb24_320x240.rgb

ffplay -pixel_format rgb24 -video_size 320x240 -framerate 5 -i rgb24_320x240.rgb

#  Play PCM data 
ffplay -ar 48000 -ac 2 -f f32le 48000_2_f32le.pcm
		-ar set audio sampling rate (in Hz) (from 0 to INT_MAX) (default 0)
		-ac set number of audio channels (from 0 to INT_MAX) (default 0)
         

Simple filter

#  Video rotation 
ffplay -i test.mp4 -vf transpose=1

#  Video inversion 
ffplay test.mp4 -vf hflip
ffplay test.mp4 -vf vflip

#  Video rotation and inversion 
ffplay test.mp4 -vf hflip,transpose=1

#  Audio variable speed playback 
ffplay -i test.mp4 -af atempo=2

#  Video variable speed playback 
ffplay -i test.mp4 -vf setpts=PTS/2

#  Audio and video speed changes at the same time 
ffplay -i test.mp4 -vf setpts=PTS/2 -af atempo=2
 

ffprobe View video information

Option description

  • -L, Display protocol
  • -h/-?/-help/--help topic, Help can choose topics
  • -version, Display version
  • -buildconf, Show compilation configuration options
  • -formats, Show supported encodings
  • -muxers, Show supported wrappers
  • -demuxers, Show supported unpackers
  • -devices, Show supported devices
  • -codecs, Show the supported codes
  • -decoders, Show supported decoders
  • -encoders, Display supported encoders

  • -bsfs, Show supported bitstream filters
  • -protocols, Show supported protocols
  • -filters, Show supported filters
  • -pix_fmts, Display supported pixel formats
  • -layouts, Show supported Channel Formats
  • -sample_fmts, Displays the supported sampling formats
  • -colors, Show supported color names
  • -loglevel loglevel, Set the log level
  • -v loglevel, Set the log level
  • -report, Generate a report
  • -max_alloc bytes, Set the maximum size of a single allocated block
  • -cpuflags flags, Appoint cpu sign
  • -hide_banner hide_banner, Do not display program banners
  • -sources device, List the output devices of the source
  • -sinks device, List the receivers for the output device
  • -f format, Specify the format
  • -unit, Display the unit of the displayed value
  • -prefix, Use SI Prefix
  • -byte_binary_prefix, Use binary prefixes for byte units
  • -sexagesimal, Use hexadecimal format for time units HOURS:MM:SS.MICROSECONDS
  • -pretty, Beautify the output value , Readable

  • -print_format format, Set print format (available formats are: default, compact, csv, flat, ini, json, xml)
  • -of format, -print_format The coding

  • -select_streams stream_specifier, Select specified stream
  • -sections, Print section structure and section information , And then quit

  • -show_data, Display packet information
  • -show_data_hash, Display packets hash value
  • -show_error, Show errors in the probe
  • -show_format, Display format / Container information
  • -show_frames, Display frame information
  • -show_format_entry entry, Display format / Specific entries in the container information
  • -show_entries entry_list, Displays a set of specified items
  • -show_log, Show log
  • -show_packets, Show packet Information
  • -show_programs , Display program information
  • -show_streams, Show stream Information about
  • -show_chapters, Show chapters Information about
  • -count_frames, Every stream Number of frames in
  • -count_packets, Every stream Number of packages in

  • -show_program_version, ffprobe Version of
  • -show_library_versions, Version of the library
  • -show_versions, Version numbers of programs and libraries

  • -show_pixel_formats, Display pixel format description
  • -show_private_data, Display private data
  • -private, Just like displaying private data
  • -bitexact, Force extraction bit Output
  • -read_intervals read_intervals, Set the read interval
  • -default, Default to all options
  • -i input_file, Read the specified file
  • -print_filename print_file, Redisplay the entered file name
  • -find_stream_info, Read and decode the stream , Fill in the missing information with heuristic methods

Examples of use

Basic use

#  View audio and video file information 
ffprobe  file name 

#  View the output format information of the file , Length of time , file size , Bit rate , Number of streams, etc .
ffprobe -show_format  file name 

#  With json Format output   flow   Information 
ffprobe -print_format json -show_streams  file name 

#  Display frame information 
ffprobe -show_frames  file name 

#  View package information  
ffprobe -show_packets  file name 

Output format

Support formats :xml、ini、json、csv、flat

ffprobe -show_format test.mp4 -print_format json

ffprobe -show_frames -of json input.flv


frame Frame information

 #  View the total frames of video 
ffprobe -v error -count_frames -select_streams v:0 -show_entries stream=nb_frames -of default=nokey=1:noprint_wrappers=1 gemfield.mp4


ffprobe -select_streams v -of xml -show_entries frame=pkt_pts,pkt_dts,pkt_size,pkt_duration,pict_type a.mp4

#  Check the total number of audio frames 
ffprobe -v error -count_frames -select_streams a:0 -show_entries stream=nb_read_frames -of default=nokey=1:noprint_wrappers=1

ffprobe -v error -count_frames -select_streams v:0 -show_entries stream=nb_read_frames -of default=nokey=1:noprint_wrappers=1 -skip_frame nokey gemfield.mp4 

#  see  key frame  At the time 
ffprobe -v error -skip_frame nokey -select_streams v:0 -show_entries frame=pkt_pts_time -of csv=print_section=0 gemfield.mp4 

#  see  key frame  Distribution 
ffprobe -v error -show_frames gemfield.mp4 | grep pict_type

#  see  key frame  Number of frames  
ffprobe -v error -select_streams v -show_frames -show_entries frame=pict_type -of csv gemfield.mp4 | grep -n I | cut -d ':' -f 1

#  To reset  key frame interval 
ffmpeg -i gemfield.mp4 -vcodec libx264 -x264-params keyint=1:scenecut=0 -acodec copy out.mp4


#  Check the video baud rate  
ffprobe -v error -select_streams v:0 -show_entries stream=bit_rate -of default=noprint_wrappers=1:nokey=1 gemfield.mp4

#  Only view audio (a)、 video (v)、 subtitle (s) Information about 
ffprobe -show_frames -select_streams v -of json input.mp4
ffprobe -show_frames -select_streams a -of json input.mp4

# packet  Data packets  
ffprobe -show_packets video.mp4

show_packets View the multimedia package information using PACKET Included in the label , The information contained therein is mainly as follows :

  • codec_type: Multimedia type , For example, video package , Audio package, etc ;
  • stream_index: Multimedia stream Indexes ;
  • pts: Display time value of multimedia
  • pts_time: Calculate the display time of the multimedia according to different formats
  • dts: Multimedia decoding time value
  • dts_time: Calculate the multimedia decoding time according to different formats
  • duration: Time value of multimedia package
  • duration_time: Calculate the time occupied by the multimedia package according to different formats
  • size: Size of multimedia package
  • pos: The file offset of the multimedia package
  • flags: Multimedia package tags , Marking of critical and non critical packages

ffmpeg

ffmpeg [global options] {
    [infile options]['-i' 'infile'] ...} {
    [outfile options] 'outfile' ...}

Parameter options consist of three parts : An optional set of global parameters 、 One or more sets of input file parameters 、 One or more sets of output file parameters .
among , Each group of input file parameters is represented by -i Mark for the end ;
Each set of output file parameters is marked with the output file name .


ffmepg Transcoding process in , Here's the picture :

 _______              ______________
|       |            |              |
| input |  demuxer   | encoded data |   decoder
| file  | ---------> | packets      | -----+
|_______|            |______________|      |
                                           v
                                       _________
                                      |         |
                                      | decoded |
                                      | frames  |
                                      |_________|
 ________             ______________       |
|        |           |              |      |
| output | <-------- | encoded data | <----+
| file   |   muxer   | packets      |   encoder
|________|           |______________|



Filtering

Simple filtergraphs

 _________                        ______________
|         |                      |              |
| decoded |                      | encoded data |
| frames  |\                   _ | packets      |
|_________| \                  /||______________|
             \   __________   /
  simple     _\||          | /  encoder
  filtergraph   | filtered |/
                | frames   |
                |__________|


 _______        _____________        _______        ________
|       |      |             |      |       |      |        |
| input | ---> | deinterlace | ---> | scale | ---> | output |
|_______|      |_____________|      |_______|      |________|


Complex filtergraphs

 _________
|         |
| input 0 |\                    __________
|_________| \                  |          |
             \   _________    /| output 0 |
              \ |         |  / |__________|
 _________     \| complex | /
|         |     |         |/
| input 1 |---->| filter  |\
|_________|     |         | \   __________
               /| graph   |  \ |          |
              / |         |   \| output 1 |
 _________   /  |_________|    |__________|
|         | /
| input 2 |/
|_________|


Command specification

The basic options

Support capability

  • -formats, List supported file formats .
  • -codecs, List the codecs supported .
  • -decoders, List the supported decoders .
  • -encoders, List supported encoders .
  • -protocols, List supported protocols .
  • -bsfs, List the supported bitstream filters .
  • -filters, List the supported filters .
  • -pix_fmts, List supported image sampling formats .
  • -sample_fmts, List the supported sound sampling formats .

Common input options

  • -i filename, Specify the input file name .
  • -f fmt, Force file format , You need to use the name in the capability set list ( The default is based on the extension ).
  • -ss hh:mm:ss[.xxx], Set the starting time of the input file , After starting, it will jump to this point in time and start reading data .
  • -y, Overwrite existing files .
  • -t duration, Designated time length .
  • -fs limit_size, Set the maximum file size .
  • -ss limit_size, From the appointed time ( The unit is in seconds ) Start , Also support [-]hh:mm:ss[.xxx] Format
  • -re, Represents sending at frame rate . When you are a streaming tool, you must add this parameter , otherwise ffmpeg It will continuously send data to the streaming media server at the highest rate .
  • -map, Specify the stream mapping relationship of the output file . without -map Options , be ffmpeg Use the default mapping relationship .

For input , The following options are usually automatically recognized , But it can also be set by force .

  • -c codec, Specified decoder , You need to use the name in the capability set list .
  • -acodec codec, Specify the decoder of the sound , You need to use the name in the capability set list .
  • -vcodec codec, Specify the decoder of the video , You need to use the name in the capability set list .
  • -b:v bitrate, Set the bit rate of the video stream , Integers , Company bps.
  • -r fps, Set the frame rate of the video stream , Integers , Company fps.
  • -s WxH, Set the picture size of the video . It can also be achieved by attaching the image zoom filter .
  • -pix_fmt format, Set the image format of the video stream ( Such as RGB still YUV).
  • -ar sample rate, Set the sampling rate of the audio stream , Integers , Company Hz.
  • -ab bitrate, Set the bit rate of the audio stream , Integers , Company bps.
  • -ac channels, Set the number of channels of the audio stream .

Common output options

  • -f fmt, Force file format , You need to use the name in the capability set list ( The default is based on the extension ).
  • -c codec, Specify encoder , You need to use the name in the capability set list ( The encoder is set to ”copy“ Means no encoding and decoding ).
  • -acodec codec, Specify the encoder of the sound , You need to use the name in the capability set list ( The encoder is set to ”copy“ Means no encoding and decoding ).
  • -vcodec codec, Specify the encoder of the video , You need to use the name in the capability set list ( The codec is set to ”copy“ Means no encoding and decoding ).
  • -r fps, Set the frame rate of the video encoder , Integers , Company fps.
  • -pix_fmt format, Set the image format used by the video encoder ( Such as RGB still YUV).
  • -ar sample rate, Set the sampling rate of the audio encoder , Integers , Company Hz.
  • -b bitrate, Set the bit rate output by the audio and video encoder , Integers , Company bps.
  • -ab bitrate, Set the bit rate of audio encoder output , Integers , Company bps.
  • -ac channels, Set the number of channels of the audio encoder .
  • -an, Ignore any audio streams .
  • -vn, Ignore any video streams .
  • -t hh:mm:ss[.xxx], Set the length of time for the output file .
  • -to hh:mm:ss[.xxx], If the time length of the output file is not set, the end time point can be set .

Stream identification

FFMPEG Some of the options of can work for a specific media stream , In this case, you need to add a flow ID after the option . Stream identification allows the following formats `,

  • Stream number . for example “:1” Represents the second stream .
  • Stream type . for example “:a“ Represents an audio stream , Flow type can be combined with flow sequence number , for example “️1” Represents the second audio stream .
  • show . Program and stream serial numbers can be combined .
  • Stream identification . The flow ID is an internal ID number .

If you want to set the second audio stream to copy, You need to specify -codec️1 copy


Audio options

  • -ab, Set bit rate ( Unit is bit/s).
  • -aq, Equivalent to q:a, The old version is qscale:a, Used to set the audio quality .
  • -aq quality, Set the audio quality ( Specified encoding ).
  • -ar rate, Set the audio sampling rate ( Unit is Hz).
  • -ac channels, Set the number of channels .1 It's mono ,2 It's stereo .
  • -an, Cancel audio track .
  • -acodec codec, Specify audio encoding ('copy’ Means no audio transcoding ).
  • -aframes, Equivalent to frames:a, Output options , Used to specify the number of audio frames to output .
  • -atag, Equivalent to tag:a, Used to set the label of the audio stream .
  • -af, Equivalent to filter:a, Used to set the post-processing filter chain of a sound , Its parameter is a string describing the sound post-processing chain .
  • -vol volume, Set the recording volume ( Default 256)< percentage >.

Video options

  • -b, Specify the bit rate (bit/s),ffmpeg It's automatic use VBR Of , If this parameter is specified, the average bit rate is used .
  • -bitexact, Using standard bit rates .
  • -vb, Specifies the video bit rate (bit/s).
  • -r rate, Frame rate (fps).
  • -s size, Specified resolution (320 * 320)
  • -aspect aspect, Set the video aspect ratio (4:3,16:9 perhaps 1.3333,1.7777).
  • -croptop size, Set the top cut size (in pixels).
  • -cropbottom size, Set the cutting size of top and bottom (in pixels).
  • -cropleft size, Set the left cut size (in pixels).
  • -cropright size, Set the right cut size (in pixels).
  • -padtop size, Set top fill size (in pixels).
  • -padbottom size, Set the bottom fill size (in pixels).
  • -padleft size, Set left fill size (in pixels).
  • -padright size, Set the right fill size (in pixels).
  • -padcolor color, Complement with color (000000-FFFFFF).
  • -vn, Cancel video output .
  • -vcode codec, Compulsory use codec Codec mode ( 'copy’ Means no recoding ).
  • -vframes, Equivalent to frames:v, Output options , Used to specify the number of output video frames .
  • -aspect, Set the aspect ratio , Such as 4:3、16:9、1.3333、1.7777 etc. .
  • -bits_per_raw_sample, Set the number of bits per pixel .
  • -vstats, produce video Statistics .
  • -vf, Equivalent to filter:v, Used to set the post-processing filter chain of an image , Its parameter is a string describing the image post-processing chain .
  • -vtag, Equivalent to tag:v, Used to set the label of the video stream .
  • -force_fps, Force the video frame rate .
  • -force_key_frames, Explicitly control the insertion of keyframes , Parameter is a string , It can be a timestamp , It can also be a expr: The expression of the prefix . Such as -force_key_frames 0:05:00-force_key_frames expr:gte(t,n_forced*5)

Advanced options

  • -re, Require input data to be processed at a given rate , This rate is the frame rate of the input file .
  • -map, Specify the stream mapping relationship of the output file . for example “-map 1:0 -map 1:1” It is required to write the first stream and the second stream of the second input file to the output file . without -map Options ,ffmpeg Use the default mapping relationship .

Examples of use

View support information

#  see FFmpeg Supported encoders 
ffmpeg configure -encoders
 
#  see FFmpeg Supported decoders 
ffmpeg configure -decoders
 
#  see FFmpeg Supported communication protocols  
ffmpeg configure -protocols


#  see FFmpeg Supported audio and video coding formats 、 File encapsulation format and streaming media transfer protocol 
ffmpeg configure --help

#  List ffmpeg All supported formats 
ffmpeg -formats
#  An old-fashioned avi Document conversion mp4
ffmpeg -i final.avi -acodec copy -vcodec copy final.mp4 

#  Extract a frame of image from a video file 
ffmpeg -y -i test.mp4 -ss 00:03:22.000 -vframes 1 -an test.jpg

ffmpeg -i final.avi -vf scale=640:640 square.avi  

4、

#  Use alsa The interface records an audio and stores it in a wav In file 
ffmpeg -f alsa -i hw:0 -t 100 alsaout.wav


#  Use alsa Interface to build a personal network radio 
ffmpeg -f alsa -i default -acodec aac -strict -2 -b:a 128k -r 44100 /var/www/data/main.m3u8

#  Will a mp4 The audio and video stream of the file is transcoded in real time and sent to a remote device , Remote devices can be accessed via http Acquired sdp File to receive rtp Media data .
ffmpeg -re -i example.mp4 -acodec copy -vcodec libx264 -s 480x270 -map 0:0 -f rtp rtp://10.131.202.62:1234 -map 0:1 -f rtp rtp://10.131.202.62:1238 > /var/www/live.sdp


Operation video

Set the screen aspect ratio of the video

ffmpeg -i input.mp4 -aspect 16:9 output.mp4 

The commonly used aspect ratio is :
16:9
4:3
16:10
5:4
2:21:1
2:35:1
2:39:1


Encoding format conversion

MPEG4 Code to H264 code

ffmpeg -i input.mp4 -strict -2 -vcodec h264 output.mp4

H264 Code to MPEG4 code

ffmpeg -i input.mp4 -strict -2 -vcodec mpeg4 output.mp4

video compression

ffmpeg -i 2020.mp4 -vcodec h264 -vf scale=640:-2 -threads 4 2020_conv.mp4
 
ffmpeg -i 1579251906.mp4 -strict -2 -vcodec h264 1579251906_output.mp4

Parameter interpretation :

  • -i 2020.mp4, Input file , Source file
  • 2020_conv.mp4, The output file , Target file
  • -vf scale=640:-2, Change the video resolution , Zoom to 640px wide , High -2 Taking into account libx264 The required height is even , So set it to -2, Let the software automatically calculate an even number high close to equal proportion
  • -threads 4, 4 Kernel operation

The other parameters :

  • -s 1280x720 Set the resolution of the output file ,w*h.
  • -b:v, Bit rate of output file , commonly 500k Just around , The human eye cannot see the obvious flicker , This is the most directly related to the video size .
  • -preset, For specific types of source content ( Like movies 、 Animation, etc. ), You can also use -tune Parameters are specially optimized .
    Specifies the video quality of the output , Will affect the file generation speed , There are several values available ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow.
    And veryslow comparison ,placebo At the cost of extremely high coding time , Only about 1% Video quality improvement . This is a rule of diminishing returns :slow And medium Compared with 5%~10%;slower And slow Compared with 5%;veryslow And slower Compared with 3%.
  • -an, Remove the audio stream .
  • -vn, Remove the video stream .
  • -c:a, Specify audio encoder .
  • -c:v, Specify video encoder ,libx264,libx265,H.262,H.264,H.265.
    • libx264: The most popular open source H.264 Encoder .
    • NVENC: be based on NVIDIA GPU Of H.264 Encoder .
    • libx265: Open source HEVC Encoder .
    • libvpx: Google's VP8 and VP9 Encoder .
    • libaom:AV1 Encoder .
  • -vcodec copy, No recoding , If the format is unchanged, use .
  • -re, Send data at the inherent frame rate of the source file .
  • -minrate 964K -maxrate 3856K -bufsize 2000K, The minimum specified code rate is 964K, The maximum is 3856K, The buffer size is 2000K.
  • -y, Without confirmation , Directly overwrite the file with the same name when exporting .
  • -crf, Parameters to control transcoding , The value range is 051, among 0 Lossless mode ,1828 It's a reasonable range , The greater the numerical , The worse the picture quality .

Video splicing

#  take 4 Videos are spliced into a long video ( No sound )
ffmpeg -i 0.mp4 -i 1.mp4 -i 2.mp4 -i 3.mp4 -filter_complex '[0:0][1:0] [2:0][3:0] concat=n=4:v=1 [v]' -map '[v]' output.mp4

#  take 4 Videos are spliced into a long video ( There's a voice )
ffmpeg -i 1.mp4 -i 2.mp4 -i 3.mp4 -filter_complex '[0:0][0:1] [1:0][1:1] [2:0][2:1] concat=n=3:v=1:a=1 [v][a]' -map '[v]' -map '[a]'  output.mp4

Parameter interpretation :

  • [0:0][0:1] [1:0][1:1] [2:0][2:1], Separate indication control 1 Videos of input files 、 Audio , The first 2 Videos of input files 、 Audio , The first 3 Videos of input files 、 Audio .
  • concat=n=3:v=1:a=1, Express 3 An input file , Output a video stream and an audio stream .
  • [v][a], Get the names of the video stream and audio stream , Pay attention to bash etc. shell Quotation marks are required in , Prevent wildcard expansion .

Horizontal splicing 2 A video

ffmpeg -i 0.mp4 -i 1.mp4 -filter_complex "[0:v]pad=iw*2:ih*1[a];[a][1:v]overlay=w" out.mp4

Parameter interpretation :

  • pad The width and height of the synthesized video , here iw On behalf of the 1 The width of a video ,iw*2 Represents double the width of the synthesized video ,ih For the first time 1 Video height , The two videos synthesized should have the same resolution .
  • overlay Cover ,[a][1:v]overlay=w, The following represents the coverage position w:0.

Vertical splicing 2 A video

ffmpeg -i 0.mp4 -i 1.mp4 -filter_complex "[0:v]pad=iw:ih*2[a];[a][1:v]overlay=0:h" out_2.mp4

Horizontal splicing 3 A video

ffmpeg -i 0.mp4 -i 1.mp4 -i 2.mp4 -filter_complex "[0:v]pad=iw*3:ih*1[a];[a][1:v]overlay=w[b];[b][2:v]overlay=2.0*w" out_v3.mp4

Vertical splicing 3 A video

ffmpeg -i 0.mp4 -i 1.mp4 -i 2.mp4 -filter_complex "[0:v]pad=iw:ih*3[a];[a][1:v]overlay=0:h[b];[b][2:v]overlay=0:2.0*h" out_v4.mp4

4 A video 2x2 Mode arrangement

ffmpeg -i 0.mp4 -i 1.mp4 -i 2.mp4 -i 3.mp4 -filter_complex "[0:v]pad=iw*2:ih*2[a];[a][1:v]overlay=w[b];[b][2:v]overlay=0:h[c];[c][3:v]overlay=w:h" out.mp4

Cut video

ffmpeg -i input.mp4 -ss 00:00:50.0 -codec copy -t 20 output.mp4

  • Indicates that the file will be saved input.mp4 From 50s Start cutting 20s Time for ,
  • output to a file output.mp4 in ,
  • among -ss Specify the offset time ( time Offset) ,
  • -t A specified length of time ( duration)

Video frame operation

ffmpeg and H264 Video codec

#  View the information of each frame 
ffprobe -v error -show_frames gemfield.mp4 

from pict_type=I It can be seen that this is a key frame , then key_frame=1 It means that this is IDR frame, If key_frame=0 It means that this is Non-IDR frame.


Capture a frame in the video

hold gemfield.mp4 The first part of the video 1 branch 05 Seconds of an image is intercepted .


input seeking

ffmpeg -ss 00:1:05 -i gemfield.mp4 -frames:v 1 out.jpg

output seeking

ffmpeg -i gemfield.mp4 -ss 00:1:05 -frames:v 1 out1.jpg

Parameter interpretation :

  • -frame:v 1, stay video stream Upper interception 1 frame .
    input seeking It uses key frames, So it's very fast ;
    and output seeking It's frame by frame decode, until 1 branch 05 second , So it's very slow .

Important note :

ffmpeg There are 2 Kind of seeking The way , The due 2 Kind of coding Pattern :transcoding and stream copying(ffmpeg -c copy).

transcoding Pattern : need decoding + encoding The pattern of , First of all decoding Again encoding.

stream copying Pattern : Unwanted decoding + encoding The pattern of , By command line options -codec Add parameters copy To specify the (-c:v copy ). In this mode ,ffmpeg stay video stream Will ignore decoding and encoding step .


Pictures and videos

Picture to video ( The name of the rule )

ffmpeg -f image2 -i 'in%6d.jpg' -vcodec libx264 -r 25 -b 200k test.mp4

Parameter interpretation :

  • -r 25, Means play every second 25 frame
  • -b 200k, The specified code rate is 200k

The file name of the picture is "in000000.jpg", from 0 Start increasing in sequence .


Picture to video ( Irregular names )

Irregular picture name to video .


Method 1

Irregular picture name synthetic video file .

ffmpeg -framerate 10 -pattern_type glob -i '*.jpg' out.mp4

cat *.png | ffmpeg -f image2pipe -i - output.mp4

Parameter interpretation :

  • -framerate 10: Video frame rate
  • -pattern_type glob:Glob pattern- Fuzzy matching
  • -f image2pipe- : Image pipeline , Fuzzy matching gets the picture name

Method 2

Irregular picture name synthetic video file .
First rename the irregular file to the regular picture name .

def getTpyeFile(filelist, type):     
    res = []     
    for item in filelist:
         name, suf = os.path.splitext(item) #  file name , suffix 
         if suf == type:
             res.append(item)
     return res

pwd = os.getcwd() #  Return the absolute path of the current directory 
dirs = os.listdir() #  An array of all file names in the current directory 
typefiles = getTpyeFile(dirs, '.jpg')

for i in range(0,len(typefiles)):
     os.rename(typefiles[i],"./%d.jpg" % (i)) # Command the file as a number rule 

Put the image to be synthesized in txt in , By reading the txt Merge files into videos .

ffmpeg -f concat -i files.txt output.mp4

Picture format conversion

# webp convert to jpg
ffmpeg -i in.webp out.jpg


# webp convert to png
ffmpeg -i in.webp out.png


# jpg convert to png 
ffmpeg -i in.jpg out.png

# jpg convert to webp 
ffmpeg -i in.jpg out.webp


# png convert to webp 
ffmpeg -i in.png out.webp

# png convert to jpg 
ffmpeg -i in.png out.jpg

If you record a video on your mobile phone for a long time, you can't share it on wechat , Then you can use ffmpeg Cut the video file into multiple files

ffmpeg -i input.mp4 -t 00:00:50 -c copy small-1.mp4 -ss 00:00:50 -codec copy small-2.mp4

#  Mute the audio in a video ,  That is, only keep the video 
ffmpeg -i input.mp4 -an -vcodec copy output.mp4

#  from MP4 Extract the video stream from the file and export it as raw H264 data 
ffmpeg -i output.mp4 -an -vcodec copy -bsf:v h264_mp4toannexb output.h264
  • Video data usage mp4toannexb This bitstream filter To convert to the original H264 data ,

#  Use AAC Audio data and H264 Video generation MP4 file 
ffmpeg -i test.aac -i test.h264 -acodec copy -bsf:a aac_adtstoasc -vcodec copy -f mp4 output.mp4

7、

#  Convert the encoding format of audio files 
ffmpeg -i input.wav -acodec libfdk_aac output.aac

#  from WAV Export from audio file PCM Raw data 
ffmpeg -i input.wav -acodec pcm_s16le -f s16le output.pcm
  • In this way, you can export 16 individual bit To represent a sample Of PCM Data. ,
  • And each sample The byte order of is the format represented by the small tail ,
  • The number of channels and sampling rate are original WAV Number of channels and sampling rate of the file PCM data .

#  Re encode the video file ,  Copy audio stream ,  At the same time, it is encapsulated into MP4 Format file 
ffmpeg -i input.flv -vcodec libx264 -acodec copy output.mp4

#  Will a MP4 Format video is converted into gif Dynamic graph of format 
ffmpeg -i input.mp4 -vf scale=100:-1 -t 5 -r 10 image.gif
  • The fixed width of the above code is changed to 100( Use VideoFilter Of scaleFilter) ,
  • The frame rate is changed to 10(-r), Just before handling 5 Second (-t) In the video , Generate gif

#  Generate pictures from the picture part of a video , It can be used to analyze the content of each frame in a video 
ffmpeg -i output.mp4 -r 0.25 frames_%04d.png

#  Using a group of pictures can form a gif
ffmpeg -i frames_%04d.png -r 5 output.gif

#  Use volume effector , You can change the volume in an audio media file 
ffmpeg -i input.wav -af ‘volume=0.5’ output.wav
  • The above order is to input.wav The sound in is reduced by half , Output to output.wav In file ,
  • You can listen to it directly , Or put it into some audio editing software to directly watch the effect of waveform amplitude

#  Use of fade in effector 
ffmpeg -i input.wav -filter_complex afade=t=in:ss=0:d=5 output.wav
  • The above command can change input.wav Before in the document 5s Make a fade in effect , Output to output.wav in ,
  • You can drag the files before and after processing to Audacity Check the waveform diagram in the audio editing software

#  Use of fade out effector 
ffmpeg -i input.wav -filter_complex afade=t=out:st=200:d=5 output.wav
  • The above command can change input.wav File from the 200s Start , do 5s The fade out effect of , And on the output.wav In file

#  Merge the two sounds ,  For example, add background music to a sound 
ffmpeg -i vocal.wav -i accompany.wav -filter_complex amix=inputs=2:duration=shortest output.wav
  • The above order is to vocal.wav and accompany.wav Two files mix,
  • The length of the audio file with a shorter length of time is taken as the final output output.wav Length of time .

#  The use of an effect device that changes the speed of sound without changing the tone 
ffmpeg -i vocal.wav -filter_complex atempo=0.5 output.wav
  • The above order is to vocal.wav according to 0.5 Times the speed of processing to generate output.wav,
  • The length of time will become the entered 2 times . But the pitch is constant , This is what we often call variable speed without changing tone

#  Add watermark effect to the video 
ffmpeg -i input.mp4 -i changba_icon.png -filter_complex 
    '[0:v][1:v]overlay=main_w-overlay_w-10:10:1[out]' -map '[out]' output.mp4
  • The above command contains several built-in parameters ,
  • main_w Represents the width of the main video ,
  • overlay_w Represents the width of the watermark ,
  • main_h Represents the height of the main video ,
  • overlay_h Represents the watermark height

#  Use of video brightener 
ffmpeg -i input.flv -c:v libx264 -b:v 800k  -c:a libfdk_aac -vf eq=brightness=0.25
        -f mp4 output.mp4
  • The highlight parameter is brightness, The value range is from -1.0 To 1.0, The default value is 0

#  Add contrast to the video 
ffmpeg -i input.flv -c:v libx264 -b:v 800k  -c:a libfdk_aac -vf eq=contrast=1.5 -f mp4 output.mp4
  • The contrast parameter is contrast, The value range is from -2.0 To 2.0, The default value is 1.0

#  Use of video rotation effector 
ffmpeg -i input.mp4 -vf "transpose=1" -b:v 600k output.mp4

#  The use of video clipping effect device 
ffmpeg -i input.mp4 -an -vf "crop=240:480:120:0" -vcodec libx264 -b:v 600k output.mp4

#  Put a RGBA The data represented by the format is converted to JPEG Format picture 
ffmpeg -f rawvideo -pix_fmt rgba -s 480*480 -i texture.rgb 
    -f image2 -vcodec mjpeg output.jpg

23、

#  Will a YUV The data represented by the format is converted to JPEG Format picture 
ffmpeg -f rawvideo -pix_fmt yuv420p -s 480*480 -i texture.yuv 
    -f image2 -vcodec mjpeg output.jpg

24、

#  Push a video to the streaming media server 
ffmpeg -re -i input.mp4 -acodec copy -vcodec copy -f flv rtmp://xxx
  • In the above code , rtmp: //xxx Represents the address of the streaming media server ,
  • add -re The parameter represents that the playback speed of the actual media file is pushed as the streaming speed

#  Stream the stream on the streaming media server dump To local 

ffmpeg -i http://xxx/xxx.flv -acodec copy -vcodec copy -f flv output.flv
  • In the above code , http://xxx/xxx.flv Represents an accessible video network address ,
  • You can copy the video stream format and audio stream format , Download the file to the local output.flv In the media file

Encapsulate two audio files into one file in the form of two streams ,

  • For example K In the application scenario of song , In the scene of real-time switching of the original backup singer ,
  • You can use a file that contains two streams , All the way is the accompaniment flow , The other way is the original singing stream
ffmpeg -i 131.mp3 -i 134.mp3 -map 0:a -c:a:0 libfdk_aac -b:a:0 96k -map 1:a -c:a:1
        libfdk_aac -b:a:1 64k -vn -f mp4 output.m4a

ffmpeg -formats
 List ffmpeg All supported formats 

ffmpeg -i  Input file  -ss 00:00:50.0 -codec copy -t 20  Output file name 
-i  Specify input file 
-ss  From the specified time 
-codec  Compulsory use codec Codec mode  copy Means no recoding 
-t  Designated time length   second 
 Cut a piece of media file 

ffmpeg -i input.mp4 -t 00:00:50 -c copy small-1.mp4 -ss 00:00:50 -codec copy small-2.mp4
 Cut a long-time video file into multiple files 

ffmpeg -i input.mp4 -vn -acodec copy output.m4a
-vn  Cancel video output  -acodec  Specify audio encoding  copy Means no recoding 
 Extract an audio file from a video file 

ffmpeg -i input.mp4 -an -vcodec copy output.mp4
-an  Cancel audio output  -vcodec  Specify video encoding  copy Means no recoding 
 Mute the audio in a video , Keep only videos 

ffmpeg -i output.mp4 -an -vcodec copy -bsf:v h264_mp4toannexb output.h264
 Video data usage h264_mp4toannexb This bitstream filter To convert to the original H264 data .
 from mp4 Extract the video stream from the file and export it as raw H264 data 

ffmpeg -i test.aac -i test.h264 -acodec copy -bsf:a aac_adtstoasc -vcodec copy -f mp4 output.mp4
-f  Specify the output format 
 Use aac Audio data and H264 Video generation MP4 file 

ffmpeg -i input.wav -acodec libfdk_aac output.aac
 Convert the encoding format of audio files 

ffmpeg -i input.wav -acodec pcm_s16le -f s16le output.pcm
 from wav Export from audio file pcm Raw data 

ffmpeg -i input.flv -vcodec libx264 -acodec copy output.mp4
 Re encode the video file , Copy audio stream   At the same time, it is encapsulated into MP4 Format file 

ffmpeg -i input.mp4 -vf scale=100:-1 -t 5 -r 10 image.gif
-vf Set the video filter 
 According to the resolution ratio, the fixed width is changed to 100( Use videofilter Of scalefilter), The frame rate is changed to 10(-r) The duration is changed to 5(-t)
 Will a MP4 Format video is converted to gif Dynamic graph of format 

ffmpeg -i input.mp4 -r 0.25 frames_%04d.png
 Every time 4  Seconds to intercept a frame of video to generate a picture , The generated pictures are from frames_0001.png Began to increase 

ffmpeg -i frames_%04d.png -r 5 output.gif
 Using a group of pictures can form a gif

ffmpeg -i input.wav -af ‘volume=0.5’ output.wav
 Use volume effector , Change the volume in an audio media file 

ffmpeg -i input.wav -filter_complex afade=t=in:ss=0:d=5 output.wav
 Put the audio file in front of 5 Seconds to make a fade effect 

ffmpeg -i input.wav -filter_complex afade=t=out:st=200:d=5 output.wav
 Turn the audio from 200s Start   do 5 Second fade effect 

ffmpeg -i input1.wav -i input2.wav -filter_complex amix=inputs=2:duration=shortest output.wav
 Merge the two audio , Take the shorter audio time as the output 

ffmpeg -i input.wav -filter_complex atempo=0.5 output.wav
 Press the audio as 0.5 Times the speed of processing to generate output.wav  The length of time becomes the original 2 times , The pitch does not change 

ffmpeg -i test.avi -i frames_0004.jpeg -filter_complex overlay after.avi
 Watermark video 

ffmpeg -i test.avi -vf"drawtext=fontfile=simhei.ttf:text=‘ Thunder ’:x=100:y=10:fontsize=24:fontcolor=yellow:shadowy=2" after.avi
 Add text watermark 

ffmpeg -i input.flv -c:v libx264 -b:v 800k -c:a libfdk_aac -vf eq=brightness=0.25 -f mp4 output.mp4
 Video improves brightness   Parameters brightness  Value range -1.0 To 1.0.

ffmpeg -i input.flv -c:v libx264 -b:v 800k -c:a libfdk_aac -vf eq=contrast=1.5 -f mp4 output.mp4
 Video increases contrast   Parameters contrast, The value range is from -2.0 To 2.0

ffmpeg -i input.mp4 -vf “transpose=1” -b:v 600k output.mp4
 The video rotation effector uses 

ffmpeg -i input.mp4 -an -vf “crop=240:480:120:0” -vcodec libx264 -b:v 600k output.mp4
 The video clipping effect device uses 

ffmpeg -f rawvideo -pix_fmt rgba -s 480*480 -i texture.rgb -f image2 -vcodec mjpeg output.jpg
 Put a RGBA The data represented by the format is converted to JPEG Format picture 

ffmpeg -f rawvideo -pix_fmt yuv420p -s 480*480 -i texture.yuv -f image2 -vcodec mjpeg output.jpg
 Will a YUV The data represented by the format is converted to JPEG Format picture 

ffmpeg -re -i ipnut.mp4 -acodec copy -vcodec copy -f flv rtmp://xxx
 Push a video to the streaming media server 

ffmpeg -i http://xxx/xxx.flv -acodec copy -vcodec copy -f flv output.flv
 Stream the stream on the streaming media server dump To local 


Hard decoding and soft decoding

CPU Surplus 、 The decoding process needs to be accurately controlled 、 Optimization of decoding algorithm 、 High universality requirements , Use soft solutions directly ( That is to say CPU decode );
There are other codec chips / module 、CPU Not enough use , You have to turn to hard decoding ( That is, special chip decoding ).


Break down and reuse commands

ffplay animals.mp4

ffmpeg -i animals.mp4 -vcodec copy -acodec copy animal.mov   format conversion , take mp4 Turn into mov
ffplay animals.mov


ffmpeg -i animal.mov -an -vcodec copy animal.h264   take mov Extract the video , Remove audio 
ffplay animals.h264	 It can be played directly h264


ffmpeg -i animal.mov -acodec copy -vn animal.aac  Extract audio 



Commands for processing raw data

 in the light of mp4 Format of video files , No audio , The video coding format is rawvideo, The pixel format is yuv420p, The output is yuv Format 
ffmpeg -i animals.mp4 -an -c:v rawvideo -pix_fmt yuv420p animal.yuv 

ffplay -s 720x1280 animal.yuv  Be sure to increase the resolution when playing 



 Don't video , Just audio , Audio sampling rate  44100, The channel number  2,-f Represents the audio storage format ,s It means positive and negative , For each value, use 16 Who said ,l Express little,e Express end, Small end storage 
ffmpeg -i animals.mp4 -vn -ar 44100 -ac 2 -f s16le animal.pcm

ffplay -ar 44100 -ac 2 -f s16le animal.pcm  Be sure to add parameters when playing 



Filter command

Video picture size clipping

-vf Indicates that there is a video filter behind , Use here crop Filter ,crop The parameter is :in_w Is the width of its own video , subtract 200,in_h-200 Video height minus 200
 Video encoder libx264, The audio encoder adopts copy The pattern of , Indicates that the audio is not processed 
ffmpeg -i animal.mov -vf crop=in_w-200:in_h-200 -c:v libx264 -c:a copy animal_crop.mp4
ffmpeg -i animal.mov -vf crop=in_w-200:in_h-200 -vcodec libx264 -acodec copy animal_crop.mp4  Exactly the same as above , Use -vcodec Instead of -c:v



Crop and merge commands

 Video clip :
 From 0 Hours , The first 0 minute , The first 20s Start , tailoring 10s In the video , The output format is ts
ffmpeg -i animals.mp4 -ss 00:00:20 -t 10 animal_10s.ts


 Video splicing 
 take txt All video files in the file are spliced into one file 
ffmpeg -f concat -i inputs.txt animal_concat.mp4 

 among inputs.txt:
file 'animal_10s.ts'
file 'animal_5s.ts'



Picture and video conversion command

 Video to picture 
-r Specify the frame rate of the converted picture , Every second 1 frame 
-f tell ffmpeg, I want to output the picture as image2 Format 
 Finally, the naming format 
ffmpeg -i animals.mp4 -r 1 -f image2 images/image-%3d.jpeg   Here we should establish an empty one in advance images Folder 
 obtain :
image-001.jpeg	image-002.jpeg	image-003.jpeg	image-004.jpeg	image-005.jpeg	image-006.jpeg	image-007.jpeg



 Picture to video 
ffmpeg -i images/image-%3d.jpeg animal_images.mp4



Live streaming and streaming

 Push flow 
-re  Slow down the frame rate , Don't try to play as fast as possible 
-c  Indicates the encoding and decoding method including audio and video , All are copy
-f  Indicates the file format pushed , yes flv Format 
ffmpeg -re -i animals.mp4 -c copy -f flv rtmp://server/live/streamName


 Pull flow 
ffmpeg -i rtmp://server/live/streamName -c copy dump.flv  

 It can be used ffplay Go to play Hunan Satellite TV rtmp Address :
ffplay rtmp://58.200.131.2:1935/livetv/hunantv

 And then use ffmpeg Play the same address :
ffmpeg -i rtmp://58.200.131.2:1935/livetv/hunantv -c copy dump.flv  

 Pull in the same way cctv1 In the video , It will be saved in sections :
ffmpeg -i http://ivi.bupt.edu.cn/hls/cctv1hd.m3u8 -c copy dump.m3u8 




2022-07-22( Four )

原网站

版权声明
本文为[Yizhi code]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/204/202207230834040342.html