Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to install multimedia processing tool FFmpeg under Linux

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to install the multimedia processing tool FFmpeg under Linux, which has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article.

FFmpeg is an open source computer program that can be used to record, convert digital audio and video, and convert it into a stream. Use the LGPL or GPL license. It provides a complete solution for recording, converting and streaming audio and video. It contains a very advanced audio / video codec library libavcodec. In order to ensure high portability and codec quality, many code in libavcodec are developed from scratch. FFmpeg is developed under the Linux platform, but it can also be compiled and run in other operating systems, including Windows, Mac OS X and so on.

Install ffmpeg on Ubuntu and other Linux systems

Installing ffmpeg is very easy, it is a very popular program, so you can install it directly through the package manager in most linux distributions.

Install ffmpeg on Ubuntu

On Ubuntu, ffmpeg exists in "Universe repository", so make sure you turn on enable universe repository, then update and install ffmpeg. Here are the commands you may need.

Sudo add-apt-repository universesudo apt updatesudo apt install ffmpeg

This is OK, you can try to see if it is installed correctly by using the following command:

Ffmpeg

He will print out some ffmpeg configuration and version information.

As shown in the figure above, the installed version is 3.4.4. But the latest version of ffmpeg should be 4.1. In order to install the 4.x version, you need to use ffmpeg ppa, you can study it yourself.

Install ffmpeg on Linux of Arch system

This is also very simple, just use the following command:

Sudo pacman-S ffmpeg installs ffmpeg on Linux of Fedora system

Just use the following command:

How sudo dnf install ffmpeg uses ffmpeg: basics

With the ffmpeg installation in place, I'll cover some of the basic concepts of using this powerful tool.

0. Ffmpeg command

The basic form of using the ffmpeg command is:

Ffmpeg [global parameters] {[input file parameters]-I input file address}. {[output file parameters] output file address}.

It is important to note that all parameters are valid only for the next file (the next file has to be written again).

All files that are not specified with-I are considered output files. Ffmpeg can accept multiple input files and export them to a location you specify. You can also specify the same file name for both input and output, but use the-y flag before the output file.

Note

You should not confuse input with output. Specify the input first and then the output file.

1. Get information about media files

The simplest use of ffmpeg is to display file information. Don't give the output, just simply write:

Ffmpeg-I file_name

Both video and audio files can be used:

Ffmpeg-I video_file.mp4ffmpeg-I audio_file.mp3

View file properties through ffmpeg

The command outputs a lot of information that has nothing to do with your file (ffmpeg itself), although this is useful, you can use-hide_banner to hide them:

Ffmpeg-I video_file.mp4-hide_bannerffmpeg-I audio_file.mp3-hide_banner

As shown in the figure, the command now displays only the information related to your file (encoder, data stream, etc.).

two。 Convert media files

Perhaps the most commendable thing about ffmpeg is that you can easily convert between different media formats. You just need to specify the input and output file names. Ffmpeg will guess the format from the suffix name. This method is suitable for both video and audio files.

Here are some examples:

Ffmpeg-I video_input.mp4 video_output.aviffmpeg-I video_input.webm video_output.flvffmpeg-I audio_input.mp3 audio_output.oggffmpeg-I audio_input.wav audio_output.flac

You can also specify multiple output suffixes at the same time

Ffmpeg-I audio_input.wav audio_output_1.mp3 audio_output_2.ogg

This will output multiple files at the same time.

To see the supported formats, you can use:

Ffmpeg-formats

Similarly, you can use-hide_banner to omit some program information.

You can use-qscale 0 to retain the original video quality before outputting the file:

Ffmpeg-I video_input.wav-qscale 0 video_output.mp4

Further, you can specify the encoder, specify the name of the encoder using-g-c:v a (audio) and video (video), or write copy to use the same encoder as the source file:

Ffmpeg-I video_input.mp4-CRAV copy-CRAV a libvorbis video_output.avi

Note: this can make file suffixes confusing, so avoid doing so.

