Audio and video command conversion tool - FFmpeg

With the rise of self-media, many people will take selfie videos or find video materials to crop, add background music or commentary, add various special effects borders, generate new video files after processing, and publish them on major platforms. The generated original video files are very large. We need to convert the format or compress the size to facilitate uploading or sharing on other devices such as mobile phones.

1. Introduction to FFmpeg

FFmpeg is an open source and free multimedia video processing tool. Its functions include video capture, video format conversion, video capture, and video watermarking. It provides a complete solution for recording, converting and streaming audio and video.

The influence of the FFmpeg project in the open source world is also one of the best. Almost all the functions or encoding/decoding used in video conversion software or players you see are based on FFmpeg. It adopts LGPL or GPL license. Some well-known players in China used FFmpeg source code without declaring it, and was nailed to the pillar of shame in history by FFmpeg.

2. FFmpeg installation

FFmpeg provides source code, which can be compiled by itself. Almost all distributions of Linux repositories already have FFmpeg, which can be installed directly.

# debian/ubuntu 
apt install ffmpeg 
​#
redhat/centos/fedora' 
yum 
install ffmpeg 
or 
dnf install ffmpeg

FFmpeg also officially provides binary packages: ffmpeg.org/download.ht…

It is recommended to use the official static binary package of FFmpeg, no additional dynamic library is required, and there is only one executable file downloaded from the official website. After decompression, FFmpeg has three executable files: ffmpeg, ffprobe, and ffplay.

3. FFmpeg use

After decompressing FFmpeg downloaded from the official website, there are three executable files:

  • ffmpeg: audio and video file processing conversion

  • ffprobe: read video file information

  • ffplay: a simple player


3.1 ffmpeg command:

ffmpeg -i video.wma ./video.mp4

Convert video.wma video files to mp4 format. ffmpeg mp4 format uses h264 encoding by default

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

Convert video.wma video files to h265 encoded mp4 format.

h265 has a higher compression rate than h264 encoding, and the files generated at low bit rates are smaller. That is to say, h265 encoding at the same quality is clearer than h264. For a video file of the same size, the h265 encoded video file is smaller than the h264 encoded video file. But when the video bit rate reaches a certain size, there is little difference in the definition between the two. H264 has better compatibility than h265. Some mobile phones do not support h265 encoded video files by default, and need to use software to decode them. For video conversion, h265 encoding takes longer than h264 encoding and consumes more CPU resources.

ffmpeg -i video.mp4 -s 1280x720 video2.mp4

Compress video to 1280x720 resolution.

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

Encoding without changing the video and audio clips the video, and keeps 30 seconds after 10 seconds of the video, because the audio and video encoding specifies the parameter value copy, which will not affect the video quality.

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

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

Flip video 90 degrees without changing video and audio quality. Change the horizontal screen to vertical screen.

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

Use FFmpeg to record, this command is valid on Linux. 30 frames per second, the audio format uses aac, the video format uses h264, and the output is in mkv format.

vcodec is the same as c:v parameter, specify video encoding, such as: libx264, libx265 acodec is the same as c:a parameter, specify audio encoding, such as: aac, mp4, flac

There are many other uses of FFmpeg, such as adjusting bit rate, frame number, etc. Use ffmpeg --help to see all parameter descriptions.

3.2GPU acceleration

FFmpeg uses CPU encoding by default. Explorer can see elevated CPU usage when executing ffmpeg commands.

If you use GPU computing, the conversion speed can be doubled, especially for converting h265 encoded videos.

View ffmpeg supported video encoding formats:

ffmpeg -codecs 
copy code

Find hevc, which is h265 encoding:

  • hevc_qsv: intel nuclear display

  • hevc_nvenc: nvidia graphics card

  • hevc_amf: amd graphics card

If using h264 encoding, use h264_qsv / h264_nvenc.

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

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

Using the GPU to accelerate the video conversion speed is much faster, and I can only use the core display speed to be several times faster than the CPU transcoding.


3.3 ffprobe command

ffprobe ./video.mp4

View video file information.

From the output information, we can see that the video file is encoded with h264, the resolution is 1280x720, the average bit rate of the video is 921kbps, and 24 frames. The audio of the video is encoded with aac, the sampling rate is 44100Hz, and the bit rate is 128kpbs.

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

Use json format to output video information.


3.4 ffplay command

ffplay ./video.mp4

Play video files. A player will pop up.

The ffplay command also has many control parameters. But I don't think it's very useful. After all, the graphical player is better and more powerful.


4. Development Integration

Using FFmpeg commands, we can complete automatic video conversion with programs or scripts. It is also possible to allow the system we develop to have the function of video transcoding or reading video information. Take Java as an 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;
}

Java calls external commands using Runtime.getRuntime().exec(command). When using the ffmpeg command, it is necessary to read the content in the output stream of the execution command, so that the program will not block. Otherwise, after the buffer is full, the process will be stuck. Video conversion can be time-consuming, depending on hardware performance. In practical applications, we can also open threads for processing.

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

Link: Audio and video command conversion tool - FFmpeg - Nuggets

★The business card at the end of the article can receive audio and video development learning materials for free, including (FFmpeg, webRTC, rtmp, hls, rtsp, ffplay, srs) and audio and video learning roadmaps, etc.

see below!

 

Guess you like

Origin blog.csdn.net/yinshipin007/article/details/130116033