Use of FFmpeg and FFplay

Usage of FFmpeg and FFplay

Audio and video processing process (1)

ffmpeg command classification query

Command parameters Content
-version Show version
-buildconf Show compilation configuration
-formats Show available formats (muxers + demuxers)
-muxers Show available muxers
-demuxers Show available demultiplexers
-codecs Show available codecs (decoders + encoders )
-decoders Display available decoders
-encoders Show available encoders
-bsfs Show available bitstream filters
-protocols Show available protocols
-filters Show available filters
-pix_fmts Show available pixel formats
-layouts Show standard channel names
-sanmple_fmts Display available audio sample formats
-colors Display available colors Name
# Syntax: ffmpeg -h type=name
# ffmpeg -h muxer=flv
# ffmpeg -h filter=atempo (atempo adjusts the audio playback rate)
# ffmpeg -h encoder=libx264

ffplay playback control

Options Description
q, ESC Exit playback
f Full screen switch
p, SPC Pause
m Mute switch
9, 0 9 decreases the volume, 0 increases the volume
a Cycle switching audio stream
v Cycle switching video stream
t Cycle switching subtitle stream
c Cycle through programs
w Cycle through filters or display modes
s Play frame by frame
left/right Drag backward/forward for 10 seconds
down/up Drag backward/forward for 1 minute
Right click Drag the file corresponding to the percentage of the display width to play
Double-click the left mouse button Switch to full screen

ffplay command-main options

td>

Options Description
-x width Force display width
-y height Force display height
-video_size size Frame size setting displays frame storage (WxH format), only applicable to videos such as original YUV that do not contain frame size (WxH)
-pixel_format format Format settings pixel format
-fs Start in full screen mode
-an Disable audio (do not play sound)
-vn Disable video (do not play video)
-sn Disable subtitles (do not display subtitles)
-ss pos Position and drag according to the set seconds, pay attention to the event unit: such as ’55’ 55seconds,’12:03:45′,12 hours,03 minutes and 45 seconds,’23.189′ 23.189seconds
-t duration Set the playback video/audio length, the time unit is such as -ss option
-bytes Position and drag by bytes (0=off 1=on -1=auto)
-seek_interval interval Custom left/ Right-click dragging interval (in seconds), the default value is 10 seconds
-nodisp Close the graphical display window and the video will not be displayed
-noborder borderless window
-volume vol Set the starting volume. Volume range [0~100]
-f fmt Force to use the set format for parsing. For example -f s16le
-window_title title Set the window title (default is the input file name)
-loop number Set the display mode. Available mode values are: 0 displays video, 1 displays audio waveform, and 2 displays audio spectrum. The default is 0, if the video does not exist, 2 is automatically selected
-vf filtergraph Set video filter
-af filtergraph Set audio filter

ffplay command-advanced options

Options Description
-stats Prints multiple playback statistics, including showing stream duration, codec parameters, current position in the stream, and audio/video sync delta. Enabled by default, to explicitly disable it you need to specify -nostats
-fast Non-standardized multimedia compatibility optimization
-genpts Generate pts
-sync type The synchronization type will be the main The clock is set to audio (type=audio), video (type=video) or external (type=ext). The default is audio as the main clock
-ast audio_stream_specifier Specify the audio stream index, such as -ast 3, play the audio stream with stream index 3
-vst video_stream_specifier Specify the video stream index , for example -vst 4, play the video stream with stream index 4
-sst subtitle_stream_specifier Specify the subtitle stream index, for example -sst 5, play the stream Subtitle stream with index 5
-autoexit Exit after the video is finished playing
-exitonkeydown Press any key on the keyboard to exit playback
-exitonmousedown Press any key on the mouse to exit playback
-codec:media_specifier codec_name Force to use the set multimedia codec. The available values of media_specifier are a (audio), v (video) and s subtitles. For example, codec:v h264_qsv forces the video to use h264_qsv decoding
-acodec codec_name Forces the use of the set audio decoder for audio decoding
-vcodec codec_name Force to use the set video codec for video decoding
-scodec codec_name Force to use the set subtitle decoder for subtitle decoding
-autorotate Automatically rotate the video based on file metadata. Value is 0 or 1, default is 1
-framedrop Drop video frames if the video is out of sync. When the main clock is not the video clock, it is enabled by default. If you want to disable it, use -noframedrop
-infbuf Does not limit the input buffer size. Read as much data as possible from the input as quickly as possible. Enabled by default when playing a live stream, data may be discarded if it is not read in time. This option will not limit the size of the buffer. If you need to disable it, use -noinfbuf

ffplay command playback example

# Play local files
ffplay -window_title "test time" -ss 2 -t 10 -autoexit test.mp4
ffplaybuweishui.mp3
# Play network stream
ffplay -window_title "rtmp stream" rtmp://202.69.69.180:443/webcast/bshdlive-pc
# Force decoder
# mpeg4 decoder:
ffplay -vcodec mpeg4 test.mp4
# h264 decoder:
ffplay -vcodec h264 test.mp4
# Disable audio or video
# Disable audio:
ffplay test.mp4 -an
# Disable video:
ffplay test.mp4 -vn
#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)