3. Extract audio from video

To extract audio from a video file, simply add a-vn parameter:

Ffmpeg-I video.mp4-vn audio.mp3

This allows the command to reuse the bit rate of the original file. In general, it is better to use-ab (audio bit rate) to specify the encoding bit rate:

Ffmpeg-I video.mp4-vn-ab 128k audio.mp3

Some common bit rates are 96k, 128k, 192k, 256k, 320k (mp3 can also use the highest bit rate).

Other commonly used parameters such as-ar (sampling rate: 22050, 441000, 48000),-ac (number of channels),-f (audio format, usually automatically recognized). -ab can also be replaced by-bVRIA. For example:

Ffmpeg-I video.mov-vn-ar 44100-ac 2-bazaa 128k-f mp3 audio.mp34. Mute the video

Similar to the previous requirement, we can use-an to get pure video (formerly-vn).

Ffmpeg-I video_input.mp4-an-video_output.mp4

Note: this-an flag invalidates all audio parameters, because no audio will be generated in the end.

5. Extract pictures from video

This feature may be useful for many people, for example, you may have some slides from which you want to extract all the pictures, then the following command can help you:

Ffmpeg-I video.mp4-r 1-f image2 image-%3d.png

Let's explain this command:

-r represents the frame rate (how many images are exported in a second, by default 25), and-f represents the output format (image2 actually means image2 sequence).

The last parameter (output file) has an interesting name: it uses% 3D to indicate that the output image has three digits (000, 001, etc.). You can also use% 2d (two digits) or% 4d (4 digits). You can experiment with how you can write it if you like!

Note: there are also ways to turn pictures into videos / slides, which will be discussed in the following advanced applications.

6. Change video resolution or aspect ratio

It's another simple task for ffmpeg. You just need to use the-s parameter to zoom the video:

Ffmpeg-I video_input.mov-s 1024x576 video_output.mp4

At the same time, you may need to use-CRAV a to ensure that the audio encoding is correct:

Ffmpeg-I video_input.h364-s 640x480-CRAV a video_output.mov

You can also use * *-aspect** to change the aspect ratio:

Ffmpeg-I video_input.mp4-aspect 4:3 video_output.mp4

Note: more powerful methods will be mentioned in advanced applications

7. Add a cover image to the audio

There is a great way to turn audio into video, using a single picture (such as an album cover). It's useful when you want to upload audio to a website that only accepts videos (such as YouTube, Facebook, etc.).

Here is an example:

Ffmpeg-loop 1-I image.jpg-I audio.wav-v libx264-C image.jpg an aac-strict experimental-b Rau a 192 k-shortest output.mp4

Just change the encoding settings (- CRAV is video encoding,-CRAV is audio encoding) and the name of the file will work.

Note: if you use a newer version of ffmpeg (4.x), you can unspecify-strict experimental

8. Add subtitles to the video

Another common and easy requirement is to add letters to the video, such as a foreign language power supply, using the following command:

Ffmpeg-I video.mp4-I subtitles.srt-c output.mp4 v copy-c subtitles.srt a copy-preset veryfast-c V mov_text-map 0-map 1 output.mp4

Of course, you can specify your own encoder and any other audio and video parameters. You can read this article to learn about subtitles, editing subtitles in Linux.

9. Compress media files

Compressing files can greatly reduce the volume of files and save storage space, which is particularly important for file transfer. With ffmepg, there are several ways to compress the file volume.

Note: too much file compression can significantly reduce file quality.

First, for audio files, you can reduce the bit rate (using-bfrea or-ab):

Ffmpeg-I audio_input.mp3-ab 128k audio_output.mp3ffmpeg-I audio_input.mp3-baza192 k audio_output.mp3

Again, some commonly used bit rates are: 96k, 112k, 128k, 160k, 19K, 256k, 320k. The higher the value, the larger the size of the file required.

For video files, there are many options, and a simple way is to reduce the video bit rate (through-baffle v):

Ffmpeg-I video_input.mp4-bvv1000k-bufsize 1000k video_output.mp4

