Ffmpeg Compress Video

0 views
Skip to first unread message
Message has been deleted

Savage Doherty

unread,
Jul 11, 2024, 11:26:18 PM7/11/24
to sommasordi

Lossless video - where a video has been transferred directly from a camera or generated by motion graphics software is typically very large. It is possible for the file size of uncompressed video to take up gigabytes of disk space for each minute of video. This is useful for high end cinematic or broadcast video production but is less useful for most every day video tasks, especially where the video is used online.

ffmpeg compress video


Descargar Zip >>> https://tinurli.com/2yOKNe



While it is simple to compress and resize videos using the Shotstack Ingest API, you might want to use a desktop tool and compress videos locally. In this post, we will learn how to use FFmpeg to compress video file sizes using different approaches. FFmpeg is an open-source command line tool that can be used to compress videos by adjusting codecs, resolution, bitrates and frame rate.

To follow along with this tutorial, download and install the latest version of FFmpeg for your operating system from the FFmpeg official downloads page. The tutorial also assumes you have some working knowledge of how to run commands in a terminal.

You will also want a large, high resolution test video with low compression. For this tutorial we are going to use a 1080p MOV sample file. Right click and save this file to your hard-drive and save or rename it to input.mov.

The first step to compress the video is to use a different video compression codec. A codec is simply an algorithm used to encode and compress video to a smaller file size without losing visual quality. One of the most popular and widely supported video formats is MP4. This is largely thanks to its video compression options made available using the H.264 codec - a highly efficient, open-source compression algorithm, that can create very small file sizes with minimal loss of visual quality.

It is worth noting you could use other codecs and file formats (containers) to compress the video. For example, you could use the H.265 codec as an MP4 file, or you could use the VP9 codec as a WebM file.

If you run either of these commands you might run in to a couple of problems. The H265 codec results in a much smaller file size, but when I tried to open the file using Windows built in media player I have to pay for and install a new codec - this will put a lot of people off. When encoding using VP9 codec, it is very slow and the final file is larger than a using H264/MP4 file. It is for these reasons that H264/MP4 files are still the most popular format.

One way to compress the video further is using a constant rate factor (CRF). This is the simplest way to control the compression without controlling individual settings. You can provide a value between 0-51. Lower values generate higher quality but larger files, and higher values create lower visual quality but smaller files. Sensible values are between 17 and 28 for MP4 files but you can experiment with different values to find the sweet spot that balances quality and file size. The default value is 23.

Another way to reduce a video's size is by changing its resolution - the dimensions of the video. Our input.mov file is 1920px x 1080px, which is a common resolution for high definition video. But you might be able to get away with a lower resolution, especially if the video is going to be viewed on a mobile device or embedded in a web page.

In the above command, we added the scale filter to one of our earlier commands to resize the video's width and height to 1280px x 720px. The -vf option is used to apply the filter and is short for video filter.

It is also common to maintain the aspect ratio of the video so it does not become distorted. To do this we can use the scale filter with a -1 value, which will automatically calculate the correct height or width based on the other dimension. Like this:

Reducing the bitrate of a video can also impact its file size and gives you a lot of control over the compression. The bitrate is the number of bits per second that are used to encode the video. The higher the bitrate, the higher the quality of the video, but the larger the file size. Conversely, the lower the bitrate, the lower the quality of the video, but the smaller the file size.

Finally, we can compress the video even further by reducing its frame rate. The frame rate is the number of frames per second (fps) that are displayed when the video is played. The higher the frame rate, the smoother the video, but the larger the file size. Conversely, the lower the frame rate, the choppier the video, but the smaller the file size.

In this article we showed how it is possible to compress a video using FFmpeg by changing its properties. By using FFmpeg and applying the correct compression settings, you can drastically reduce the file size of your videos while optimising the visual quality level.

In our example we were able to reduce the original file size by up to 99% from 269MB to 3.35MB. Even if you don't go to such extremes, compressing a video by even 80-90% will save massive amounts on storage and bandwidth costs and lead to a better user experience with faster downloads and smoother playback.

