Tweaking FFMPEG Stream For Games For Open Source Projech - ffmpeg

Right now I am using FFMPEG to record and broadcast video games on a open source projected call Glitch: https://www.glitch.fun/
The problem is the streams are a little but choppy and pixelated. Here is an example video recording:
https://www.glitch.fun/streams/7e5b7557-d028-4d39-bc59-af3836f7e30d/watchrecording/c147ffda-d26d-4c5f-8be1-a8832ba03dfa
The FFMPEG command I am currently for getting the stream from a source and sending it to a recording file and to destinations is as follows:
ffmpeg [some_input_source] -vf scale=1920x1080 -pix_fmt yuv420p -r 60 -b:v 4000k -maxrate 4000k -bufsize 4500k -vcodec libx264 -preset veryfast -keyint_min 24 -level 3.0 -s 1920:h=1080 -g 40 -x264opts no-scenecut -strict experimental -f [output_location]
What can I tweak in here to improve the quality of streaming for games?

Related

Video size streaming to YouTube with FFMPEG on Raspberry Pi 4

I'm using FFMPEG to stream video to YouTube with my Raspberry Pi 4. It used to work well, but just recently YouTube has started displaying the video smaller than it should be with a black surround as pictured.
preview in YouTube studio
The command I am using is:
ffmpeg -re -i $path -r 24 -g 48 -pix_fmt yuv420p -x264-params kiyint=48:min-keyint=48:scenecut=-1 -video_size 1280x720 -b:v 2000k -b:a 128k -ar 44100 -acodec aac -vcodec h264_v4l2m2m -preset superfast -bufsize 960k -crf 28 -threads 4 -f flv rtmp://a.rtmp.youtube.com/live2/$key
I thought -video_size 1280x720 should set the resolution that FFMPEG is outputting, but it's still displaying as in the picture. The videos themselves are 720p.
I can't work out how to get YouTube and FFMPEG to understand the resolution of the video from one another and have it fill the frame.

ffmpeg "steam" cbr gop

It's about live video streaming to STEAM... with ffmpeg
I have this command
ffmpeg -re -i file-from-webcam.webm -deinterlace
-c:v libx264 -pix_fmt yuv420p -preset veryfast
-tune zerolatency -c:a aac -b:a 128k -ac 2 -strict -2 -crf 18
-r 30 -g 60 -vb 1369k -minrate 1369k -maxrate 1369 -ar 44100 -x264-params "nal-hrd=cbr"
-vf "scale=1280:720" -profile:v main
-f flv "rtmp://ingest-rtmp.broadcast.steamcontent.com/app/steam_...."
but after a few seconds, the stream stops and the log of steam says
Make sure your upload key-frame interval is set to 2 seconds
and use constant bitrate (CBR).
Limit your encoders group of picture (GOP) to at most two times your framerate.
but I do have -x264-params "nal-hrd=cbr" and -r 30 -g 60 framerate 30 GOP 60...
Is there something wrong in the ffmpeg command ?
Or is it linux server related ?
**** The SAME ffmpeg command work very nicely in youtube, twitter, twitch, dlive, facebook, etc...
so what I'm I missing to get it work for steam ?
ffmpeg -re -i file.webm -deinterlace -c:v libx264 -preset veryfast -tune zerolatency -c:a aac -b:a 128k -ac 2 -r 30 -g 60 -vb 1369k -minrate 1369k -maxrate 1369k -bufsize 2730k -ar 44100 -x264-params "nal-hrd=cbr" -vf "scale=1280:720,format=yuv420p" -profile:v main -f flv "rtmp://ingest-rtmp.broadcast.steamcontent.com/app/___key___"
-crf and -b:v/-vb are mutually exclusive. It's likely your -vb was being ignored. Since you want a specific bitrate remove -crf.
-maxrate 1369 was missing the k.
Add -bufsize. See FFmpeg Wiki: Encoding for Streaming Sites.
No need for -strict -2. Users always add that without knowing why. (It was for the old AAC encoder before 2015.)
Make sure your input has audio. Some sites like YouTube require audio. If it does not have audio use the anullsrc filter to generate silent audio.

ffmpeg resulting in no audio and unplayble video

I am trying to get ffmpeg to work as expected however I am having all kinds of trouble getting it to work.
I need to output a webm and h264 for web play. However, the command I am using, while it used to work a few years ago, does not work at all now.
Both my webm and h264 do not have audio, and neither will play in any browser.
My command for webm is:
ffmpeg -y -i "$KMVAR_File" -c:v libvpx -crf 24 -b:v 1000k -vf scale=720:-2 -c:a libvorbis "$KMVAR_webmPath"
and my command for mp4 is:
ffmpeg -y -i "$KMVAR_File" -c:v libx264 -pix_fmt yuv420p -profile:v baseline -level 3.0 -crf 32 -b:v 1M -minrate 1M -maxrate 1M -bufsize 2M -vf scale=720:-2 -c:a aac -strict experimental -movflags +faststart "$KMVAR_mp4Path"
When playing with multiple audio, downmixing or extracting, there's no "one size fit all" solution with ffmpeg.
Look at https://trac.ffmpeg.org/wiki/AudioChannelManipulation as it provides multiple possible solution to your problem.
(I usually go with the pan filter : not the easiest to use, but more powerful than the map_channel approach)