Note: the bit rate of video is different from audio (usually much larger).

You can also use the-crf parameter (constant quality factor). A smaller crf means a higher bit rate. The use of libx264 encoders also helps to reduce file size. Here's an example, the compression is good, and the quality doesn't change significantly:

Ffmpeg-I video_input.mp4-CVR v libx264-crf 28 video_output.mp4

A crf setting of 20 to 30 is the most common, but you can also try some other values.

Lowering the frame rate can also be effective in some cases (but this often makes the video look stuck):

Ffmpeg-I video_input.mp4-r 24 video_output.mp4

-r indicates the frame rate (24 here).

You can also reduce the size of the video file by compressing the audio, such as setting it to stereo or reducing the bit rate:

Ffmpeg-I video_input.mp4-C libx264 v libx264-ac 2-C libx264 an aac-strict-2-b ffmpeg a 128k-crf 28 video_output.mp4

Note:-strict-2 and-ac 2 are used to handle the stereo part.

10. Cut media files (basic)

To clip a portion from the beginning, use the T-t parameter to specify a time:

Ffmpeg-I input_video.mp4-t 5 output_video.mp4 ffmpeg-I input_audio.wav-t 00:00:05 output_audio.wav

This parameter applies to both audio and video, and the above two commands do something similar: save a 5s output file (at the beginning of the file). There are two different ways to represent time, a simple number (description) or HH:MM:SS (hours, minutes, seconds). The second way actually indicates the end time.

You can also give a start time with-ss and an end time with-to:

Ffmpeg-I input_audio.mp3-ss 00:01:14 output_audio.mp3ffmpeg-I input_audio.wav-ss 00:00:30-t 10 output_audio.wav ffmpeg-I input_video.h364-ss 00:01:30-to 00:01:40 output_video.h364 ffmpeg-I input_audio.ogg-ss 5 output_audio.ogg

You can see the use of start time (- ss HH:MM:SS), duration seconds (- t duration), end time (- to HH:MM:SS), and start seconds (- s duration).

You can use these commands in any part of the media file.

Ffmpeg: advanced use

Now it's time to talk about some advanced features (such as screenshots, etc.). Let's get started.

1. Split media files

I've already talked about how to cut a file, so how to split a media file? You only need to specify a start time, an end, or a duration for each output file.

Look at the following example:

Ffmpeg-I video.mp4-t 00:00:30 video_1.mp4-ss 00:00:30 video_2.mp4

The syntax is simple: you specify-t 00:00:30 as the duration for the first file (the first part is the first 30 seconds of the original file), and then specify everything that follows as the second file (starting at the end of the first part. 00:00:30).

You can specify as many parts as you want, give it a try, this feature is really powerful, and it also works with audio files.

two。 Stitching media files

Ffmpeg can also do the opposite: put multiple files together.

To do this, you have to create a text file with your own handy editor.

Because I like to use terminals, I use touch and vim here. The file name doesn't matter. Here I create the video_to_join.txt file with the touch command:

Touch videos_to_join.txt

Now, edit it using vim:

Vim videos_to_join.txt

You can use any tool you like, such as nano,gedit and so on.

In the contents of the file, enter the full path of the file you want to splice (the files will be spliced together in order), one file at a time. Make sure they have the same suffix name. Here is my example:

/ home/ubuntu/Desktop/video_1.mp4/home/ubuntu/Desktop/video_2.mp4/home/ubuntu/Desktop/video_3.mp4

Save this file, and the same method applies to any audio or video file.

Then use the following command:

Ffmpeg-f concat-I join.txt output.mp4

Note: the name of the output file used is output.mp4, because my input files are all mp4.

In this way, all the files in your videos_to_join.txt will be spliced into a separate file.

3. Turn a picture into a video

This will tell you how to turn a picture into a slide show, as well as how to add audio.

