preface

FFmpeg is a very powerful audio and video processing tool, we can use it to handle video composition, editing, adding effects and so on.

Official documentation is the official documentation of FFmpeg

FFmpeg official documentation command is really too much too much, and are all In English, feel master of these commands, are enough to learn a new language!

SO in order to let some friends quickly use FFmpeg, I sorted out some FFmpeg common knowledge and most of the daily use of the command for everyone to refer to!

Not collecting yet? Cut the crap and get to the dry stuff!

Android uses FFmpeg

FFmpeg integration of the environment is very troublesome to recommend everyone to use this library, one key integration of FFmpeg! Skr~

RxFFmpeg

Things to know about audio and video

Audio/video streaming

In the field of audio and video, we call all the way sound/video all the way stream. For example, when we used to watch Hong Kong movies on VCD when we were young, we could choose cantonese or Mandarin sound. In fact, there are two audio streams stored in the CD video file, and users can choose one to play.

The container

We are familiar with MP4, RMVB, MKV, AVI are multimedia container file formats (or multimedia package format), the so-called container refers to the different data streams (video streams, audio streams, subtitles streams, etc.) encapsulated in a file (carrier). During the playback, various streams are decoded and processed respectively, and then output to display and audio equipment for playback. Multimedia container format is different from encoding format, a container can encapsulate multiple encoding format of media streams. Streams encapsulate actual media data, such as video streams, audio streams, and subtitle streams. Typically, only one encoding format can be used for data in a stream.

channel

It’s a concept in audio called the track. In one audio stream, there can be mono, two – channel or stereo.

Frame rate

The frames per second (FPS) rate is the number of frames refreshed per second. The higher the frame rate, the smoother the video. Generally 30fps is acceptable, 60fps can significantly improve the sense of interaction and realism, but more than 75fps it is not easy to notice a significant improvement in fluency.

The resolution of the

Resolution is a measure of how fine a picture is, usually expressed in pixel density, commonly known as ppi(pixels per inch). Generally, the higher the pixel density, the finer the image and the less blur. In the case of video files, the pixel density is uncontrollable (determined by the player and display device). We usually use the number of pixels of video to represent its resolution such as 1080×640, 640×320 and so on.

Bit rate

Bit rate, also known as bit rate, indicates the number of bytes output by a multimedia stream per second. The unit is KB/s or Kbps. Under the same compression algorithm, the higher the bit rate, the better the audio and video quality.

Variable Bitrate (VBR)

It means that the output bit rate of the encoder can be adjusted adaptively according to the complexity of the input source signal so as to reduce the amount of data as much as possible while the output quality remains unchanged. VBR is good for storage, not for streaming.

Constant Bitrate (CBR)

It means that the output bit rate of the encoder is fixed, and CBR is not suitable for storage. There may not be enough bit rate to encode complex content, resulting in quality decline, and some bit rate will be wasted in simple content.

Sampling rate

The number of times an audio signal is sampled per second. The higher the sampling frequency, the more natural the sound will be. The unit is Hertz Hz. The sampling rate of audio files is generally 44.1kHz, that is, 44100 sampling times per second. Experiments have found that there will be obvious loss if the value is lower than this value, and it is difficult for human ears to distinguish higher than this value, and it will increase the space occupied by digital audio.

Video coding

A video stream can be thought of as a sequence of pictures, and we call each picture in this sequence a frame. If you store all the frames in a video, the amount of data is too large to be stored and transmitted easily. Fortunately, statistics show that the difference between adjacent frames of most videos is not large, so for A video with little change, we can first encode frame A completely, then B frame only needs to encode the part different from FRAME A, and C frame after B frame only needs to encode the difference between frame B and frame B. So recursively, you encode a video as a sequence. When an image is too different from the previous image to be generated with reference to the previous frame, we end the previous sequence and encode the frame in its entirety and start a new sequence.

H264 is a popular video coding algorithm, which defines three kinds of frames: I frame of full coding, P frame of reference I frame generation containing only differences, and B frame of reference before and after frame coding.

The core algorithms H264 uses are intra – frame compression and inter – frame compression. Intra – frame compression is the algorithm to generate I frames, and inter – frame compression is the algorithm to generate B frames and P frames. In general, we also refer to fully encoded I frames as key frames. Because decoding non-key frames requires decoding the reference frame, key frames are often captured to improve performance in operations that do not require full decoding, such as screenshots.

FFmpeg basic character command

-i Enter the file address

-y overwrites existing output files

-n: Exit if an output file already exists (ffmPEG will ask whether to overwrite the output file if the -y or -n option is not set and the output file already exists)

