Adjusting bit rate in rendered output

Description: Ubuntu 10.04.2 LTS
Release: 10.04
Codename: lucid
Using Sunab's repository
Kdenlive version 0.7.8 KDE on 4.5.3

I would like to use Kdenlive for screen casting and specifically creating a series of tutorials on how to use in Intranet wiki that I have developed.

When I first used Kdenlive I found the recordmydesktop output couldn't be imported. From the bug reports this was something to do with ffmpeg and has supposedly been resolved in later editions of ffmpeg. As an aside I have compiled ffmpeg using the Ubuntu instructions and installed and uninstalled them in a bid to get recordmydesktop to work properly.

http://ubuntuforums.org/showthread.php?t=786095

I then used Sunab's instructions to install an updated Kdenlive and used the medibuntu repositories for the extra codecs (Sunab's recommendation)
http://www.kdenlive.org/forum/kdenkive-078-packaged-ubuntu-maverick-luci...

My present ffmpeg library is the one from the medibuntu repositories.

Output of ffmpeg --formats:

MPEG version SVN-r0.5.1-4:0.5.1-1ubuntu1, Copyright (c) 2000-2009 Fabrice Bellard, et al.
configuration: --extra-version=4:0.5.1-1ubuntu1 --prefix=/usr --enable-avfilter --enable-avfilter-lavf --enable-vdpau --enable-bzlib --enable-libgsm --enable-libschroedinger --enable-libspeex --enable-libtheora --enable-libvorbis --enable-pthreads --enable-zlib --disable-stripping --disable-vhook --enable-runtime-cpudetect --enable-gpl --enable-postproc --enable-swscale --enable-x11grab --enable-libdc1394 --enable-shared --disable-static
libavutil 49.15. 0 / 49.15. 0
libavcodec 52.20. 1 / 52.20. 1
libavformat 52.31. 0 / 52.31. 0
libavdevice 52. 1. 0 / 52. 1. 0
libavfilter 0. 4. 0 / 0. 4. 0
libswscale 0. 7. 1 / 0. 7. 1
libpostproc 51. 2. 0 / 51. 2. 0
built on Mar 4 2010 12:41:55, gcc: 4.4.3

Because on the issues below, I found the following website and followed the instructions here on some simple screen casting..
http://verb3k.wordpress.com/2010/01/26/how-to-do-proper-screencasts-on-l...

Two stages:
ffmpeg -f alsa -ac 2 -i hw:0,0 -f x11grab -r 30 -s 1024x768 -i :0.0 -acodec pcm_s16le -vcodec libx264 -vpre lossless_ultrafast -threads 0 output.mkv

ffmpeg -i output.mkv -acodec libfaac -ab 128k -ac 2 -vcodec libx264 -vpre slow -crf 22 -threads 0 our-final-product.mp4

I found the recording and compression very good with ffmpeg.
-rw-r--r-- 1 cmoman4 cmoman4 537K 2011-03-19 09:41 our-final-product.mp4
-rw-r--r-- 1 cmoman4 cmoman4 3.8M 2011-03-19 09:37 output.mkv

The compressed file is about 1/7 the original size and I am happy with the output quality.

The details of the original file are

Details for the output.mkv
Seems stream 0 codec frame rate differs from container frame rate: 2000.00 (2000/1) -> 30.00 (30/1)
Input #0, matroska, from 'output.mkv':
Duration: 00:00:16.53, start: 0.000000, bitrate: N/A
Stream #0.0: Video: h264, yuv420p, 1024x768, PAR 1:1 DAR 4:3, 30 tbr, 1k tbn, 2k tbc
Stream #0.1: Audio: pcm_s16le, 44100 Hz, stereo, s16, 1411 kb/s

Details for the compressed file are:

Seems stream 0 codec frame rate differs from container frame rate: 60.00 (60/1) -> 30.00 (30/1)
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'our-final-product.mp4':
Duration: 00:00:16.53, start: 0.000000, bitrate: 266 kb/s
Stream #0.0(und): Video: h264, yuv420p, 1024x768, 30 tbr, 30 tbn, 60 tbc
Stream #0.1(und): Audio: aac, 44100 Hz, stereo, s16

However, I would like to use the editing functions of Kdenlive to remove my mistakes as I develop these tutorials for work colleagues.

I created a custom profile as per these instructions
http://www.kdenlive.org/tutorial/kdenlive-how-create-custom-profile

Options
Custom 1024x768
Frame rate 30/1
Pixel Aspect Ratio 1/1
Display Aspect Ratio 4/3
Progressive

I then imported the video made with the instructions above (output.mkv). When I render it, without modification I get a file bigger than the original file! This is using the options found under Render>Rendering.

H.264 I-frame only(video) + AAC (sound)
H.264 lossless slow(video) + AAC (sound)
H.264 lossless fast (video) + AAC (sound)

