ffmpeg v4l2 taking 100% cpu usage - ffmpeg

I am on Fedora 37, version is irrelevant as I had same issue on Fedora 36.
I am using the following command to send video from my usb video card to v4l2loopback and the CPU usage goes to 100% and I can't do much more on my computer (i5-8600k, nvidia 2060 rtx, 16GB ram).
ffmpeg -nostdin -threads 1 -f video4linux2 -video_size 1920x1080 -input_format mjpeg -i /dev/video1 -f v4l2 -preset fast -c:v copy /dev/video0
previously the command was like that and the rest of the arguments was added to see if it makes any difference, it seems it doesn't change much
ffmpeg -f video4linux2 -video_size 1920x1080 -input_format mjpeg -i /dev/video1 -f v4l2 -c copy /dev/video0
To be honest I don't know much about ffmpeg or video in general, I was using the same command a few months ago on the same hardware with almost 0 impact to my performance, I could play games and watch videos at the same time while now I get 15 FPS in my games while I am using ffmpeg. Did anything change in ffmpeg? Any improvements to my ffmpeg command? Shouldn't ffmpeg not encode anything if I am using pixelformat and frame rate supported by my usb video card and if that is true shouldn't be super light?

Related

record Linux Wayland/DRM screen using ffmpeg's kmsgrab device with superimposed webcam

Setup is Linux, ffmpeg using kabylake iGPU.
I am capturing a running sway instance using the kmsgrab device, which requires the use of a hardware backend to coherently process the image on my hardware. Only VA API fits this bill. I want to overlay the webcam in the bottom right corner during encoding. However, attempts at manipulating the filter graph to accomplish this have been unsuccessful. This is ultimately for Twitch/Tube stream.
Right now, I am actually capturing the webcam to an sdl window, and simply recording the screen using separate instances of ffmpeg. This doesn't actually solve my problem since the window is easily disguised by workspace switching or other windows.
This is the workaround:
#!/usr/bin/env zsh
# record webcam and open it in sdl window
ffmpeg -v quiet -hide_banner \
-re -video_size 640X480 -hwaccel vaapi -vaapi_device /dev/dri/renderD128 -i /dev/video0 \
-vf 'format=nv12,hwupload' -c:v hevc_vaapi -f hevc - \
| ffmpeg -v quiet -i - -f sdl2 - &
# wait for webcam window to open
until swaymsg -t get_tree | grep 'pipe:' &>/dev/null; do
sleep 0.5
done
# position webcam in the bottom right corner of screen using sway
swaymsg floating enable
swaymsg resize set width 320 height 240
swaymsg move position 1580 795
swaymsg focus tiling
#screencast
ffmpeg -format bgra -framerate 60 -f kmsgrab -thread_queue_size 1024 -i - \
-f alsa -ac 2 -thread_queue_size 1024 -i hw:0 \
-vf 'hwmap=derive_device=vaapi,scale_vaapi=w=1920:h=1080:format=nv12' \
-c:v h264_vaapi -g 120 -b:v 3M -maxrate 3M -pix_fmt vaapi_vld -c:a aac -ab 96k -threads $(nproc) \
output.mkv
kill %1
in momento, this may be impossible.
It looks like a limitation in ffmpeg's vaapi backend. I noticed that Intel's QuickSync backend has an overlay_qsv filter, yet there is no equivalent overlay_vaapi.
In theory, there may be a mechanism to download the hardware surface to a software buffer using the hwdownload filter once it's been encoded in hevc_vaapi so as to make use of the software overlay filter to do the superimposition in the last phase of encoding. I will leave this open for now to see if someone can help work toward this end or knows anything more on the topic.
note on QuickSync:
After building with QuickSync support and attempting to use it, it appears to lack the capability to derive a surface from the kmsgrab device resulting in a function not implemented error from ffmpeg.

ffmpeg NVENC Encoding with -flags:v +ildct shows "No NVENC capable devices found"

