With the rise of “we Media”, many people will take selfies of videos or find video materials to cut, set with background music or commentary, add various special effects border, and then generate new video files after processing, and release them to various platforms. The original video files generated are very large, and we need to convert them or compress them to a size that can be easily uploaded or shared on other devices such as mobile phones.

FFmpeg is introduced

FFmpeg is an open source free multimedia video processing tool, including video capture function, video format conversion, video capture, video watermark and so on. It provides a complete solution for recording, converting, and streaming audio and video.

The FFmpeg project is also one of the most influential in the open source community. Almost everything you see in video conversion software or player is based on FFmpeg for functionality or encoding/decoding. It uses LGPL or GPL license, once some well-known domestic players use FFmpeg source code is not declared, was FFmpeg peg in the history of shame column.

FFmpeg installation

FFmpeg source code can be compiled. FFmpeg is available in almost all distribution Linux repositories and can be installed directly.

# debian/ubuntu apt install ffmpeg # redhat/centos/fedora' yum install ffmpeg # debian/ubuntu apt install ffmpeg # redhat/centos/fedora' yum install ffmpegCopy the code

FFmpeg official binary package is also available: ffmpeg.org/download.ht…

It is recommended to use the FFmpeg official static binary package, no additional dynamic library, only one executable file FFmpeg official website download after decompression after three executable files: FFmpeg, FFProbe, ffPlay.

FFmpeg use

There are three executable files after extracting FFmpeg from the official website:

  • Ffmpeg: Audio and video file processing conversion

  • Ffprobe: reads video file information

  • Ffplay: a simple player


Ffmpeg command:

ffmpeg -i video.wma ./video.mp4

Convert video.wma to MP4 format. The default ffMPEG MP4 format is H264 encoding

ffmpeg -i video.wma -c:v libx265 video.mp4

Convert video.wma video file to H265 encoded MP4 format.

H265 encoding has a higher compression rate than H264, and generates smaller files at lower bit rates. That is to say, h265 encoding is clearer than H264 at the same quality. For a video file of the same size, h265 encoding is smaller than H264 encoding. But when the video bit rate reaches a certain size, there is little difference between the two. H264 is more compatible than H265. By default, some mobile phones do not support H265-encoded video files, which need to be decoded by software. Video conversion, H265 encoding than H264 encoding will take longer, more CPU consumption.

ffmpeg -i video.mp4 -s 1280×720 video2.mp4

Compress the video to 1280×720 resolution.

ffmpeg -i video.mp4 -ss 00:00:10 -i video.mp4 -t 00:00:30 -c:v copy -c:a copy cut.mp4

Do not change the encoding of video and audio to cut the video, save 30 seconds from 10 seconds after the video, because the audio and video encoding specified parameter value copy, does not affect the video quality.

The ss parameter must be used before -i. If written after -i, ffmpeg will understand that the starting point is applied to the output file.

ffmpeg -i video.mp4 -metadata:s:v rotate=”90″ -c:v copy -c:a copy out.mp4

Flip the video 90 degrees without changing the video and audio quality. Change landscape to portrait.

Ffmpeg-f x11grab-r 30 -i: 0.0-f alsa -i hw:0,0 -acodec aac-vcodec libx264 out.mkv

Use FFmpeg video, which works on Linux. 30 frames per second, aAC for audio format, H264 for video format, and MKV for output.

Acodec specifies audio codecs (aAC, MP4, FLac) just like c: V

There are many other uses for FFmpeg, such as adjusting the bit rate, frame count, etc. Use ffmpeg –help to see the full parameter description.

GPU acceleration

By default, FFmpeg uses CPU encoding. When the ffmpeg command is executed, the resource manager sees an increase in CPU usage.

If you use GPU computing, the conversion speed can be doubled, especially for H265 encoded video.

View ffMPEG supported video encoding formats:

ffmpeg -codecs
Copy the code

Find heVC, which is h265 code:

  • Hevc_qsv: Intel core display

  • Hevc_nvenc: indicates an NVIDIA graphics card

  • Hevc_amf: INDICATES an AMD graphics card

If h264 encoding is used, h264_QSV/H264_nvenc is used.

ffmpeg -i ./video.mp4 -c:v hevc_qsv ./out.mp4

Using the GPU encoding, run the above ffmpeg command and you can see that the GPU usage increases.

Using the GPU to speed up the video conversion was much faster, and I was able to use the core display only several times faster than CPU transcoding.


Ffprobe command

ffprobe ./video.mp4

View video file information.

From the output information, you can see that the video file is encoded in H264, the resolution is 1280×720, the average video bit rate is 921kbps, 24 frames. The audio of the video is encoded using aac, with a sampling rate of 44100Hz and a bit rate of 128kpbs.

ffprobe -v error -show_streams -print_format json ./video.mp4

Output video information in JSON format.


Ffplay command

ffplay ./video.mp4

Play a video file. A player pops up.

The ffplay command also has many control parameters. But I don’t think it’s much use, since graphical players are much more useful and powerful.


The development integration

Using the FFmpeg command, we can use a program or script to complete automatic video conversion. Also can let us develop the system has the video transcoding, or read the video information function. As a Java example:

public static int doWaitFor(Process process) { InputStream in = null; InputStream err = null; int exitValue = -1; // returned to caller when p is finished try { in = process.getInputStream(); err = process.getErrorStream(); boolean finished = false; // Set to true when p is finished while (! finished) { try { while (in.available() > 0) { // Print the output of our system call Character c = new Character((char)  in.read()); System.out.print(c); } while (err.available() > 0) { // Print the output of our system call Character c = new Character((char) err.read()); System.out.print(c); } // Ask the process for its exitValue. If the process // is not finished, an IllegalThreadStateException // is thrown. If it is finished, we fall through and // the variable finished is set to true. exitValue = process.exitValue(); finished = true; } catch (IllegalThreadStateException e) { // Process is not finished yet; // Sleep a little to save on CPU cycles Thread.currentThread().sleep(500); } } } catch (Exception e) { e.printStackTrace(); } finally { try { if (in ! = null) { in.close(); } } catch (IOException e) { e.printStackTrace(); } if (err ! = null) { try { err.close(); } catch (IOException e) { e.printStackTrace(); } } } return exitValue; }Copy the code

Java calls external commands using runtime.geTruntime ().exec(command). When using the call to ffmpeg command, the contents of the execute command output stream must be read so that the program does not block. Otherwise, when the buffer is full, the process will stall. Video conversion can be time-consuming, depending on hardware performance. In practical applications, we can also start the thread to process.

FFmpeg also provides C calls, common library integration. The above method is not the optimal solution, only a reference.


All original articles are from “Siege Lion · zheng” unless otherwise noted. Please cite the source.

Link to this article: engr-z.com/422.html