x264: Encoded videos need lots of CPU to play - ffmpeg

My computer (Intel Core 2 Duo T9300, 2.5GHz) can usually play any Full HD file, and Blu-Rays perfectly.
However, when I encode a Full HD file myself, the CPU is struggling and frames are dropped.
Here's my command line:
ffmpeg.exe -r 24 -f concat -i list_of_png_files.txt -i w:\audio.wav -acodec copy -c:v libx264 -preset medium -b:v 10000k -shortest output.avi
I tried adding -maxrate 13000k after "-preset medium". However, x264 doesn't seem to honor this - the framerate still reaches 20MBit at parts.
How can I encode videos in such a way as to reduce CPU usage during decoding?

There is an x264 tuning option for exactly this purpose. I believe -tune fastdecode is what you are looking for. But this will disable some compression features, so the video may not look as good without increasing the bitrate.

Related

Ffmpeg nvenc encoder on gpu does not compress files as much as compared to libx264

I wanted to encode a video file which was initially encoded by a libx264 encoder on a non gpu machine with ultrafast preset and crf 23 , i typically re-encode it with preset medium and get a good compression but the process is very slow , so i am considering a gpu based solution
my current command to use ffmpeg on a nvidia turing gpu
ffmpeg -y -vsync passthrough -hwaccel cuda -i a.mp4 -max_muxing_queue_size 9999 -pix_fmt yuv420p -c:v h264_nvenc -preset medium -tune ll -b:v 4M -bufsize 4M -maxrate 10M -qmin 0 b.mp4
usual command i use to do the same
ffmpeg -i a.mp4 -max_muxing_queue_size 9999 -pix_fmt yuv420p -c:v libx264 -preset medium b.mp4
enter code here
How can i make this command do a better job at reducing file size , i am okay to compromise on the quality of the video for a good reduction in size
I would highly recommend reading this H.264 Video Encoding Guide
On the surface, these variants can help you:
Decrease your bitrate
Add -cq option with suitable value 0-51 (-cq for h264_nvenc is pretty the same as -crf for libx264)
Change -tune option value to hq
Try two-pass encoding (if you know desired output file size), but here is very low benefit
If you struggle with available options for h264_nvenc you can see the whole list of them by executing following command:
ffmpeg -hide_banner -h encoder=h264_nvenc
Most of them are self descriptive or similar from libx264

FFMPEG: Youtube streaming quality and speed issues

I am trying to make a reliable stream from my Icecast/Shoutcast servers to Youtube live. The command that I use is:
ffmpeg -v verbose -framerate 30 -loop 1 -i /var/image.jpg -re -i http://127.0.0.1:4700/radio -c:v libx264 -preset ultrafast -b:v 2250k -maxrate 6000k -bufsize 6000k -c:a copy -ab 128k -s 1920x1080 -framerate 30 -g 60 -keyint_min 60 -f flv rtmp://a.rtmp.youtube.com/live2/xxx
As you can see I am using recommended bitrate for Youtube, insert keyframes every 2 seconds and streaming at 30 frames per second.
The stream is working but after running for some time two thing are happening:
FFMPEG speed falls from 1x to something like 0.998x
Youtube starts complaining that video stream speed is slow, markes the quality as bad and sometimes video starts buffering.
Why is this happening? CPU load is normal, connectivity is ok (the stream is running on a 1Gg/s dedicated server).
Since in my example above I am streaming a single image as a logo of the stream I also tried to generate a short 30 seconds video with that image and broadcast that video instead of an image, but that did not help as well.
The command I used for conversion:
ffmpeg -framerate 30 -loop 1 -i /var/image.jpg -c:v libx264 -preset ultrafast -tune stillimage -b:v 2250k -minrate 2250k -maxrate 6000k -bufsize 6000k -framerate 30 -g 60 -keyint_min 60 -t 30 out4.mp4
And broadcast with
ffmpeg -stream_loop -1 -i out4.mp4 -re -i http://127.0.0.1:4700/radio -c:v copy -c:a copy -framerate 30 -g 60 -keyint_min 60 -f flv rtmp://a.rtmp.youtube.com/live2/xxx
ffmpeg version is 4.1.1
Are you sure that your original stream is really keeping up with the wall-clock?
Depending on how it's encoded there are possibilities that it gets heavily skewed. This ultimately leads to buffer under (or overruns if it's too fast) and the player complaining/skipping.
Can you try and dump several hours worth of stream to a file and then stream that with FFmpeg? If that works, then it's a strong indication that your original stream timing (sample rate) is off.
Getting the sample rate right is why professional/expensive sound cards use high precision Quartz-Crystal controlled oscillators. Purely virtual processing (e.g. files get encoded into a stream) can easily get skewed, especially inside virtual machines. Also, cheap USB sound cards are often among the worst offenders in terms of frequency accuracy and stability.
FFmpeg might have an option to deal with too slow input. Keywords could be 'padding' or 'missing samples'.
Youtube's error saying "...buffer....." is not a buffer issue on your PC, but simply data you are sending to youtube is too small.
1)note that [-preset ultrafast] and [-preset fast] does not make big difference.
2) change your ffmpeg comannd for broadcast one. like, [-b:v 2250k] to [-b:v 15000k],and set fps to 12→[-r 12] option.
I's gonna be.
ffmpeg -stream_loop -1 -i out4.mp4 -re -i http://127.0.0.1:4700/radio -preset fast -r 12 -framerate 30 -g 60 -video_track_timescale 1000 -b:v 15000k -f flv rtmp://a.rtmp.youtube.com/live2/xxx
I hope this will be good for you !!(^v^)Y

