48
34
I want to compress an AVI file to MP4 using an h.264 codec. I could not get the same quality. How can I compress it?
Original Video:
Original Video Screen:
FFmpeg console output (ffmpeg -i input.avi
):
FFmpeg version SVN-r26402, Copyright (c) 2000-2011 the FFmpeg developers
built on Dec 28 2012 10:03:40 with gcc 4.4.6 20120305 (Red Hat 4.4.6-4)
configuration: --enable-shared --enable-gpl --enable-version3 --enable-nonfree --enable-pthreads --enable-x11grab --enable-libopencore-amrnb --enable-libopenc ore-amrwb --enable-libdc1394 --enable-libfaac --enable-libmp3lame --enable-libop enjpeg --enable-libspeex --enable-libtheora --enable-libvorbis --enable-libx264 --enable-libxvid --enable-zlib --enable-filter=drawtext
libavutil 50.36. 0 / 50.36. 0
libavcore 0.16. 1 / 0.16. 1
libavcodec 52.108. 0 / 52.108. 0
libavformat 52.93. 0 / 52.93. 0
libavdevice 52. 2. 3 / 52. 2. 3
libavfilter 1.74. 0 / 1.74. 0
libswscale 0.12. 0 / 0.12. 0
[mpeg4 @ 0x626b50] Invalid and inefficient vfw-avi packed B frames detected
Input #0, avi, from 'input.avi':
Metadata:
encoder : VirtualDubMod 1.5.10.2 (build 2542/release)
Duration: 01:32:38.13, start: 0.000000, bitrate: 2094 kb/s
Stream #0.0: Video: mpeg4, yuv420p, 720x304 [PAR 1:1 DAR 45:19], 23.98 tbr, 23.98 tbn, 23.98 tbc
Stream #0.1: Audio: ac3, 44100 Hz, 5.1, s16, 384 kb/s
At least one output file must be specified
Test 1
FFmpeg command:
ffmpeg -i input.avi -vcodec libx264 -vpre lossless_slow -crf 25 -acodec libfaac -threads 0 -t 60 1.mp4
Test 1 Screen:
FFmpeg console output (ffmpeg -i 1.mp4
)
FFmpeg version SVN-r26402, Copyright (c) 2000-2011 the FFmpeg developers
built on Dec 28 2012 10:03:40 with gcc 4.4.6 20120305 (Red Hat 4.4.6-4)
configuration: --enable-shared --enable-gpl --enable-version3 --enable-nonfree --enable-pthreads --enable-x11grab --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libdc1394 --enable-libfaac --enable-libmp3lame --enable-libopenjpeg --enable-libspeex --enable-libtheora --enable-libvorbis --enable-libx264 --enable-libxvid --enable-zlib --enable-filter=drawtext
libavutil 50.36. 0 / 50.36. 0
libavcore 0.16. 1 / 0.16. 1
libavcodec 52.108. 0 / 52.108. 0
libavformat 52.93. 0 / 52.93. 0
libavdevice 52. 2. 3 / 52. 2. 3
libavfilter 1.74. 0 / 1.74. 0
libswscale 0.12. 0 / 0.12. 0
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '1.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
creation_time : 1970-01-01 00:00:00
encoder : Lavf52.93.0
Duration: 00:01:00.01, start: 0.000000, bitrate: 618 kb/s
Stream #0.0(und): Video: h264, yuv420p, 720x304 [PAR 1:1 DAR 45:19], 437 kb/s, 23.98 fps, 23.98 tbr, 24k tbn, 47.95 tbc
Metadata:
creation_time : 1970-01-01 00:00:00
Stream #0.1(und): Audio: aac, 44100 Hz, 5.1, s16, 176 kb/s
Metadata:
creation_time : 1970-01-01 00:00:00
At least one output file must be specified
Test 2
FFmpeg command:
ffmpeg -y -i input.avi -pass 1 -vcodec libx264 -vpre slow -b 2000k -threads 0 -t 60 -f mp4 -an -y /dev/null
ffmpeg -y -i input.avi -pass 2 -vcodec libx264 -vpre slow -b 2000k -threads 0 -t 60 -acodec libfaac -ab 128k -ac 2 2.mp4
Test 2 Screen:
FFmpeg console output (ffmpeg -i 2.mp4
)
FFmpeg version SVN-r26402, Copyright (c) 2000-2011 the FFmpeg developers
built on Dec 28 2012 10:03:40 with gcc 4.4.6 20120305 (Red Hat 4.4.6-4)
configuration: --enable-shared --enable-gpl --enable-version3 --enable-nonfree --enable-pthreads --enable-x11grab --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libdc1394 --enable-libfaac --enable-libmp3lame --enable-libopenjpeg --enable-libspeex --enable-libtheora --enable-libvorbis --enable-libx264 --enable-libxvid --enable-zlib --enable-filter=drawtext
libavutil 50.36. 0 / 50.36. 0
libavcore 0.16. 1 / 0.16. 1
libavcodec 52.108. 0 / 52.108. 0
libavformat 52.93. 0 / 52.93. 0
libavdevice 52. 2. 3 / 52. 2. 3
libavfilter 1.74. 0 / 1.74. 0
libswscale 0.12. 0 / 0.12. 0
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '2.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
creation_time : 1970-01-01 00:00:00
encoder : Lavf52.93.0
Duration: 00:01:00.01, start: 0.000000, bitrate: 1097 kb/s
Stream #0.0(und): Video: h264, yuv420p, 720x304 [PAR 1:1 DAR 45:19], 1028 kb/s, 23.98 fps, 23.98 tbr, 24k tbn, 47.95 tbc
Metadata:
creation_time : 1970-01-01 00:00:00
Stream #0.1(und): Audio: aac, 44100 Hz, stereo, s16, 63 kb/s
Metadata:
creation_time : 1970-01-01 00:00:00
At least one output file must be specified
In Windows, if libaac is not present I think doing the following does not hurt:
ffmpeg -i input.avi -c:v libx264 -crf 19 -preset slow -c:a libvo_aacenc -b:a 192k -ac 2 out.mp4
– hyde – 2014-07-17T15:44:32.2003@hyde You should prefer
-c:a aac -strict experimental
instead oflibvo_aacenc
, because it delivers better quality. – slhck – 2014-07-17T17:27:13.413But in Windows I could not get it to work as the codec was missing :( – hyde – 2014-07-17T17:40:26.160
@hyde
-c:a aac
should always be available because it is built into ffmpeg. If you don't have it, you probably have some really outdated version? – slhck – 2014-07-17T17:46:38.723So I did
ffmpeg -formats
to see what options are available andaac
was listed there but I encountered an error using-c:a aac
. And my ffmpeg build is pretty recent. I think it's something to do with Windows. Windows build miss many features. – hyde – 2014-07-17T18:13:55.433@hyde But it should not miss this
aac
feature. Post a new question please, include full command line output, and we'll sort it out. Note thataac
is not a format, but an encoder, so checkffmpeg -codecs
. – slhck – 2014-07-17T19:26:44.527"Unrecognized option 'crf'", this is with the latest ffmpeg from github – matteo – 2015-12-05T01:27:06.473
@matteo Did you compile it with
– slhck – 2015-12-06T17:19:16.427--enable-libx264
? (See also https://trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu)@slhck "The vpre option is deprecated" --> still present and not marked as such – Gyan – 2018-06-14T05:42:44.937
@Gyan You're right. I think at the time I had read something to that effect. – slhck – 2018-06-14T09:08:34.860
If your video is smaller in size after the conversion with two passes, you need to specify a higher bitrate. Try
-b:v 3M
or more; but I'd recommend using the CRF method instead of the two-pass variant. – slhck – 2012-12-28T15:54:45.88310@AhmetKa Another option is to simply re-mux the streams from AVI container to MP4 container:
ffmpeg -i input.avi -c copy -map 0 output.mp4
. There is no re-encoding so there is no quality loss. – llogan – 2012-12-28T18:24:54.0231
@AhmetKa I should have mentioned that muxing will not always work if the output container format does not support media formats of the input. See Comparison of container formats to get a general idea of what works.
– llogan – 2012-12-28T21:39:09.603Hello again @slhck Crf is not properly. Bitrate, width, height, etc. Can I calculate? – Ahmet KAPIKIRAN – 2012-12-30T15:42:04.113
@AhmetKa I'm not sure I understand you. Do you want to be able to calculate the result file size? That's not really possible with CRF since it's variable bit rate. – slhck – 2012-12-30T15:43:17.460
Could not place the same quality video. Different is the quality of videos. I have to write a standard code.
ffmpeg -i -c copy -t 60 -threads 0 out.avi > http://tny.cz/038284ca ffmpeg -i input.avi -c:v libx264 -crf 19 -preset veryslow -c:a libfaac -b:a 64k -ac 2 -threads 0 -t 60 out.mp4 >http://tny.cz/4bbd4d60
@AhmetKa Sorry, I don't understand what you're asking or what the problem is. What is "placing the same quality video" and "writing a standard code"? Please also, rather than showing the console output from the result files, show the actual output when you're encoding the video. – slhck – 2012-12-30T16:08:49.163
@AhmetKa Try using a lower CRF value, maybe 18, 16, 14? The thing is: If you have to re-encode the video, you will lose quality, there's no way around that. Or you will get a big file. – slhck – 2012-12-30T16:45:00.230
@Ahmet If you're willing to get a crazily huge file, you could set a -crf of 0, which is mathematically lossless; but for all practical purposes, a crf of about 18 is the best you're going to get, as slhck suggests – evilsoup – 2013-01-03T23:44:28.003