-codec(-c) Specifies the input and output decoding encoder copy the input stream directly to the output stream without codec operation

-C :v or -vcodec can specify encoders for all video streams, and -c:v:1 specifies codecs for the second video stream. Xvid (using XVID encoding compressed video, cannot be changed)

-C :a or -acodec can specify an encoder for all audio streams, and -c:a:12 specifies acodec for the 13th video stream.

-pixel_format/-pix_fmt Specifies the conversion format, for example, yuv420p

– vol volume

The -ss option is used to set the start time of the stream. You can set input/output or filters. Frames prior to the start time are skipped without processing (input is not decoded, output is not encoded, filters are not processed).

Example: ffmPEG-SS 2-T 10-I test.mp4 test.mov

Time format: for example, -t 10, -t 23.167 Hour minute and second: for example, -t 10:23, -t 21:31:00.233

The -t option is used to set the input and output. The -t option is used to limit the input duration before -i and the -t option is used to limit the output duration before output files

The to option is similar to the -t option, except that to specifies the end time and -t specifies the duration

-f Specifies the input/output file format. By default, FFMPEG determines the file format based on the file name extension

-filter / -filter_complex Uses filter flow to process complex filters

-vf replaces -filter:v to process the video stream

-af replaces -filter:a to process audio streams

-vframes Sets the number of video frames to be output: for example, ffmpeG-i test.mp4-vframes 10 test.mov

-aframes Sets the number of audio frames to output. This is an outdated alias for -frames: a.

-vn does not write the video stream to the output file. Example :ffmpeg -i test.mp4 -vn -a:c copy out.mp3

Ffmpeg-i test.mp4 -v:c copy-an out.mp4

-r sets the framerate of a stream. -r sets the framerate of a stream. -r is the same as -framerate. -framerate is used to limit the input, and -r is used to limit the output. For example, ffmpeg -I test.mp4 -r:v 30 test.mov

-s Sets the frame size. For example :ffmpeg -I test.mp4 -s 1080×680 out.mp4

– Aspect [: stream_specifier] Aspect ratio (output, per stream) Sets the aspect specified in the video display aspect ratio. Aspect can be a string of floating-point numbers or a string of the form num: den, where num and den are the numerator and denominator of the aspect ratio. For example, “4:3”, “16:9”, “1.3333” and “1.7777” are valid parameter values. If used with a copy of -vCOdec, this affects the aspect ratio stored at the container level, but does not affect the aspect ratio stored in encoded frames (if present).

– Threads Sets the number of threads to be processed. For example, ffmPEG-Threads 8 -i test.mp4 out.mp4

– Shortest input stream stops coding and output example :ffmpeg -i gm.mp3 -i test.mp4 – Shortest output. Mp4

– sample_FMts Sets the audio sampling format. Use -sample_FMts to get a list of supported sample formats.

-b:a Specifies the audio bit rate. B is short for bitrate and A is short for audio

-b:v Specifies the video bit rate. B is short for bitrate and V is short for video

-loop Loops the input stream. Works only on the image stream

Concat splicing video cases: ffmpeg -i “concat: 1. Mp4 | 2. Mp4 | 3. Mp4” – c copy output. The mp4

The Filter (Filter)

Complex filter (filter_complex)

The filter will process the decoded frame and the processed frame will be re-encoded for output

scale

Specifies the size of the operation source,iw specifies the width of the video by integer, and ih specifies the height of the video by integer. For example, iW /2:-1 video is reduced by one time

overlay

Overlay =30:10 Main_w and main_h are the width and height of the underlying video, and overlay_w and overlay_h are the width and height of the overlay video

delogo

Delete a watermark. For example, delogo=x=800:y=20:w=70:h=80

crop

Crop format: crop=out_w:out_h: x :y out_w: output width. You can use the in_W table to enter the width of the video. Out_h: output height. You can use the in_H table to enter the height of the video. X: x coordinates Y: y coordinates if x and y are set to 0, clipping starts from the upper left corner. If you don’t write it is clipped from the center

setpts

Setpts =0.5*PTS means that the PTS timestamp of each video frame is multiplied by 0.5, i.e. the video speed is doubled

-map

Can be used to handle complex outputs, such as the ability to output a specified multiple stream to one output file, or to specify the output to multiple files. [v]” Alias for complex filter output as a stream of output files. The use of map above is to output video and audio output from a complex filter to a specified file.

Flip the HFLIP horizontally

The mirror

crop=iw/2:ih:0:0,split[left][tmp]; [tmp]hflip[right]

Various special curves