So far I use NVENC with ffmpeg for all my encoding successfully. Today I got a new Zotac nVidia GEForce GTX 1660 6GB card.
I get "No NVENC capable devices found" error from ffmpeg. Only if I use -flags:v +ildct flag. Without ildct flag no issues except the output is progressive.
I 100% need Interlaced output and I tried most of the Interlaced flags like, -vf tinterlace=interleave_top,fieldorder=tff -x264opts tff=1 Nothing is giving me Interlaced output except -flags +ildct But with "Zotac nVidia GEForce GTX 1660" ffmpeg shows:
No NVENC capable devices found
I even tried all latest nVidia drivers. Nothing helped me.
My pseudo ffmpeg command line is as below:
ffmpeg -i SourceFile.mkv -codec:v h264_nvenc -preset:v slow -flags:v +ildct+cgop -s:v 1920x1080 -ac 2 -ar 48000 -codec:a mp2 -b:a 384k -r 25 -f mp4 -y NewFile.mp4
Is it a bug with nVidia driver or ffmpeg ? Is latest nVidia Turing technology doesn't support Interlaced (very Bad) ?
Could some one help me ?
nVidia inexplicably disabled interlaced encoding on Turing GPU's. See here and here.
I'm waiting for fix, too.

ffmpeg screencasting with webcam

I want to use the screen view of the computer in real-time for a fake webcam. I did the video action on the bottom. But the frames like the video (2.34) are forming on top of each other. How can I solve this problem.
https://www.youtube.com/watch?v=6AZRiW3hHrw
Load the module
sudo modprobe v4l2loopback exclusive_caps=1
Find the dummy device
v4l2-ctl --list-devices
Start the virtual-webcam (change "/dev/video1" to reflect your system)
ffmpeg -f x11grab -r 15 -s 1920x1080 -i :0.0+0,0 -vcodec rawvideo -pix_fmt yuv420p -threads 0 -f v4l2 /dev/video1

" No NVENC capable devices found" with ffmpeg for screen capture and RTP stream

I´m trying to stream my Desktop with RTP using ffmpeg.
libx264 seems to work fine. But I would like to test the performance of a hardware accelerated codec.
ffmpeg -re -f dshow -i video="screen-capture-recorder" -vcodec libx264 -tune zerolatency -preset ultrafast -an -f rtp rtp://192.168.0.1
The NVENC codec works fine in other situations like this
ffmpeg -y -rtbufsize 2000M -f gdigrab -framerate 60 -offset_x 0 -offset_y 0 -video_size 1280x1080 -i desktop -c:v nvenc -preset:v fast -pix_fmt nv12 out.mp4
The Codec also apears in the list of available codecs.
However this command doesn´t work
ffmpeg -re -f dshow -i video="screen-capture-recorder" -vcodec nvenc -preset llhq -an -f rtp rtp://192.168.0.1
My machine is a Windows 10 with GTX 760
I found out what was wrong a few weeks ago.
Ffmpeg was expecting a GPU with CUDA 8, unfortunately GPUs with Kepler archtecture, like my 760, have limited CUDA 8 features.
So NVENC could not properly work. The "solution" is to find a Ffmpeg release with CUDA 7 or 7.5 support.

ffmpeg: Low framerate when capturing with -vcodec mjpeg but not with -vcodec copy

I'm trying to capture video from a webcam, and I find that when I use the -vcodec copy option, it works really well (far better than any other software I've tried). However, I'd like my files to be a bit smaller, and it seems that every attempt I make to compress the video leads to extremely jumpy video. If, for example, I switch the output vcodec to mjpeg, it changes from reporting 15 fps to reporting between 3 and 4 fps. Am I doing something wrong?? Here is the call with -vcodec copy:
ffmpeg -y -f dshow -vcodec mjpeg -s 1184x656 -framerate 25 -i video="HD 720P Webcam" -vcodec copy test.avi
-- which gets me 15 fps. But if I change to mjpeg, I get only 3-4 fps:
ffmpeg -y -f dshow -vcodec mjpeg -s 1184x656 -framerate 25 -i video="HD 720P Webcam" -vcodec mjpeg test.avi
Experimental attempts to put -framerate 25 or -r 25 before test.avi also does nothing to help the situation. I'm not getting any smoother video when experimenting with mpeg4 or libx264 either. Only the copy option gives me smooth video (btw I'm filming my hands playing a piano, so there is a lot of fast motion in the videos).
Help!!!! And thank you...
I don't understand why the framerate drops so much, but you could try a 2 pass approach where you first record it using -vcodec copy (as you pasted in the question)
ffmpeg -y -f dshow -vcodec mjpeg -s 1184x656 -framerate 25 -i video="HD 720P Webcam" -vcodec copy test.avi
Then transcode it into mjpeg once it's done (something like this):
ffmpeg -i test.avi -vcodec mjpeg test.mjpeg
note: I haven't actually tested any of the above command lines.
Sounds like your webcam is outputting a variable frame rate stream. Try the below on one of your copy captured files.
ffmpeg -i test.avi -vcodec libx264 -r 30 test.mp4
(You should avoid capturing to AVI, use MKV instead)

Resources