{"id":5148,"date":"2014-03-30T19:13:26","date_gmt":"2014-03-30T19:13:26","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/use-divx-settings-to-encode-to-mp4-with-ffmpeg-collection-of-common-programming-errors\/"},"modified":"2014-03-30T19:13:26","modified_gmt":"2014-03-30T19:13:26","slug":"use-divx-settings-to-encode-to-mp4-with-ffmpeg-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/use-divx-settings-to-encode-to-mp4-with-ffmpeg-collection-of-common-programming-errors\/","title":{"rendered":"Use DivX settings to encode to mp4 with ffmpeg-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m used to use VirtualDub to encode a video to AVI container with DivX-codec (and MP3 for audio). Now I&#8217;m planning to use ffmpeg to encode videos to MP4 container with h264-codec. What I&#8217;ve figured out is that I need to use libx264 and one of those presets to make anything work. However, I&#8217;m amazed about the video bitrate ffmpeg uses for encoding.<\/p>\n<p>What I currently have is this little batch file:<\/p>\n<pre><code>@ECHO OFF\n\nSETLOCAL\n\nSET IN=source.avs\nSET FFMPEG_PATH=C:\\Program Files (x86)\\ffmpeg\nSET PRESET=-fpre \"%FFMPEG_PATH%\\presets\\libx264-lossless_slow.ffpreset\" \nSET AUDIO=-acodec libmp3lame -ab 128000\nSET VIDEO=-vcodec libx264 -vb 1978000\n\n\"%FFMPEG_PATH%\\ffmpeg.exe\" -i %IN% %AUDIO% %VIDEO% %PRESET% test.mp4\n\nENDLOCAL\n<\/code><\/pre>\n<p>With this I tell ffmpeg to use 1978k as the bitrate, but ffmpeg uses 15000k+! I tried other presets, but they don&#8217;t use my specified bitrate. Here are the presets I have:<\/p>\n<ul>\n<li>libx264-baseline.ffpreset<\/li>\n<li>libx264-ipod320.ffpreset<\/li>\n<li>libx264-ipod640.ffpreset<\/li>\n<li>libx264-lossless_fast.ffpreset<\/li>\n<li>libx264-lossless_max.ffpreset<\/li>\n<li>libx264-lossless_medium.ffpreset<\/li>\n<li>libx264-lossless_slow.ffpreset<\/li>\n<li>libx264-lossless_slower.ffpreset<\/li>\n<li>libx264-lossless_ultrafast.ffpreset<\/li>\n<\/ul>\n<p>ffmpeg version:<\/p>\n<pre><code>FFmpeg git-N-29181-ga304071\nlibavutil    50. 40. 1 \/ 50. 40. 1\nlibavcodec   52.120. 0 \/ 52.120. 0\nlibavformat  52.108. 0 \/ 52.108. 0\nlibavdevice  52.  4. 0 \/ 52.  4. 0\nlibavfilter   1. 79. 0 \/  1. 79. 0\nlibswscale    0. 13. 0 \/  0. 13. 0\n<\/code><\/pre>\n<p><em>Note that I don&#8217;t use the latest version as it has problems with spaces in filenames.<\/em><\/p>\n<p>Here&#8217;s what seems to be the full parameter list DivX 6.9.2 uses:<\/p>\n<pre><code>-bvnn 1978000 -vbv 218691200,100663296,100663296 -dir \"C:\\Users\\sjngm\\AppData\\Roaming\\DivX\\DivX Codec\" -w -b 1 -use_presets=1 -preset=10 -windowed_fullsearch=2 -thread_delay=1\n<\/code><\/pre>\n<p>What command line parameters would that be for ffmpeg?<\/p>\n<p>EDIT:<\/p>\n<p>Going with slhck&#8217;s suggestion I tried a new 32-bit version. I have no idea if that is 0.9 or newer, I can&#8217;t find that info.<\/p>\n<pre><code>ffmpeg version N-36890-g67f5650\nlibavutil      51. 34.100 \/ 51. 34.100\nlibavcodec     53. 56.105 \/ 53. 56.105\nlibavformat    53. 30.100 \/ 53. 30.100\nlibavdevice    53.  4.100 \/ 53.  4.100\nlibavfilter     2. 59.100 \/  2. 59.100\nlibswscale      2.  1.100 \/  2.  1.100\nlibswresample   0.  6.100 \/  0.  6.100\nlibpostproc    51.  2.100 \/ 51.  2.100\n<\/code><\/pre>\n<p>I reworked my batch file to look like this (interestingly enough I can&#8217;t find parameter <code>-vprofile<\/code> in the documentation):<\/p>\n<pre><code>@ECHO OFF\n\nSETLOCAL\n\nSET IN=VTS_01_1.avs\nSET FFMPEG_PATH=C:\\Program Files (x86)\\ffmpeg\nSET PRESET=-vprofile high -preset veryslow\nSET AUDIO=-acodec libmp3lame -ab 128000\nSET VIDEO=-vcodec libx264 -vb 1978000\n\n\"%FFMPEG_PATH%\\ffmpeg.exe\" -i %IN% %AUDIO% %PRESET% %VIDEO% test.mp4\n\nENDLOCAL\n<\/code><\/pre>\n<p>I see that it now uses the bitrate properly (thanks to LongNeckbeard for pointing out that the lossless-stuff ignores the bitrate!).<\/p>\n<p>Just in case you wonder how I came up with the 1978000, I&#8217;m using this formula which I found valid for DivX-files (I&#8217;m guessing the bitrate won&#8217;t change that much for h264):<\/p>\n<pre><code>width * height * 25 * 0.22 \/ 1000\n<\/code><\/pre>\n<p>I&#8217;m not sure if the 0.22 correlates with the CRF somehow.<\/p>\n<p>Overall I forgot to say the I will use a two-pass scenario, which is why I don&#8217;t use the CRF here. I will try to read more about this. Currently I&#8217;m just trying to get something running that shows me that I&#8217;m doing something right (ffmpeg isn&#8217;t the easiest tool to understand ;)).<\/p>\n<pre><code>C:\\Program Files (x86)\\ffmpeg\\ffmpeg.exe\" -i VTS_01_1.avs -acodec libmp3lame -ab 128000 -vcodec libx264 -vb 1978000 -vprofile high -preset veryslow test.mp4\n<\/code><\/pre>\n<p>The output is now:<\/p>\n<pre><code>ffmpeg version N-36890-g67f5650 Copyright (c) 2000-2012 the FFmpeg developers\n  built on Jan 16 2012 21:57:13 with gcc 4.6.2\n  configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-runtime-cpudetect --enable-avisynth --enable-bzlib --enable-frei0r --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libfreetype --enable-libgsm --enable-libmp3lame --enable-libopenjpeg --enable-librtmp --enable-libschroedinger --enable-libspeex --enable-libtheora --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libxavs --enable-libxvid --enable-zlib\n  libavutil      51. 34.100 \/ 51. 34.100\n  libavcodec     53. 56.105 \/ 53. 56.105\n  libavformat    53. 30.100 \/ 53. 30.100\n  libavdevice    53.  4.100 \/ 53.  4.100\n  libavfilter     2. 59.100 \/  2. 59.100\n  libswscale      2.  1.100 \/  2.  1.100\n  libswresample   0.  6.100 \/  0.  6.100\n  libpostproc    51.  2.100 \/ 51.  2.100\nInput #0, avs, from 'VTS_01_1.avs':\n  Duration: 00:58:46.12, start: 0.000000, bitrate: 0 kb\/s\n    Stream #0:0: Video: rawvideo (YV12 \/ 0x32315659), yuv420p, 576x448, 77414 kb\/s, 25 tbr, 25 tbn, 25 tbc\n    Stream #0:1: Audio: pcm_s16le ([1][0][0][0] \/ 0x0001), 48000 Hz, 2 channels, s16, 1536 kb\/s\nFile 'test.mp4' already exists. Overwrite ? [y\/N] y\nw:576 h:448 pixfmt:yuv420p tb:1\/1000000 sar:0\/1 sws_param:\n[libx264 @ 05A2C400] using cpu capabilities: MMX2 SSE2Fast FastShuffle SSEMisalign LZCNT\n[libx264 @ 05A2C400] profile High, level 3.1\n[libx264 @ 05A2C400] 264 - core 120 r2120 0c7dab9 - H.264\/MPEG-4 AVC codec - Copyleft 2003-2011 - http:\/\/www.videolan.org\/x264.html - options: cabac=1 ref=16 deblock=1:0:0 analyse=0x3:0x133 me=umh subme=10 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=24 chroma_me=1 trellis=2 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=3 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=8 b_pyramid=2 b_adapt=2 b_bias=0 direct=3 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=60 rc=abr mbtree=1 bitrate=1978 ratetol=1.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00\nOutput #0, mp4, to 'test.mp4':\n  Metadata:\n    encoder         : Lavf53.30.100\n    Stream #0:0: Video: h264 (![0][0][0] \/ 0x0021), yuv420p, 576x448, q=-1--1, 1978 kb\/s, 25 tbn, 25 tbc\n    Stream #0:1: Audio: mp3 (i[0][0][0] \/ 0x0069), 48000 Hz, 2 channels, s16, 128 kb\/s\nStream mapping:\n  Stream #0:0 -&gt; #0:0 (rawvideo -&gt; libx264)\n  Stream #0:1 -&gt; #0:1 (pcm_s16le -&gt; libmp3lame)\nPress [q] to stop, [?] for help\nframe=    0 fps=  0 q=0.0 size=       0kB time=00:00:00.00 bitrate=   0.0kbits\/s\nframe=    0 fps=  0 q=0.0 size=       0kB time=00:00:00.00 bitrate=   0.0kbits\/s\nframe=    0 fps=  0 q=0.0 size=       0kB time=00:00:00.00 bitrate=   0.0kbits\/s\nframe=    3 fps=  1 q=22.0 size=      39kB time=00:00:00.04 bitrate=8063.8kbits\/\nframe=    8 fps=  2 q=22.0 size=      82kB time=00:00:00.24 bitrate=2801.3kbits\/\nframe=   13 fps=  3 q=23.0 size=     120kB time=00:00:00.44 bitrate=2229.5kbits\/\nframe=   16 fps=  4 q=23.0 size=     147kB time=00:00:00.56 bitrate=2156.7kbits\/\nframe=   20 fps=  4 q=22.0 size=     175kB time=00:00:00.72 bitrate=1987.4kbits\/\n:\nvideo:4387kB audio:273kB global headers:0kB muxing overhead 0.260038%\n[libx264 @ 05A2C400] frame I:2     Avg QP:19.53  size: 29850\n[libx264 @ 05A2C400] frame P:76    Avg QP:22.24  size: 19541\n[libx264 @ 05A2C400] frame B:359   Avg QP:25.93  size:  8210\n[libx264 @ 05A2C400] consecutive B-frames:  0.5%  0.5%  0.0%  8.2% 17.2% 52.2% 16.0%  5.5%  0.0%\n[libx264 @ 05A2C400] mb I  I16..4:  5.4% 75.3% 19.3%\n[libx264 @ 05A2C400] mb P  I16..4:  1.3% 16.5%  2.2%  P16..4: 36.3% 28.6% 12.7% 1.8%  0.2%    skip: 0.4%\n[libx264 @ 05A2C400] mb B  I16..4:  0.4%  3.8%  0.3%  B16..8: 40.0% 18.4%  4.7% direct:18.5%  skip:13.9%  L0:45.4% L1:38.1% BI:16.5%\n[libx264 @ 05A2C400] final ratefactor: 20.35\n[libx264 @ 05A2C400] 8x8 transform intra:83.1% inter:68.5%\n[libx264 @ 05A2C400] direct mvs  spatial:99.2% temporal:0.8%\n[libx264 @ 05A2C400] coded y,uvDC,uvAC intra: 64.9% 83.4% 49.2% inter: 49.0% 50.4% 4.4%\n[libx264 @ 05A2C400] i16 v,h,dc,p: 25% 22% 27% 26%\n[libx264 @ 05A2C400] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 10%  7% 23%  9% 10% 10% 10%10% 13%\n[libx264 @ 05A2C400] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 12% 11% 13%  9% 12% 11% 10% 9% 12%\n[libx264 @ 05A2C400] i8c dc,h,v,p: 42% 28% 16% 14%\n[libx264 @ 05A2C400] Weighted P-Frames: Y:18.4% UV:7.9%\n[libx264 @ 05A2C400] ref P L0: 29.1% 11.3% 15.7%  7.3%  6.9%  4.9%  5.1%  3.4%3.9%  2.7%  2.8%  1.8%  1.7%  1.2%  1.4%  0.9%\n[libx264 @ 05A2C400] ref B L0: 68.8% 11.4%  5.5%  2.9%  2.3%  1.9%  1.5%  1.1%1.1%  1.0%  0.9%  0.7%  0.5%  0.3%  0.1%\n[libx264 @ 05A2C400] ref B L1: 91.9%  8.1%\n[libx264 @ 05A2C400] kb\/s:2055.88\n<\/code><\/pre>\n<p>As far as I&#8217;m concerned it doesn&#8217;t look that bad to me.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m used to use VirtualDub to encode a video to AVI container with DivX-codec (and MP3 for audio). Now I&#8217;m planning to use ffmpeg to encode videos to MP4 container with h264-codec. What I&#8217;ve figured out is that I need to use libx264 and one of those presets to make anything work. However, I&#8217;m amazed [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5148","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5148","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5148"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5148\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5148"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5148"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}