Record and stream desktop to Youtube by ffmpeg with HD resolution

I want to record and stream desktop to Youtube live by FFmpeg. But the output resolution is very low, maximum 360.
What options I need to change?
ffmpeg -framerate 30 -f x11grab -i :1 -f pulse -i default -c:v libx264 -s 1920x1080 -r 60 -b:v 5000k -crf 10 -vf format=yuv420p -c:a aac -b:a 128k -f flv rtmp://a.rtmp.youtube.com/live2/stream_key
Problem
Default size for x11grab is the full desktop or window (640x480 for old ffmpeg versions). Your ffmpeg is old, so it is capturing at 640x480. You are then upscaling 640x480 to 1920x1080 which is bad and looks ugly.
Solution 1: Upgrade ffmpeg
Fix by using a modern ffmpeg version and it will grab the full desktop or window size by default. See FFmpeg Download page for links or the FFmpeg compile and install guides.
Solution 2: Use -video_size input option
ffmpeg -framerate 30 -video_size 1920x1080 -f x11grab -i :0.0 -f pulse -i default -c:v libx264 -b:v 5000k -maxrate 5000k -bufsize 10000k -g 60 -vf format=yuv420p -c:a aac -b:a 128k -f flv rtmp://a.rtmp.youtube.com/live2/stream_key
See the FFmpeg x11grab documentation for more info and options.
For streaming it is recommended to add -g, -bufsize, and -maxrate to enable VBV.

Ffmpeg converts video of double size of original video with second part without audio

I am converting some videos with below command in ffmpeg
ffmpeg -y -i source.mp4 -c:a libfdk_aac -ac 2 -ab 128k -c:v libx264 -x264opts keyint=24:min-keyint=24:no-scenecut -crf 18 -b:v 4000k -maxrate 4000k -bufsize 4000k -vf "scale=-1:1080" destination_1080.mp4
But in some cases the output video is exactly double size of the original and the second part is without audio. Please help.
First thing, there's a flaw in your command. You can't use: -crf 18 -b:v 4000k together, use one or the other.
I don't know what version of FFMPEG you are using, but with the inclusion of: libfdk_aac it's either an old version or one you have self compiled. If I remember correctly that audio codec was taken out of the common builds once the standard aac encoder matured. (I may be wrong about that)
First thing I'd try is to narrow things down a little. From your command line I'm guessing you have a Windows PC, download the latest FFMPEG from HERE
Here are some examples to try: (change audio codec if needed)
BITRATE
ffmpeg -y -i source.mp4 -c:v libx264 -x264opts keyint=24:min-keyint=24:no-scenecut -b:v 4000k -maxrate 4000k -bufsize 4000k -vf "scale=-1:1080" -c:a aac -ac 2 -ab 128k destination_1080_bit.mp4
CRF
ffmpeg -y -i source.mp4 -c:v libx264 -x264opts keyint=24:min-keyint=24:no-scenecut -crf 18 -maxrate 4000k -bufsize 4000k -vf "scale=-1:1080" -c:a aac -ac 2 -ab 128k destination_1080_crf18.mp4
It's interesting you mention the video is twice the size and the audio finishing in the second part.
Your output size will be greater if the bitrate used is higher than the original. Higher the crf value, or lower the bitrate, and have a few tests.
There may also be some FPS issue where the video and audio are mismatched, maybe one thinks the other is at double frame rate. (that's a complete guess)
I would give the above a try first and see how you get on before digging deeper.
Let us know how you get on.
I'll post a new answer, rather than edit other answer, as my first answer is already rather long.
First, I would try to get to the bottom of the problem by starting simple and working my way up on the problematic file/s.
Just copy source to destination:
ffmpeg -i source.mp4 -c copy destination_1.mp4
Is the ouptut as expected? Any errors? What did FFMPEG display?
Copy video - encode audio:
ffmpeg -i source.mp4 -c:v copy -c:a libfdk_aac -ac 2 -ab 128k destination_2.mp4
Is the ouptut as expected? Any errors? What did FFMPEG display?
Encode video (simple) - encode audio:
ffmpeg -i source.mp4 -c:v libx264 -b:v 4000k -maxrate 4000k -bufsize 4000k -c:a libfdk_aac -ac 2 -ab 128k destination_3.mp4
Is the ouptut as expected? Any errors? What did FFMPEG display?
Encode video (+opts) - encode audio:
ffmpeg -i source.mp4 -c:v libx264 -x264opts keyint=24:min-keyint=24:no-scenecut -b:v 4000k -maxrate 4000k -bufsize 4000k -c:a libfdk_aac -ac 2 -ab 128k destination_4.mp4
Is the ouptut as expected? Any errors? What did FFMPEG display?
Encode video (+filter) - encode audio:
ffmpeg -i source.mp4 -c:v libx264 -x264opts keyint=24:min-keyint=24:no-scenecut -b:v 4000k -maxrate 4000k -bufsize 4000k -filter:v "scale=1920:-2" -c:a libfdk_aac -ac 2 -ab 128k destination_5.mp4
Is the ouptut as expected? Any errors? What did FFMPEG display?
If you still get problems it's going to be difficult to fix or make further suggestions without us seeing the output from FFMPEG.
Remember, don't use -crf 18 -b:v 4000k in the same command, one or the other, cfr OR bitrate, not both.
Best of luck...

Resources