Detailed explanation of common examples of video processing tool FFmpeg

2020/12/3123:55:04 entertainment 2717


FFmpeg is a professional multimedia framework that can decode, encode, transcode, multiplex, demultiplex, stream, filter and play media files in almost all formats. Its core is the FFmpeg program itself, which is a video and audio processing tool based on the command line . It is mostly used for video transcoding , basic editing (trimming and merging), video scaling , post effect production , etc. Scenes. Here are some examples to briefly introduce the basic use of the ffmpeg command.

Detailed explanation of common examples of video processing tool FFmpeg - DayDayNews

1. Get detailed information

ffmpeg -i -hide_banner The -hide_banner option is used to omit the version information and compilation options of ffmpeg when outputting the detailed information of the file.

$ ffmpeg -i bbb.mp4 -hide_banner
Input 0, mov,mp4,m4a,3gp,3g2,mj2, from &39;bbb.mp4&39;:
  Metadata:
    major_brand: isom
    minor_version: 1
    compatible_brands: isomavc1
    creation_time: 2013-12-17T16:40:26.000000Z
    title: Big Buck Bunny, Sunflower version
    artist: Blender Foundation 2008, Janus Bager Kristensen 2013
    comment: Creative Commons Attribution 3.0-http://bbb3d.renderfarming.net
    genre: Animation
    composer: Sacha Goedegebure
  Duration: 00:10:34.53, start: 0.000000, bitrate: 8487 kb/s
    Stream 0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 3840x2160 [SAR 1:1 DAR 16:9], 8002 kb/s, 60 fps, 60 tbr, 60k tbn, 120 tbc (default)
    Metadata:
      creation_time: 2013-12-17T16:40:26.000000Z
      handler_name: GPAC ISO Video Handler
    Stream 0:1(und): Audio: mp3 (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 160 kb/s (default)
    Metadata:
      creation_time: 2013-12-17T16:40:28.000000Z
      handler_name: GPAC ISO Audio Handler
    Stream 0:2(und): Audio: ac3 (ac-3 / 0x332D6361), 48000 Hz, 5.1(side), fltp, 320 kb/s (default)
    Metadata:
      creation_time: 2013-12-17T16:40:28.000000Z
      handler_name: GPAC ISO Audio Handler
    Side data:
      audio service type: main
At least one output file must be specified

2. Format conversion

ffmpeg -i

FFmpeg is a powerful audio and video format converter that supports almost all current commonly used formats, such as: $ ffmpeg -i input.avi output.mp4

or frequently used, convert video Convert the file to GIF animation : $ ffmpeg -i input.mp4 output.gif

If you need to preserve the quality of the source video during format conversion, you can add the -qscale 0 option (the lower the value of -qscale, the higher the quality of the output video High): $ ffmpeg -i input.webm -qscale 0 output.mp4

You can use the -formats option to list all formats supported by the ffmpeg command (a very long list...):

$ ffmpeg -formats -hide_banner
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 amrnbraw AMR-NB
 D amrwb raw AMR-WB
 D anm Deluxe Paint Animation
 D apc CRYO APC
 D ape Monkey&39;s Audio
...

3. Specify the encoding

can manually specify the encoding of the output file through the -c option, such as: $ ffmpeg -i input.mp4 -c:v vp9 -c:a libvorbis output.mkv where -c:v is used to specify the video encoding , -C:a specifies the audio encoding

PS: the suffixes of video files such as mp4, mkv, avi, etc. just indicate the "container" type used to load the media stream, and the encoding method used during encoding needs to be specified separately. Of course, in many cases ffmpeg will choose the default encoding method according to the suffix of the output file, without manually specifying it.

only changes the encoding of the video or audio stream

can change only one of the video or audio encodings when specifying the encoding, while the other is kept in the original format: $ ffmpeg -i input.webm -c:v copy -c :a flac output.mkv -c:v copy means to copy the video stream in the input file to the output file, without re-encoding

only change the file suffix

, ​​that is, the video stream and audio stream in the input file are copied to the output file at the same time. Change the file suffix: $ ffmpeg -i input.webm -c:av copy output.mkv

encoding list

View all audio and video encoding formats supported by FFmpeg (another very long list...):

$ ffmpeg -codecs -hide_banner
Codecs:
 D..... = Decoding supported
 .E.... = Encoding supported
 ..V... = Video codec
 ..A... = Audio codec
 ..S... = Subtitle codec
 ...I.. = Intra frame-only codec
 ....L. = Lossy compression
 .....S = Lossless compression
 -------
 ...
 DEV.L. flv1 FLV / Sorenson Spark / Sorenson H.263 (Flash Video) (decoders: flv) (encoders: flv)
 D.V..S fmvc FM Screen Capture Codec
 D.VI.S fraps Fraps
 D.VI.S frwu Forward Uncompressed
 D.V.L. g2m Go2Meeting
 D.V.L. gdv Gremlin Digital Video
 DEV..S gif GIF (Graphics Interchange Format)
 DEV.L. h261 H.261
 DEV.L. h263 H.263 / H.263-1996, H.263+ / H.263-1998 / H.263 version 2
 D.V.L. h263i Intel H.263
 DEV.L. h263p H.263+ / H.263-1998 / H.263 version 2
 DEV.LS h264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (decoders: h264 h264_qsv h264_cuvid) (encoders: libx264 libx264rgb h264_amf h264_nvenc h264_qsv nvenc nvenc_h264)
 DEVIL. hap Vidvox Hap
 DEV.L. hevc H.265 / HEVC (High Efficiency Video Coding) (decoders: hevc hevc_qsv hevc_cuvid) (encoders: libx265 nvenc_hevc hevc_amf hevc_nvenc hevc_qsv)
...

Four, video compression

encoding and bit rate

Sometimes, based on disk space and network transmission considerations, video files need to be compressed. One way is to change the bitrate of the video to . In some cases, the appropriate reduction of the bit rate will not have much impact on the viewing effect of the video (within the range of the human eye). Of course, the choice of encoding will also have a certain impact on the size of the output file, an example is as follows:

$ ffmpeg -i input.webm -c:a copy -c:v vp9 -b:v 1M output.mkv -b: v Used to specify the bit rate of the video.

frame rate

Another way is to change the frame rate of the video file to , which is often referred to as FPS.

$ ffmpeg -i input.webm -c:a copy -c:v vp9 -r 30 output.mkv -r 30 The option is used to specify the frame rate of the output video to be 30 FPS. The resolution of

video will also affect the file size, you can use the -s option to specify the output file resolution. Of course, the frame size of the video will also change accordingly:

$ ffmpeg -i input.mkv -c:a copy -s hd720 output.mkv or $ ffmpeg -i input.mkv -c:a copy -s 1280x720 output. mkv

5. Extract audio

Through format conversion, FFmpeg can directly convert video files into audio files, and only need to specify the output file format as .mp3 or .ogg, etc. For example: $ ffmpeg -i input.mp4 output.mp3

At the same time, you can also specify audio format options during conversion: $ ffmpeg -i input.mp4 -vn -ar 44100 -ac 2 -ab 320 -f mp3 output.mp3

: -Vn: Specify the video to be disabled in the specified output file -ar: Specify the sampling rate of the audio in the output file -ac: Specify the number of audio channels -ab: Specify the bit rate of the audio -f: Specify the format of the output file

Command collection

adjust resolution

change the resolution of a video file to 1280x720: $ ffmpeg -i input.mp4 -filter:v scale=1280:720 output.mp4 Or: $ ffmpeg -i input.mp4 -s 1280x720 output.mp4

compressed video file

$ ffmpeg -i input.mp4 -vf scale =1280:-1 -c:v libx264 -preset veryslow -crf 24 output.mp4

You can also add the following options to compress the audio stream at the same time: -c:a aac -strict -2 -b:a 128k

remove audio

$ ffmpeg -i input.mp4 -an output.mp4 -an option means to disable audio extraction in the output file

$ ffmpeg -i input.mp4 -r 1 -f image2 image-%2d.png

The meaning of each option: -r: Set the frame rate, that is, how many frames per second are extracted into the picture. The default is 25 -f: Specify the output format. In this example, it is picture (image2) -image-%2d.png: Specify the naming method of the extracted picture. The final names in this example are image-01.png, image-02.png, etc. If image-%3d.png is used, the final name will be image-001.png, imag-002.png, etc.

cropped video

is to intercept the video screen within the specified range, and cut off the extra part: $ ffmpeg -i input.mp4 -vf "crop=w:h:x:y" output.mp4

where crop=w:h:x:y is used to specify the size and position of the "crop frame". w represents the width of the cropped part (the default is the width of the source video iw); h represents the height of the cropped part (the default is the height of the source video ih; x represents the starting position of the crop on the x axis (the leftmost is 0, the default is the source The middle position of the video); y represents the starting position of cropping on the y axis (the topmost is 0, and the default is the middle position of the source video).

changes the video ratio

video ratio is the aspect ratio of the video frame, which is usually Said 4:3 and 16:9 etc. $ ffmpeg -i input.mp4 -aspect 16:9 output.mp4

Set the audio cover

to create a video with a still picture as the screen.

$ ffmpeg -loop 1- i inputimage.jpg -i inputaudio.wav -c:v libx264 -tune stillimage -c:a aac -b:a 192k -shortest output.mp4 The options and parameters can be modified and omitted according to requirements.

intercepts video clips

$ ffmpeg -i input.mp4 -ss 00:00:50 -codec copy -t 60 output.mp4 captures video clips starting from the 50th second and lasting for one minute.

where -ss is used to specify the video clip The start time of the video clip; -t specifies the duration of the video clip in seconds.

can also be used in the following ways: $ ffmpeg -i audio.mp3 -ss 00:01:54 -to 00:06:53 -c copy output .mp3

and the above commands are also applicable to audio files.

video split

$ ffmpeg -i input.mp4 -t 00:00:30 -c copy part1.mp4 -ss 00:00:30 -codec copy part2.mp4 Split the input video file into two segments, the first segment is from the beginning to the 30th second; the second segment is from the 30th second to the end of the video. Among them, -t 00:00:30 is omitted -ss 00:00:00; -ss 00:00:30 is omitted -t remaining time.

is somewhat similar to intercepting multiple consecutive video clips.

video merging

first create a text file join.txt containing the path list of each media file:

file &39;~/myvideos/part1.mp4&39;
file &39;~/myvideos/part2.mp4&39;
file &39;~/myvideos/part3.mp4&39;

Use the -f concat option to merge multiple videos: $ ffmpeg -f concat -i join.txt -c copy output.mp4

add subtitle file

$ ffmpeg -i input.mp4 -i subtitle.srt -map 0 -map 1 -c copy -c:v libx264 -crf 23 -preset veryfast output.mp4

Change the video playback speed (audio is not affected)

$ ffmpeg -i input.mp4 -vf "setpts=0.5*PTS" output.mp4

The above command Will speed up the playback speed of the video screen, and the audio playback speed will not change.

If you want to slow down the switching speed of the video screen, you can change 0.5 in setpts=0.5*PTS to a value greater than 1.

Padding

is the two "black borders" on the top and bottom of widescreen video. You can use the FFmpeg command to add similar effects: $ ffmpeg -i input.mp4 -vf "scale=1920:1080:force_original_aspect_ratio=decrease,pad=1920:1080: (ow-iw)/2:(oh-ih)/2:black" output.mp4

This effect is specified by the pad parameter of the -vf option and can be modified according to the situation.

Create video from pictures

$ ffmpeg -framerate 1 -i img%02d.jpg -c:v libx264 -r 30 -pix_fmt yuv420p output.mp4 Put multiple pictures in the current directory (named img01.jpg, img02.jpg) In the form of) combined into a video file, the effect is similar to the automatic play PPT. switches one picture per second .

$ ffmpeg -framerate 30 -i img%02d.jpg -c:v libx264 -pix_fmt yuv420p output.mp4 also combines multiple pictures in the current directory into a complete video with a frame rate of 30 FPS. switches one picture per frame .


need more audio and video, webrtc, ffmpeg video learning materials, please private message in the background [audio and video] to get


Detailed explanation of common examples of video processing tool FFmpeg - DayDayNews


Detailed explanation of common examples of video processing tool FFmpeg - DayDayNews

entertainment Category Latest News