Output files.
-rw-r--r-- 1 cmoman4 cmoman4 11M 2011-03-19 13:01 kdenlive_H264_I-frame.mp4
-rw-r--r-- 1 cmoman4 cmoman4 7.8M 2011-03-19 13:02 kdenlive_H264_Lossless_fast.mp4
-rw-r--r-- 1 cmoman4 cmoman4 5.1M 2011-03-19 13:02 kdenlive_H264_Lossless_slow.mp4

Using ffmpeg to inspect the files I get

Seems stream 0 codec frame rate differs from container frame rate: 60.00 (60/1) -> 30.00 (30/1)
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'kdenlive_H264_Lossless_slow.mp4':
Duration: 00:00:16.53, start: 0.000000, bitrate: 2549 kb/s
Stream #0.0(und): Video: h264, yuv420p, 1024x768, 30 tbr, 30 tbn, 60 tbc
Stream #0.1(und): Audio: aac, 48000 Hz, stereo, s16

By comparing
"our-final-product.mp4" to
"kdenlive_H264_Lossless_slow.mp4"

I find a large difference in the bitrate 266kps c.f 2549kps. All the other parameters seem to be matching.

kdenlive_H264_Lossless_slow.mp4 is generated with the following render instructions.

f=mp4 acodec=libfaac ab=384k ar=48000 ac=2 pix_fmt=yuv420p vcodec=libx264 cqp=0 me_method=esa subq=8 qmin=10 qcomp=0.6 qdiff=4 qmax=51 coder=ac partitions=+parti4x4+parti8x8+partp4x4+partp8x8+partb8x8 refs=16 flags2=+dct8x8+mixed_refs trellis=1 aspect=%dar

If I insert the bitrate flag (b=200K) the option flag seems to be ignored and the output file is identical.

..... pix_fmt=yuv420p vcodec=libx264 b=200K cqp=0...

If I import the file output.mkv (the original capture) and make a custom render profile trying to match the options mentioned in the tutorial, I lose the video but still get sound when I play it.

f=mp4 acodec=libfaac ab=128k ac=2 vcodec=libx264 vpre=lossless_slow crf=22 threads=0

Assuming Kdenlive is using the same ffmpeg library, I would like to know what parameters to use in my custom render profile to get the same level of compression for "our-final-product.mp4" file mentioned above. I am assuming that is something to do with the quality level being specified by the rendering options.

Similarly,
I have some screencaptures taken using CamStudio and the lossless codec.

http://www.free-codecs.com/download/CamStudio_Lossless_Codec.htm

"When encoding with a third party application, you will have to enable / force keyframes to take advantage of this codec's temporal compression. Otherwise every frame will be stored as a key frame and the resulting file size will be very big." I also read this somewhere within the CamStudio Lossless Codec documentation.

I understand ffmpeg has a -g option but I am unsure of how to apply it to fulfil the above recommendation.

It would be really good (for me at least :) )to develop a rendering profile that can take input from both these sources and render them in h264+aac format but at a bitrate suitable for screen casting.

Any assistance gratefully received.

Thanks very much to developers for Kdenlive. It looks like very useful software.

Thanks again,

Chris

Forums:

To do the equivalent of final-product.mp4 use

f=mp4 acodec=libfaac ab=128k vcodec=libx264 vpre=/usr/share/ffmpeg/libx264-slow.ffpreset crf=22 threads=0

In versions of MLT older than 0.6.0, you must specify the full path to the preset file. A g option is already in the ffpreset.

Big thank you!!!!!

I tweaked your recommendation just slightly to

f=mp4 acodec=libfaac ab=128k vcodec=libx264 vpre=/usr/share/ffmpeg/libx264-lossless_slow.ffpreset crf=22 threads=0

That's brilliant. After about 20hrs of experimenting, tweaking, compiling code and reading all sorts of blogs and forums, I've finally got a workable solution. The compression is really good and quality is definitely good enough.

Thanks for your remarkably quick and accurate response.

Chris

Updating:
In Version 0.8.2.1 and for the last .5 yr that I've been using kdenlive, you can specify the video bit rate instead of using a preset. Here is my preferred format to make a 2-hour 720p movie fit on a DVD in mp4 format. I do have to pre-process the m2ts fie into mp4 using ffmpeg before editing with kdenlive.

f=mp4 hq=1 acodec=libfaac ab=128k ar=48000 pix_fmt=yuv420p vcodec=libx264 minrate=0 vb=4000k g=250 bf=3 b_strategy=1 subcmp=2 cmp=2 coder=1 flags=+loop flags2=dct8x8 qmax=51 subq=7 qmin=10 qcomp=0.6 qdiff=4 trellis=1 aspect=@16/9 pass=2 threads=0 real_time=-1"

I finally got tired of changing the bit rates manually and editing a script to change aac to libfaac every time, and edited the H.264 profile in /usr/share/kde4/apps/kdenlive/export/profiles.xml
---------------------
** Actually I don't know whether threads=0 is any different from threads=8 on my 4-core processor, but I still do edit the script from an 8 to a 0 before running it.