If you are looking for an easier and more automated way to manage your media optimisation and compression needs, consider Shotstack as a viable FFmpeg alternative. The Shotstack Ingest API can be used to upload, ingest and compress videos using a simple JSON schema and cloud based processing.

I'm developing android application that converts mp4 files into gifs using ffmpeg.Problem is that generated gifs are huge in size. And another problem is that I can't use anything else than ffmpeg(e.g.imagemagick for convert, or even palletes for now) to reduce generated gif size.

I tried -vf palettegen and -filter_complex paletteuse but that removed the ability to -vf "scale=iw*.2:ih*.2" in the same run so I had to do that separately. And ffmpeg created larger files for 0.3 scale than 0.5 which is odd. After an hour of fiddling, I tried ezgif and it worked nicely.

What worked for me was specifying a lower frame rate than the 10 you're using (-r 10) for the output gif. Probably not what you want if you're after good quality. If you're after a better quality gif then it will be bigger in file size.

Now I try to do it with WinFF, QWinFF, Videomass, FFmpeg, and nothing achieves this effect... Not only does the conversion of a single file strangely take literally multiple hours, but the resulting file is even larger than the original mp4s... They're only 640x480 too. The bitrate is like 1500.

What am I doing wrong? I cannot, for the life of me, seem to find any video converter on Linux that will reduce the file size of my oversized uncompressed MP4s and AVIs. Is it just the parameters? Is there something in it that I'm doing wrong? Are there any video converters that will do it better?

Edit: Example - input video with codecs mpeg4 aac, 2k bitrate, 127k audio at 44k sample rate, 24 FPS. Converted with ffmpeg to VPx vorbis, same audio, 1.5k bitrate, same framerate, same dimensions. And the resulting file was larger?

Do you have loads of GoPro movies eating up disk space? Looking for a way to compress them, but in such a way that quality is still very high and they continue to play nice with GoPro software, like Quik? I did too.

I've owned a GoPro Hero 3+ and now more recently a Hero 5. With the right conditions and settings it takes some really nice video, but sometimes they're just a little bit too space hungry for my liking.

I've previously written about how to compress videos generally, but for GoPro clips we need to do a little more. That's because there are additional streams embedded in the videos, as well as proprietary metadata.

What happened is that during our transcoding we essentially stripped out loads of data which the GoPro camera writes into the file, meaning we've lost some information as well as the ability to use our files in GoPro software.

What might not be so intuitive at first though is that a file can, and often does, include more streams than just one audio and one video. For example, we might have several different audio streams for different spoken languages, plus a subtitle stream. A decent video player will allow us to choose which streams we want to use when we play the file, perhaps french audio with english subtitles.

To tell FFmpeg to copy all streams we can use -map 0. Also, to tell it to copy streams even if it doesn't recognise their content (necessary for some GoPro data streams), we can use -copy_unknown also.

Since we want to preserve most of the streams without modification, we pass -c copy first, which says to just copy each stream in the input directly to the output. Then, we override the copy codec just for the video stream, using -c:v h264 as before.

Great, so we can override the handler names to the GoPro specific ones! One slight snag though, the streams are not always in the same order. That means if we set the handler names by index, for some videos we might end up using a name of GoPro AAC for the video, when that's the name for the audio.

This might not be a problem if you're just encoding a single file, as you can check it first to see what order it has been recorded in. But, if you want to batch convert many with the same command in a script for example, it will bite you.

Fortunately, we can work around this by explicitly listing the input streams that we want to map, and then follow that with the handler names we would like for each in the same order. This works because FFmpeg will map the output streams in the same order as you list them from the input.

We can explicitly list the video and audio streams easily, as there is only one of them. This just requires -map 0:v or -map 0:a respectively, which says to map the video or audio stream from the 0th (first) input file.

That happens because FFmpeg doesn't know what an fdsc stream is, so strangely instead of just copying it anyway (which is what we asked), it decides to stuff the whole thing with garbage data. It seems like this SOS stream is only used for file recovery anyway, and isn't that important.

d3342ee215
Reply all
Reply to author
Forward
0 new messages