FFmpeg ignores quantity parameter

this is how I use FFmpeg
ffmpeg -f dshow -i video="UScreenCapture" -vcodec libx264 -q 26 -f flv output.flv
the thing is, the quantity is always 28, ffmpeg ignores that. How to fix this? I need a "flash" codec anyway, to stream to twitch tv
The options -q (and the alias -qscale) are ignored by libx264. If you want to
control the quality,
use:
-crf
ffmpeg -i input -c:v libx264 -crf 22 output.flv
Or set the bitrate with -b:v
ffmpeg -i input -c:v libx264 -b:v 555k output.flv
According to the documentation, "the meaning of q is codec-dependent" and apparently libx264 ignores that option. Use -crf (and a -preset if you want) instead. The bigger the crf value, the lower the quality.
if you wish to generate CQP (constant QP stream), e.g for constant QP=20 i suggest using the following parameters:
'x264-params qp=20:ipratio=1.0:pbratio=1.0:qpstep=0'
Example:
ffmpeg -s 1920x1080 -i test.yuv -vcodec libx264 -x264-params qp=20:ipratio=1.0:pbratio=1.0:qpstep=0 -y test.h264
Notice that 'ipratio=1.0' makes x264 to encode P frame with same QP as I-frame and 'pbratio=1.0' makes x264 to encode B-frame with same QP as P-frame.
The -b options, -q, and -crf seem to do nothing for video qualtiy (at least for my install of ffmpeg version 9), so I am posting a result from another post that gets right to the point
If you want high quality, setting bitrate is a poor way to achieve that. There are many other settings with far bigger influence on quality than bitrate. I would leave the bitrate setting out entirely unless you are having to meet hardware requirements of some sort.
If you are trying to get higher quality, try something like
ffmpeg -i sourcefile.mov -target pal-dvd -qscale 2 -trellis 2 outputfile.mpg
output video size goes from 13Mb for a 2 min video to 130Mb, but it gets the job done.

I only have two presets for ffmpeg x264 in ubuntu 11.04

I'me trying to install (compile) ffmpeg for ubuntu 11.04 by following this guide:
https://ffmpeg.org/trac/ffmpeg/wiki/UbuntuCompilationGuide
Overall it works. Except for some errors with checkinstall due to numbering, which i resolved with this:
http://judsonsnotes.com/notes/index.php?option=com_content&view=section&layout=blog&id=3&Itemid=54&limitstart=40
I'd say it was installed ok.
But when trying to encode some video with -vpre lossless_slow i get this error:
File for preset lossless_slow not found.
And in fact it doesn't exists. All i have is this:
/usr/local/share/ffmpeg:
libvpx-1080p50_60.ffpreset
libvpx-1080p.ffpreset
libvpx-360p.ffpreset
libvpx-720p50_60.ffpreset
libvpx-720p.ffpreset
libx264-ipod320.ffpreset
libx264-ipod640.ffpreset
Where are all the other presets ? in google usually people have many more presets than i do. What did i do wrong ?
From this post i'd say that they shopuld be there: http://git.videolan.org/?p=ffmpeg.git;a=commit;h=4b82e3cedcfc9871671bb613cd979de6995dcb4e
Thanks a lot !
FFmpeg now accesses the x264 internal presets instead of using text files to emulate them. This is easier to maintain and use. Now you must use the -preset option instead of -vpre. Current presets are: ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow, placebo. Ignore placebo as it is a joke and a waste of time.
CRF Example:
ffmpeg -i input -c:v libx264 -preset slow -crf 22 -c:a copy output.mkv
Two-Pass Example:
ffmpeg -i input -c:v libx264 -preset fast -b:v 555k -pass 1 -an -f mp4 - && \
ffmpeg -i input -c:v libx264 -preset fast -b:v 555k -pass 2 -c:a libfaac -b:a 128k output.mp4
These examples come from the x264 Encoding Guide community wiki page at ffmpeg.org.

FFmpeg bitrate issue

I'm dealing with a very big issue about bit rate , ffmpeg provide the -b option for the bit rate and for adjustment it provide -minrate and -maxrate, -bufsize but it don't work proper. If i'm giving 256kbps at -b option , when the trans-coding finishes , it provide the 380kbps. How can we achieve the constant bit rate using ffmpeg. If their is +-10Kb it's adjustable. but the video bit rate always exceed by 50-100 kbps.
I'm using following command
ffmpeg -i "demo.avs" -vcodec libx264 -s 320x240 -aspect 4:3 -r 15 -b 256kb \
-minrate 200kb -maxrate 280kb -bufsize 256kb -acodec libmp3lame -ac 2 \
-ar 22050 -ab 64kb -y "output.mp4"
When trans-coding is done, the Media Info show overall bit rate 440kb (it should be 320kb).
Is their something wrong in the command. Or i have to use some other parameter? Plz provide your suggestion its very important.
Those options don't do what you think they do. From the FFmpeg FAQ:
3.18 FFmpeg does not adhere to the -maxrate setting, some frames are bigger than
maxrate/fps.
Read the MPEG spec about video buffer verifier.
3.19 I want CBR, but no matter what I do frame sizes differ.
You do not understand what CBR is, please read the MPEG spec. Read
about video buffer verifier and constant bitrate. The one sentence
summary is that there is a buffer and the input rate is constant, the
output can vary as needed.
Let me highlight a sentance for you:
The one sentence summary is that there is a buffer and the input rate is constant, the output can vary as needed.
That means, in essence that the -maxrate and other settings don't control the output stream rate like you thought they did.

Resources