ffmpeg command parameter description

# Main parameters:
## -i Set input stream
## -f sets the input format (format)
## -ss start time
## -t time length

# Audio parameters:
## -aframes Set the number of audio frames to output
## -b:a
## -ar sets the sampling rate
## -ac sets the number of channels for sound
## -acodec Set the sound codec. If copy is used, it means that the original codec data must be copied.
## -an does not process audio
## -af audio filter
### ffmpeg -i test.mp4 -b:a 192k -ar 48000 -ac 2 -acodec libmp3lame -aframes 200 out2.mp3

#Video parameters:
## -vframes Set the number of video frames to output
## -b Set video bit rate
## -b:v video bit rate
## -r set frame rate
## -s Set the width and height of the screen
## -vn does not process videos
## -aspect aspect Set aspect ratio 4:3 16:9 or 1.3333 1.7777
## -vcodec Set the video codec. If copy is used, it means that the original codec data must be copied.
## -vf video filter
### ffmpeg -i test.mp4 -vframes 300 -b:v 300k -r 30 -s 640x480 -aspect 16:9 -vcodec libx265

ffmpeg command to extract audio and video data

# Preserve encapsulation format
## ffmpeg -i test.mp4 -acodec copy -vn audio.mp4
## ffmpeg -i test.mp4 -vcodec copy -an video.mp4
#Extract video
## Preserve encoding format: ffmpeg -i test.mp4 -vcodec copy -an test_copy.h264
## Forced format: ffmpeg -i test.mp4 -vcodec libx264 -an test.h264
# Extract audio
## Preserve encoding format: ffmpeg -i test.mp4 -acodec copy -vn test.aac
## Force format: ffmpeg -i test.mp4 -acodec libmp3lame -vn test.mp3

ffmpeg command to extract pixel format

# Extract YUV
## Extract 3 seconds of data, the resolution is the same as the source video
### ffmpeg -i test_1280x720.mp4 -t 3 -pix_fmt yuv420p yuv420p_orig.yuv
# Extract 3 seconds of data and convert the resolution to 320x240
##ffmpeg -i test_1280x720.mp4 -t 3 -pix_fmt yuv420p -s 320x240 yuv420p_320x240.yuv
#Extract RGB
## Extract 3 seconds of data and convert the resolution to 320x240
### ffmpeg -i test.mp4 -t 3 -pix_fmt rgb24 -s 320x240 rgb24_320x240.rgb
# Conversion between RGB and YUV
## ffmpeg -s 320x240 -pix_fmt yuv420p -i yuv420p_320x240.yuv -pix_fmt rgb24 rgb24_320x240_2.rgb

ffmpeg command to extract PCM data

# Extract PCM
## ffmpeg -i buweishui.mp3 -ar 48000 -ac 2 -f s16le 48000_2_s16le.pcm
## ffmpeg -i buweishui.mp3 -ar 48000 -ac 2 -sample_fmt s16 out_s16.wav
## ffmpeg -i buweishui.mp3 -ar 48000 -ac 2 -codec:a pcm_s16le out2_s16le.wav
## ffmpeg -i buweishui.mp3 -ar 48000 -ac 2 -f f32le 48000_2_f32le.pcm
## ffmpeg -i test.mp4 -t 10 -vn -ar 48000 -ac 2 -f f32le 48000_2_f32le_2.pcm

ffmpeg command to encapsulate

# Keep encoding format:
## ffmpeg -i test.mp4 -vcodec copy -acodec copy test_copy.ts
## ffmpeg -i test.mp4 -codec copy test_copy2.ts
# Change encoding format:
## ffmpeg -i test.mp4 -vcodec libx265 -acodec libmp3lame out_h265_mp3.mkv
# Modify frame rate:
## ffmpeg -i test.mp4 -r 15 -codec copy output.mp4 (wrong command)
## ffmpeg -i test.mp4 -r 15 output2.mp4
# Modify video bit rate:
## ffmpeg -i test.mp4 -b 400k output_b.mkv (the audio is also re-encoded at this time)
# Modify video bit rate:
## ffmpeg -i test.mp4 -b:v 400k output_bv.mkv
# Modify audio bitrate:
## ffmpeg -i test.mp4 -b:a 192k output_ba.mp4
## If you do not want to re-encode the video, you need to add -vcodec copy
# Modify audio and video bitrate:
## ffmpeg -i test.mp4 -b:v 400k -b:a 192k output_bva.mp4
# Modify video resolution:
## ffmpeg -i test.mp4 -s 480x270 output_480x270.mp4
# Modify audio sampling rate:
## ffmpeg -i test.mp4 -ar 44100 output_44100hz.mp4

