FFmpeg is currently the most awesome open source cross-platform audio and video processing tool.

Prepare knowledge

I am not an audio and video encoding and decoding background, for this piece of very do not understand, resulting in the learning of FFmpeg when the clouds in the fog, so before learning the best to look at some information on audio and video encoding and decoding a little understanding.

  • [[summary] FFMPEG audio codec based learning method – zero CSDN blog] (http://blog.csdn.net/leixiaohua1020/article/details/15811977)
  • [[summary] audio codec technology based learning method – zero CSDN blog] (http://blog.csdn.net/leixiaohua1020/article/details/18893769)
  • So many video formats, MP4/RMVB/MKV/AVI and so on, these video formats and coding compression standards MPEG4, H.264.H.265 and so on what is the relationship? – zhihu
  • All kinds of audio and video codec learning details – CSDN blog

The installation

Windows and MacOS users can download the built FFmpeg from Builds-Zeranoe FFmpeg, unzip it and add the environment variable PATH.

Installation package I uploaded to the baidu cloud at the same time, there are some test video: link: https://pan.baidu.com/s/1nwLh4hF password: v7yt

Play the video, FFplay

Learning FFmpeg is unavoidable to see the effect, and Windows with the player and garbage a horse, and we will need to see the video metadata, see his code, with the general player, can see but not very convenient. In fact, FFmpeg comes with a player FFplay!

FFplay is a simple cross-platform player that combines FFmpeg and SDL implementations. It’s extremely simple to use:

Ffplay [options] [' Input file ']Copy the code

And the console will print out all kinds of information about the video, which is very helpful for us to view the video conversion results.

FFplay specific documents:

  • ffplay Documentation
  • FFplay User guide
  • Ffplay shortcuts and options

Get video information, FFprobe

The FFplay command prints out the metadata of the video, but what if we just want to get the data and not play the video? For example, in the program we want to get the duration of the video, what command do we use? Run the FFprobe command.

Ffprobe [option] [' Input file ']Copy the code

The output must look familiar because it prints exactly the same information as FFplay:

We can also use some parameters:

  • -v quiet: Do not print logs so that the default output is not displayed and does not interfere with the information we want to output
  • -print_format json: Displays information in JSON format. It also supports XML, CSV, Flat, INI formats
  • -show_format: Prints the input format information
  • -show_streams: Prints information for each stream

The default output is rather brief, we can use -show_format and -show_streams to print the details we want, for example:

ffprobe -v quiet -show_format -print_format json res\BCSPA039_pre.mp4Copy the code

Then our program reads and parses the JSON and gets the duration field, which is the duration of the video.

Video manipulation, FFmpeg

Syntax for the ffmpeg command:

ffmpeg [global_options] {[input_file_options] -i input_url} ... {[output_file_options] output_url} ...Copy the code

Ffmepg supports multiple input sources (files, pipes, network streams, acquisition devices), specifying the input through -i. Ffmpeg supports multiple outputs, and any field on the command line that cannot be parsed as a parameter is used as the OUTPUT URL.

Arguments generally apply to and only to the next specified file, so the position of the argument is very important. So globally valid parameters should be first.

For details about the ffmpeg command, see

  • ffmpeg Documentation
  • Ffmpeg parameter description in Chinese

Ffmpeg has too many parameters, so it’s better to learn by using common commands.

Ffmpeg example

Split audio and video

Ffmpeg -i input_file -vcodec copy -an output_file_video// Outputs only videos ffmpeg -i input_file -acodec copy -vn Output_file_audio // Output audioCopy the code

Parameter Description:

  • -i: Specifies the input file
  • -vcodec: Specifies the video encoder, where copy is specified as a special value that copies the input video stream to the output unchanged
  • -an: Disables audio output
  • -vn: Disables video output

Video transcoding

ffmpeg -i input_file output_fileCopy the code

This is the simplest video transcoding command. Ffmpeg will infer the format from the content of the input, from the suffix of the output_file, and then transcode the output.

Let’s take a look at one of the more complex video transcoding commands THAT I work with:

ffmpeg -i "#src#" -y -s 1920x1080 -vcodec libx264 -c:a libvo_aacenc -b:a 48k -ar 44100 -ac 2 -qscale 4 -f #targetFmt# -movflags faststart -map 0:v:0 -map 0:a? "#destDir#/1080p/#fileNameNoEx#.mp4"Copy the code

Parameter Description:

  • -y: overwrites the output file
  • -s 1920x1080: Sets the frame size, i.e. video resolution, in the format ofWxH
  • -vcodec libx264: Set the video encoder.-codec:v libx264It’s another way of writing it
  • -c:a libvo_aacenc: Sets the audio encoder
  • -b:a 48k: Sets the audio bit rate
  • -ar 44100: Set the audio sampling rate to 44100
  • -ac 2: Sets the number of channels
  • -f #targetFmt#: Sets the output format. If not specified, the input file is inferred from the content, and the output file is inferred from the suffix.
  • -movflags faststart: puts the index information of MOV/MP4 files in front of the file to support downplay
  • -map 0:v:0: Selects the first video stream output of the input file
  • -map 0:a?: Select input file audio stream output, if there is no error
  • Qscale < value >VBR based on < value > mass, ranging from 0.01 to 255, the smaller the better the mass

When mp4 is transferred to OGV, if -qscale 4 is not specified, the resulting video picture is poor, with lots of noise and lag.

Video capture

Specify a time to capture a frame as output:

Flv-ss 00:00:14.435-vframes 1 out.png ffmPEG-I input.flV-ss 00:00:14.435-vframes 1 out.pngCopy the code

Parameter Description:

  • -ssIf applied to the input file, seek input file to this location, but many formats do not support SEEK, so only a rough idea. If applied to the output file, the input is decoded, but data before the specified time is ignored. This is applied to the output file, so anything up to 00:00:14.435 is ignored
  • -vframes 1: Specifies how many frames to output, in this case one frame.-vframesis-frames:vThe alias.

Take a screenshot every once in a while:

PNG, out2.png, out3.png, out1.png, out3.png. Img001.jpg, img002.jpg, img003.jpg, img003.jpg, img003.jpg, img003.jpg, img003.jpg, img003.jpg, img003.jpg, img003.jpg, img003.jpg, img003.jpg Flv-vf FPS =1/600 thumb%04d.bmp ffmPEG-i myVideo.avi -vf FPS =1/60 img% 03d.bmpCopy the code

Parameter Description:

  • -vf fps=1: Sets the filter of the video to FPS. The following parameters represent several frames per second. This is set to 1, which means one frame per second.-vfis-filter:vThe alias
  • out%d.png: Output multiple pictures,%dPlaceholders represent numbers, starting with 1. You can also use%2dSpecify fixed two digits

FPS filter Documentation: FPS Documentation

Many say

In the process of learning FFMPEG, I read several very good blogs, and then found that the authors are Lei Xiaohua. He passed away in 2016. Alas, another industry tragedy, and he unexpectedly died in college, really too hard. Hard as it is, you have to take care of your health.

How do you view the death of Lei Xiaohua? A sentence from

Heaven jealous talent, no exaggeration, if you do not know Lei Xiaohua, you may have no introduction to audio and video

Indeed, his article helped me get started with FFMPEG. Thank you, Thor. Have a good trip.

The resources

  • FFmpeg
  • ffplay Documentation
  • FFplay User guide
  • FFprobe Usage Guide
  • [[summary] FFMPEG audio codec based learning method – zero CSDN blog] (http://blog.csdn.net/leixiaohua1020/article/details/15811977)
  • [[summary] audio codec technology based learning method – zero CSDN blog] (http://blog.csdn.net/leixiaohua1020/article/details/18893769)
  • 【FFmpeg】FFmpeg common basic command – a dessert green – blog garden
  • Mp4 format file transcoding post processing (QT-Faststart tool introduction) – Omnivore bear Blog
  • Ffmpeg – Live streaming technology knowledge base
  • Chinese_Font_ Create a thumbnail image from the video every X seconds – FFmpeg
  • FFMpeg bit rate control – CBR or Vbr-CSDN blog

Test video download address

Anywhere on the Web that can download small videos in multiple formats:

  • Download Sample Videos / Dummy Videos For Demo Use
  • Sample WebM, Ogg, and MP4 Video Files for HTML5 | TechSlides
  • video comparison sample clips
  • Video Download MP4, HD MP4, Full HD, 3GP Format And Watch – HDvidz.in
  • Internet Archive: Digital Library of Free Books, Movies, Music & Wayback Machine

In this paper, the independent blog: FFmpeg notes – basic use blog | wood of Chinese fir