First of all, I suggest you put all the pictures in a folder, I put them in my_photos, and the suffix name of the image had better be .png or .jpg, no matter which one you choose, they should be the same suffix, otherwise ffmpeg may not work properly, you can easily change .png to .jpg (or vice versa).

The format of our conversion (- f) should be set to image2pipe. You must use the hyphen (-) to indicate input. Image2pipe allows you to use the result of a pipe (using | between commands) instead of a file as input to ffmpeg. The result of the command is to output the contents of all the pictures one by one, and note that the video encoder is copy (- CMV copy) to use the image input correctly:

Cat my_photos/* | ffmpeg-f image2pipe-I-CMV copy video.mkv

If you play this file, you may feel that only some of the images have been added, in fact, all the images are there, but ffmpeg plays them too fast, the default is 23fps, 23 pictures a second.

You should specify a frame rate (- framerate):

Cat my_photos/* | ffmpeg-framerate 1-f image2pipe-I-CRAV copy video.mkv

In this example, the frame rate is set to 1, that is, each frame (each image) will be displayed for 1 second.

To add some sound, you can use the audio file as input (- I audo_file) and set the copy audio encoding (- CMV a copy). You can set encoders for both audio and video, just before outputting the file. You need to calculate the length of the audio file and the number of pictures to determine the appropriate frame rate. For example, if my audio file is 22 seconds and there are 9 pictures, then the frame rate should be 9 / 22 or about 0.4, so I enter the command like this:

Cat my_photos/* | ffmpeg-framerate 0.40-f image2pipe-I-I audio.wav-c copy video.mkv4. Recording screen

It is also not difficult to record the screen through ffmpeg, set the format (- f) to x11grab. He will grab your XSERVER. If you enter it, you can. This is the screen number (usually 0:0). The crawl is calculated from the upper left corner, and the screen resolution (- s) can be specified. My screen is 1920 x 1080. Note that the screen resolution is hard to specify t before input:

Ffmpeg-f x11grab-s 1920x1080-I: 0.0 output.mp4

Press Q or CTRL+C to end the recording screen.

Tip: you can get real resolution with commands instead of writing a fixed size:

-s $(xdpyinfo | grep dimensions | awk'{print $2;}')

The complete command reads as follows:

Ffmpeg-f x11grab-s $(xdpyinfo | grep dimensions | awk'{print $2;}')-I: 0.0 output.mp45. Recording camera

Recording from the camera is even easier. All the devices on linux are in / dev, such as / dev/video0, / dev/video1, etc.:.

Ffmpeg-I / dev/video0 output.mkv

Again, Q or CTRL+C to end the recording.

6. Record sound

Both ALSA and pulseaudio are used to process sound on Linux. Ffmpeg can record both, but I want to highlight pulseaudio because it is used by default in the distribution of the Debian series. The command is as follows:

In pulseaudio, you must force to specify (- f) alsa and then specify default as input t (- I default):

Ffmpeg-f alsa-I default output.mp3

Note: you should be able to see the default recording device in your system audio settings.

I often play guitar, and I usually use a professional audio device to record sound. I was surprised to find that ffmpeg can also record easily.

Recording Tips

For recording tasks, you usually need to specify the encoder and frame rate, and of course the parameters mentioned earlier can also be used here!

Ffmpeg-I / dev/video0-f alsa-I default-c default v libx264-c Rd a flac-r 30 output.mkv

Sometimes instead of recording directly, you can give an audio file when recording / recording, so you can do this:

Ffmpeg-f x11grab-s $(xdpyinfo | grep dimensions | awk'{print $2;}')-I: 0.0-I audio.wav-CMV a copy output.mp4

Note: ffmpeg uses snippet recordings, and all sometimes very short recordings may not save files. I suggest that the recording can be a little longer (and then cut later) to ensure that the recorded files are successfully written to disk.

Basic use of filters in ffmpeg

Filters are the most powerful feature in ffmpeg. There are numerous filters in ffmepg that can meet a variety of editing needs. Because there are so many filters, here are only a few commonly used ones.

The basic structure of using filtering is:

Ffmpeg-I input.mp4-vf "filter=setting_1=value_1:setting_2=value_2,etc" output.mp4ffmpeg-I input.wav-af "filter=setting_1=value_1:setting_2=value_2,etc" output.wav

You can specify video filters (- vf, short for-filter:v) and audio filters (- af, short for-filter:a). The contents of the filter are written in double quotes (") and can be connected with commas (,). You can use any number of filters (I wrote an etc for more, this is not a real filter).

The usual format set by the filter is:

Filter=setting_2=value_2:setting_2=value_2

Different values of the filter are separated by colons.

You can even use mathematical symbols to calculate values.

Note: refer to the * ffmpeg filter manual. * for more advanced usage.

Here are a few examples to illustrate video and audio filters.

1. Video zooming

This is a simple filter, with only width and height in the settings:

Ffmpeg-I input.mp4-vf "scale=w=800:h=600" output.mp4

I said you can use math to give values:

Ffmpeg-I input.mkv-vf "scale=w=1/2*in_w:h=1/2*in_h" output.mkv

Obviously, this command changes the input size to 1 in_w 2 of the input size (in_h).

two。 Video clipping

Similar to scaling, this setting also has width and height, and you can specify the origin of the clipping (the default is the center of the video).

Ffmpeg-I input.mp4-vf "crop=w=1280:h=720:x=0:y=0" output.mp4ffmpeg-I input.mkv-vf "crop=w=400:h=400" output.mkv

The second command crop origin is the center point of the video (because I didn't give x and y coordinates), and the first command starts clipping (x=0:y=0) from the upper left corner.

Here is also an example of using mathematical calculations:

Ffmpeg-I input.mkv-vf "crop=w=3/4*in_w:h=3/4*in_h" output.mkv

This will cut the video back to the original size of 3x4 /.

3. Video rotation

You can specify a Radian to rotate the video clockwise. To make the calculation easier, you can give the angle and multiply it by PI/180:

Ffmpeg-I input.avi-vf "rotate=90*PI/180" ffmpeg-I input.mp4-vf "rotate=PI"

The first command rotates the video 90 degrees clockwise, and the second rotates the video upside down (180 degrees upside down).

4. Audio channel remapping

Sometimes, your audio can only be heard in the right ear, so this feature is very useful. You can make the sound appear on the left and right channels at the same time:

Ffmpeg-I input.mp3-af "channelmap=1-0 | 1-1" output.mp3

This maps the right channel (1) to both channels left (0) and right (1) (the number on the left is the input and the number on the right is the output).

5. Change the volume

You can multiply the volume by a real number (it can be an integer or not). All you have to do is give that number.

Ffmpeg-I input.wav-af "volume=1.5" output.wavffmpeg-I input.ogg-af "volume=0.75" output.ogg

The first one changes the volume to 1.5 times the volume, and the second one makes the volume as quiet as the original 1amp 4.

Tip: change the playback speed

Here are the filters for video (which does not affect audio) and audio.

Video

The video filter is setpts (PTS = presentation time stamp). This parameter works in an interesting way, because we are modifying PTS, so a higher value means slower playback, and vice versa:

Ffmpeg-I input.mkv-vf "setpts=0.5*PTS" output.mkvffmpeg-I input.mp4-vf "setpts=2*PTS" output,mp4

The first command doubled the playback speed, and the second halved the playback speed.

two。 Audio frequency

The filter here is atempo. There is a limit, which only accepts values between 0.5 (half speed) and 2 (double speed). To overcome this limit, you can use this filter in a chain:

Ffmpeg-I input.wav-af "atempo=0.75" output.wavffmpeg-I input.mp3-af "atempo=2.0,atempo=2.0" ouutput.mp3

The first command slows down the audio speed by 1x4, and the second one speeds up to 4 (2x2) times.

Thank you for reading this article carefully. I hope the article "how to install multimedia processing tool FFmpeg under Linux" shared by the editor will be helpful to you. At the same time, I also hope you can support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report