ffmpeg command image and video conversion

# Capture a picture
## ffmpeg -i test.mp4 -y -f image2 -ss 00:00:02 -vframes 1 -s 640x360 test.jpg
## ffmpeg -i test.mp4 -y -f image2 -ss 00:00:02 -vframes 1 -s 640x360 test.bmp
## -i input
## -y override
## -f format
## image2 a format
## -ss starting value
## -vframes frame If greater than 1, then add d to the output test d.jpg
## -s format size size
# Convert video to image (one image per frame):
## ffmpeg -i test.mp4 -t 5 -s 640x360 -r 15 frame d.jpg
#Convert image to video:
## ffmpeg -f image2 -i frame d.jpg -r 25 video.mp4

ffmpeg command GIF and video conversion

# Generate GIF images from videos
## ffmpeg -i test.mp4 -t 5 -r 1 image1.gif
## ffmpeg -i test.mp4 -t 5 -r 25 -s 640x360 image2.gif
#Convert GIF to video
## ffmpeg -f gif -i image2.gif image2.mp4

ffmpeg command filter

# Generate test files
## Find three different videos and capture 10 seconds of each video.
### ffmpeg -i sand sea 02.mp4 -ss 00:05:00 -t 10 -codec copy 1.mp4
### ffmpeg -i Avengers 3.mp4 -ss 00:05:00 -t 10 -codec copy 2.mp4
### ffmpeg -i Operation Red Sea.mp4 -ss 00:05:00 -t 10 -codec copy 3.mp4
### If the audio and video formats are not unified, they are forced to be unified to -vcodec libx264 -acodec aac
## Convert the above 1.mp4/2.mp4/3.mp4 to ts format
### ffmpeg -i 1.mp4 -codec copy -vbsf h264_mp4toannexb 1.ts
### ffmpeg -i 2.mp4 -codec copy -vbsf h264_mp4toannexb 2.ts
### ffmpeg -i 3.mp4 -codec copy -vbsf h264_mp4toannexb 3.ts
## Convert to flv format
### ffmpeg -i 1.mp4 -codec copy 1.flv
### ffmpeg -i 2.mp4 -codec copy 2.flv
### ffmpeg -i 3.mp4 -codec copy 3.flv
#### When separating H.264 in certain encapsulation formats (such as MP4/FLV/MKV, etc.), you need to write SPS and PPS first, otherwise the separated data will not be played without SPS and PPS. The SPS and PPS information of the H.264 code stream is stored in the extradata of the AVCodecContext structure. Need to use the bitstream filter named "h264_mp4toannexb" in ffmpeg.
# Start splicing files
## Splicing in MP4 format
### Method 1: ffmpeg -i "concat:1.mp4|2.mp4|3.mp4" -codec copy out_mp4.mp4
### Method 2: ffmpeg -f concat -i mp4list.txt -codec copy out_mp42.mp4
## Splicing in TS format
### Method 1: ffmpeg -i "concat:1.ts|2.ts|3.ts" -codec copy out_ts.mp4
### Method 2: ffmpeg -f concat -i tslist.txt -codec copy out_ts2.mp4
## Splicing in FLV format
### Method 1: ffmpeg -i "concat:1.flv|2.flv|3.flv" -codec copy out_flv.mp4
### Method 2: ffmpeg -f concat -i flvlist.txt -codec copy out_flv2.mp4
## Method 1 is only applicable to some encapsulation formats, such as TS
## suggestion:
###(1) Use method 2 for splicing
###(2) Convert to TS format and then splice it
# Test different encoding splicing
## Modify audio encoding
### ffmpeg -i 2.mp4 -vcodec copy -acodec ac3 -vbsf h264_mp4toannexb 2.ts
### ffmpeg -i "concat:1.ts|2.ts|3.ts" -codec copy out1.mp4 The result is that there is no sound in the second paragraph
## Modify audio sampling rate
### ffmpeg -i 2.mp4 -vcodec copy -acodec aac -ar 96000 -vbsf h264_mp4toannexb 2.ts
### ffmpeg -i "concat:1.ts|2.ts|3.ts" -codec copy out2.mp4 The second section plays abnormally
## Modify video encoding format
### ffmpeg -i 1.mp4 -acodec copy -vcodec libx265 1.ts
### ffmpeg -i "concat:1.ts|2.ts|3.ts" -codec copy out3.mp4
## Modify video resolution
### ffmpeg -i 1.mp4 -acodec copy -vcodec libx264 -s 800x472 -vbsf h264_mp4toannexb 1.ts
### ffmpeg -i "concat:1.ts|2.ts|3.ts" -codec copy out4.mp4
## Notice:
### Unify each video packaging format into ts, and then output the packaging format you need when splicing and outputting, such as MP4
### Video resolutions can be different, but the encoding format needs to be unified
### The audio encoding format needs to be unified, and the audio parameters (sampling rate/channel, etc.) also need to be unified