Curves =’vintage’ ‘Strong_contrast’ ‘lighter’ ‘negate’ ‘none’ ‘color_negative’ ‘Color’;

Edge detection edgeDetect

Halo vignette = PI / 4

dim

Colorlevels = rimin = 0.058: gimin = 0.058: bimin = 0.058

Increase contrast

fftfilt=dc_Y=0:weight_Y=’exp(-4 * ((Y+X)/(W+H)))

The noise reduction

Hqdn3d = luma_spatial = 15.0

sharpen

fftfilt=dc_Y=0:weight_Y=’1+squish(1-(Y+X)/100)’

Low pass filter

fftfilt=dc_Y=0:weight_Y=’squish((Y+X)/100-1)’

High-pass filter

fftfilt=dc_Y=128:weight_Y=’squish(1-(Y+X)/100)’

Fade video fades in and out

Ex. : Fade =in:0:25, fade=out:975:25 from 0 frame fades into 25 frame, Fade from 975 to 25 frames fade=in:5:20:color=yellow Before fading: yellow fade=in:0:25:alpha=1 Transparency of the last 15 frames fade= T =in: ST =5.5:d=0.5 Fade in 0.5 seconds d for the duration

FPS filter forces the frame rate by deleting or copying frames. Example: ffmPEG-y -i test. mp4-vf “FPS =60” out.mp4

Common commands

Video format conversion

ffmpeg -i out.mp4 -y out.avi

Turn video Gif

Turn video Gif

ffmpeg -i out.mp4 -y out.gif

10 seconds from 0 to Gif

ffmpeg -i out.mp4 -ss 00:00:00 -t 10 -y out.gif

Video to multiple pictures

One image per second

ffmpeg -i input_test.mp4 -vf fps=1 out%03d.png

One image is generated every minute

ffmpeg -i input_test.mp4 -vf fps=1/60 out%03d.png

A clip from a video

ffmpeg -ss 2 -i test.mp4 -t 10 -y -f out_test.mp4

Take the video 10 seconds from 2s

Video sharding (cutting video into segments)

ffmpeg -i test.mp4 -c copy -map 0 -f segment -segment_time 10 video/part-%d.mp4

Capture a single image or multiple images at a time in the video

ffmpeg -ss 2 -i test.mp4 -r 1 -t 2 -y -f image2 image-%3.jpeg

Single picture or multiple pictures to video

ffmpeg -loop 1 -i img%3d.png -t 10-y output.mp4

Turn the Gif video

ffmpeg -i input.gif -y output.mp4

Multiple video merging

ffmpeg -i "concat:input1.mp4|input2.mp4|input3.mp4" -c copy output.mp4

Modify video resolution

ffmpeg -i input_test.mp4 -s 320*240 out_test.mp4

Add audio to video

ffmpeg -i input.mp4 -i input1.mp3 -y output.mp4

Extract audio from video

ffmpeg -i test.mp4 -vn -a:c copy out.mp3

Video mute

ffmpeg -i input.mp4 -an -vcodec copy output.mp4

Audio merging

ffmpeg -i "concat:test1.mp3|test2.mp3" -acodec copy output.mp3

Senior command

Audio mixing

ffmpeg -i input_01.wav -i input_02.wav -filter_complex amix=inputs=2:duration=shortest:dropout_transition=3 output.wav

The duration of two short audio files is taken as the final output duration

  • Inputs: The number of inputs. If unspecified, it defaults to 2.// The input number is unspecified.
  • Duration: Determines the end of the stream
  • Longest: indicates the longest input duration
  • Shortest: Duration of the shortest input
  • First: duration of the first input
  • Dropout_transition: The conversion time (in seconds) of capacity renormations at the end of an input stream. The default value is 2 seconds.

Add text watermark

ffmpeg -y -i test.mp4 -vf "drawtext=fontfile=CourierNew.ttf:text='hello world':x=100:y=50:fontsize=24" out.mp4

Add text watermark on Android

Due to the Android side font library, I can add text by turning it into a picture and then adding it to the video. The following is the source of text to picture:

/** * Text to Bitmap * @param text text content * @param context * @returnPrivate static bitMap textToBitmap(String text, Context Context) {float scale = context.getResources().getDisplayMetrics().scaledDensity;
        TextView tv = new TextView(context);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        tv.setLayoutParams(layoutParams);
        tv.setText(text);
        tv.setTextSize(scale * TEXT_SIZE);
        tv.setGravity(Gravity.CENTER_HORIZONTAL);
        tv.setDrawingCacheEnabled(true);
        tv.setTextColor(TEXT_COLOR);
        tv.setBackgroundColor(Color.WHITE);
        tv.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        tv.layout(0, 0, tv.getMeasuredWidth(), tv.getMeasuredHeight());
        tv.buildDrawingCache();
        Bitmap bitmap = tv.getDrawingCache();
        int rate = bitmap.getHeight() / 20;
        return Bitmap.createScaledBitmap(bitmap, bitmap.getWidth()/rate, 20, false); } /** * Text generates picture * @param filePath filePath * @param text text * @param context context * @return*/ public static Boolean textToPicture(String filePath, String text, Context context){ Bitmap bitmap = textToBitmap(text , context); FileOutputStream outputStream = null; try { outputStream = new FileOutputStream(filePath); bitmap.compress(Bitmap.CompressFormat.JPEG, 90, outputStream); outputStream.flush(); } catch (IOException e) { e.printStackTrace();return false;
        }finally {
            try {
                if(outputStream != null){
                    outputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return true;
    } 
    
Copy the code

Add image Watermark

Add a single image watermark

ffmpeg -i input.mp4 -i water.png -filter_complex "[0:v][1:v]overlay=main_w-overlay_w-10:10" -y output.mp4

Add multiple image watermarks

ffmpeg -i input.mp4 -i photo1.png -i photo2.png -filter_complex "[0:v]fade=in:st=0:d=2,curves=vintage[img]; [1:v]scale=300:-1[img1]; [img][img1]overlay=10:10[out1]; [out1][2:v]overlay=main_w:main_h" -y output.mp4

Two images were added. The width of the first image was set to 300, and the height -1 was scaled according to the original image. The first image was in the upper left corner of the video, and the second image was in the lower right corner of the video

Running horse light effect

ffmpeg -i video2.mp4 -i logo.png -filter_complex "[1:v]scale=50*50[logo]; [0:v]scale=200*200[bg]; [bg][logo]overlay=x='if(gte(t,0),-overlay_w+(mod(n,main_w+overlay_w))+5,NAN)':y=0" -y output.mp4

Meaning: Time t is greater than 0, then start at x-overlay_w of the child content width, then count each frame n, divide the number of frames by (background main_w+ overlay_w of the child content background) +1 set to the x coordinate, Loop overlay=30:10 Main_w and main_h are the width and height of the underlying video, and overlay_w and overlay_h are the width and height of the overlay video

Video watermarking

ffmpeg -i test.flv -vf delogo=x=20:y=20:w=70:h=80 output.flv

X,y: specifies the location of the watermark, that is, the coordinates of the upper left corner of the picture W,h: indicates the width and height of the watermark

Add animation

ffmpeg -y -i test.mp4 -t 10 -loop 1 -framerate 6 -i test_%3d.png -filter_comple 'overlay=10:main_h-overlay_h-10' out.mp4

PNG (test_001.png, ani002.png…) Form an animation and overlay the animation in the lower left corner of the video. -t 10-loop 1 The animation will be played in a loop for 10 seconds

Fade-in fade-out effector

ffmepg -i input.wav -filter_complex afade=t=in:ss=0:d-4 output.wav

Fade the first 5s of the input file into output.wav

ffmpeg -i input.wav -filter_complex afade=t=out:st=200:d=5 output.wav

Wav file from 200s, do 5s fade effect, and put in output.wav file

Video brightening effector

Ffmpeg-i input. FLV -c:v libx264 -b:v 800k -c:a libfdk_aac -vf eq=brightness=0.25 -f mp4 output.mp4

The Brightness parameter is displayed. The value is from -1.0 to 1.0. The default is 0

Add contrast to the video

FLV -c:v libx264 -b:v 800k -c:a libfdk_aac -vf eq=contrast=1.5 -f mp4 output.mp4

The contrast parameter is contrast, and the value ranges from -2.0 to 2.0. The default value is 1.0

Capture the video area

ffmpeg -i input.mp4 -an -vf "crop=480:480:120:0" -vcodec libx264 -b:v 800k output.mp4

Rotate the video

ffmpeg -i input.mp4 -vf "transpose=1" -b:v 600k output.mp4

Change video Volume

Ffmpeg-i input.mp4 -af 'volume=0.5' output.mp4

Compressed video

– Preset Superfast can speed up compositing video time

Ffmpeg-i input. Mp4 -b:v 600k-y output.mp4

Recommended bit rate

Ali Cloud to give advice

END

FFmpeg is very powerful, the knowledge of this article is only a small part of FFmpeg, for the beginning of FFmpeg students for common video operations or completely OK, proficient in audio and video or have a long way to go, more FFmpeg know please move to FFmpeg official documents!

Welcome to collect a wave of beautiful young beautiful women, thank you ~