Log in

View Full Version : Why can't I use "copy" for gdigrab with FFMPEG?


mzso
29th October 2016, 13:20
Hi!

Only the avi container is accepted when I try this. But even then the video is totally garbage when I play it back.

I want this because I can't find a sufficiently fast encoder to record 1900*1200 real time without frame drops. But I can't make it work.

mzso
29th October 2016, 14:16
Here's how it looks:
Even the height is wrong, it's 836 instead of 1200 for some reason.
https://abload.de/img/2016-10-29_15-14-20pxq1w.png

mzso
29th October 2016, 14:57
Hmmm...

Ffmpeg accepted "rawvideo". But even that's not fast enough. Maybe I have a memory bottleneck?
The SSD should be able to write the data. (1.6Gb for 15 seconds is not too extreme) Though I guess I shouldn't hit a memory bottleneck either. What certain is that the CPU isn't maxed out.

On second look CPU usage is suspiciously at 25%. So maybe only one thread is used and that's not enough. Sucks...

raffriff42
30th October 2016, 12:18
If you can use OBS Studio (https://obsproject.com/) instead of the bare ffmpeg command line, you will get much better performance (it's like, optimized and stuff)

I get 30 fps encoding rate (dropping half the frames) with the following command lineffmpeg -f gdigrab -framerate 60 -i "desktop" -c:v copy "gdigrab-copy.avi"

With utvideo compression, performance is much worse, only 14 fps. Normally utvideo should have approximately zero performance impact, so something is wrong here. (EDIT see post #6)

Compare to OBS Studio where I get 60 fps very reliably with h.264/NVENC compression.

mzso
30th October 2016, 13:57
If you can use OBS Studio (https://obsproject.com/) instead of the bare ffmpeg command line, you will get much better performance (it's like, optimized and stuff)

I get 30 fps encoding rate (dropping half the frames) with the following command lineffmpeg -f gdigrab -framerate 60 -i "desktop" -c:v copy "gdigrab-copy.avi"

With utvideo compression, I get only 14 fps. Normally utvideo should have approximately zero performance impact, so something is wrong here.

Compare to OBS Studio where I get 60 fps very reliably with h.264/NVENC compression.
Thanks for the tip. I'll have a look

I suspect ffmpeg is just even more bugged than usual for this use...
It's also might be possilbe that 30fps is default if you don't specify for the output.

BTW you get proper video with that CLI? I get that garbled junk I previously shared.

raffriff42
30th October 2016, 16:55
Nope, aside from the framerate issue, the video is fine.
Scratch that, I just got a scrambled one. Seemingly at random.

My report of a lower fps with utvideo was wrong. I had added audio capture, which I didn't think would matter, but it does.
My actual video-only capture framerate (shown as "fps=" in the console and confirmed on playback) has varied between 27.5 ~ 30 fps.

EDIT so ffmpeg gdigrab might be OK for normal (low frame rate) desktop capture applications such as creating tutorials etc -- although I would prefer to use an integrated/optimized/debugged application like OBS or CamStudio.

mzso
30th October 2016, 17:43
Nope, aside from the framerate issue, the video is fine.
Scratch that, I just got a scrambled one. Seemingly at random.

My report of a lower fps with utvideo was wrong. I had added audio capture, which I didn't think would matter, but it does.
My actual video-only capture framerate (shown as "fps=" in the console and confirmed on playback) has varied between 27.5 ~ 30 fps.

EDIT so ffmpeg gdigrab might be OK for normal (low frame rate) desktop capture applications such as creating tutorials etc -- although I would prefer to use an integrated/optimized/debugged application like OBS or CamStudio.

It doesn't only suck with gdibrab. It's just as bad with screen capture recorder....
I also get some spammy "Thread message queue blocking; consider raising the thread_queue_size option (current value: 8)" errors. Though increasing it doesn't help at all.

Sucks... I just wanted a simple recorder for full desktop or partial recording. For which ShareX would be fine, if ffmpeg wouldn't suck.

Music Fan
4th November 2016, 13:00
Hi,
I'm a little bit lost with the ffmpeg options, I don't know how to select audio device, here are some of the available audio devices on my pc ;

#{65e8773d-8f56-11d0-a3b9-00a0c9223196}\global"
[dshow @ 0000000002f6fae0] DirectShow audio devices
[dshow @ 0000000002f6fae0] ""Ce que vous entendez" (2- Crea"
[dshow @ 0000000002f6fae0] Alternative name "@device_cm_{33D9A762-90C8-11D0-
BD43-00A0C911CE86}"Ce que vous entendez" (2- Crea"
[dshow @ 0000000002f6fae0] "Analog Mix (Line/CD/Aux/TAD/PC)"
[dshow @ 0000000002f6fae0] Alternative name "@device_cm_{33D9A762-90C8-11D0-
BD43-00A0C911CE86}\Analog Mix (Line/CD/Aux/TAD/PC)"
[dshow @ 0000000002f6fae0] "Auxiliaire 2 (2- Creative SB Au"
[dshow @ 0000000002f6fae0] Alternative name "@device_cm_{33D9A762-90C8-11D0-
BD43-00A0C911CE86}\Auxiliaire 2 (2- Creative SB Au"
[dshow @ 0000000002f6fae0] "CD Num├®rique (2- Creative SB Au"
[dshow @ 0000000002f6fae0] Alternative name "@device_cm_{33D9A762-90C8-11D0-
BD43-00A0C911CE86}\CD Num├®rique (2- Creative SB Au"
[dshow @ 0000000002f6fae0] "Entr├®e ligne 2/Mic 2 (2- Creati"
[dshow @ 0000000002f6fae0] Alternative name "@device_cm_{33D9A762-90C8-11D0-
BD43-00A0C911CE86}\Entr├®e ligne 2/Mic 2 (2- Creati"
[dshow @ 0000000002f6fae0] "Entr├®e S/PDIF (2- Creative SB A"
[dshow @ 0000000002f6fae0] Alternative name "@device_cm_{33D9A762-90C8-11D0-
BD43-00A0C911CE86}\Entr├®e S/PDIF (2- Creative SB A"
[dshow @ 0000000002f6fae0] "Microphone (2- Creative SB Audi"
[dshow @ 0000000002f6fae0] Alternative name "@device_cm_{33D9A762-90C8-11D0-
BD43-00A0C911CE86}\Microphone (2- Creative SB Audi"


How to select one of them ?
This command works for video only ;
ffmpeg -f gdigrab -framerate 15 -i "desktop" -vcodec utvideo -pix_fmt yuv420p F:\testcapff.avi
I'd like to add sound.

raffriff42
4th November 2016, 14:27
Here is a command line with audio that worked for me (albeit slowly as I said)
ffmpeg -hide_banner
-f gdigrab -framerate 60 -i "desktop"
-f dshow -i audio="Stereo Mix (Realtek High Definition Audio)"
-map 0:0 -map 1:0 -shortest -c:a pcm_s16le
-c:v utvideo -pix_fmt rgb24
"gdigrab-utr.avi"
(line breaks for clarity)

smok3
4th November 2016, 14:41
I have been using this (https://raw.githubusercontent.com/brontosaurusrex/postbang/master/bin/screenrex) (Linux) with fps=25 and gets close to actual 25
ffmpeg -hide_banner -f x11grab -r $fps -s $screenres -i :0.0 \
-vcodec libx264 -preset ultrafast -tune fastdecode -pix_fmt yuv420p -crf 30 -an -y \
"$destination/$(date +"%m%d%Y").$word.$(date +"%H%M")".mp4 -loglevel panic -stats
p.s. quality is kinda crapy and no audio in this case.

@raffriff42, why -pix_fmt rgb24 ?

Music Fan
4th November 2016, 15:57
Here is a command line with audio that worked for me (albeit slowly as I said)
ffmpeg -hide_banner
-f gdigrab -framerate 60 -i "desktop"
-f dshow -i audio="Stereo Mix (Realtek High Definition Audio)"
-map 0:0 -map 1:0 -shortest -c:a pcm_s16le
-c:v utvideo -pix_fmt rgb24
"gdigrab-utr.avi"
(line breaks for clarity)

Ok but I still don't how to select any audio device in the list found with ffmpeg.

raffriff42
4th November 2016, 21:00
p.s. quality is kinda crapy and no audio in this case.

@raffriff42, why -pix_fmt rgb24 ?No wonder, with -crf 30. I would stay within the 18-22 range.
Also, use -preset veryfast if possible; in my experience, ultrafast can compromise quality.

Why rgb24? It's the best!! :) I usually use yuv420; the command line I copied and pasted just happened to use rgb24. It has nothing to do with audio, which is why it wasn't highlighted. EDIT duh, no wonder I had low framerates if I was testing in rgb24.


Ok but I still don't how to select any audio device in the list found with ffmpeg.Your post #8 shows you know how to call list_devices (https://ffmpeg.org/ffmpeg-all.html#Examples-21); all you do is copy one of the listed device names (exactly) and paste it in place of "Stereo Mix..." in my example.

Note, certain sound cards may not have a Stereo Mix or What U hear device enabled by default. Read:
http://www.howtogeek.com/howto/39532/how-to-enable-stereo-mix-in-windows-7-to-record-audio/
http://www.sevenforums.com/tutorials/20595-what-u-hear-recording-enable.html
http://www.tenforums.com/software-apps/23432-audacity-recording-what-u-hear.html

EDIT capturing sound seems to destroy smooth video framerate for me, even at 30fps. If I was to use ffmpeg+gdigrab (not likely, being an OBS Studio (https://obsproject.com/) user) I would capture sound using some other method (Audacity)

Music Fan
5th November 2016, 12:16
all you do is copy one of the listed device names (exactly) and paste it in place of "Stereo Mix..." in my example.
ffmpeg does not find Ce que vous entendez (= "what your ear" in french) or "Ce que vous entendez" (2- Crea (which is written between quotation marks).:o
If I type Analog Mix (Line/CD/Aux/TAD/PC), I get this message ;
[gdigrab @ 0000000003071520] Capturing whole desktop as 1920x1080x32 at (0,0)
Input #0, gdigrab, from 'desktop':
Duration: N/A, start: 1478343601.712364, bitrate: 995334 kb/s
Stream #0:0: Video: bmp, bgra, 1920x1080, 995334 kb/s, 15 tbr, 1000k tbn, 15
tbc
[dshow @ 00000000030750e0] Could not run graph (sometimes caused by a device alr
eady in use by other application)
audio=Analog Mix (Line/CD/Aux/TAD/PC): Input/output error
But this device is not used by any other application.
Anyway I need Ce que vous entendez.
There is maybe a problem between the SoundBlaster driver and ffmpeg.:confused:
Actually it works with HyperCam 2 but I sometimes get audio problems (as if the sound was garbled) thus I'd like to test with ffmpeg. I have this problem only with Windows 7, I didn't have it with XP (with the same audio card).

raffriff42
5th November 2016, 14:48
I'm not sure, but "Analog Mix..." may not work unless something is connected to an analog input.

Guessing that there is an internationalization issue somewhere, try device name "What U Hear"

If that fails, and if it's not too much trouble, try:
* installing English as an alternate language (Control Panel / Language)
* listing ffmpeg devices while temporarily selecting English as your primary language (and saving the list)

EDIT agree with dipje below - use OBS Studio! Works in Windows, OSX and Linux!

dipje
5th November 2016, 18:34
Please remember that gdigrab isn't fast enough at all. gdigrab can't get more than +/- 21fps from my desktop at 1920x1080 and that is without doing anything with it at all (throwing it away so to speak).


The 'Analog Mix' or 'What you Hear' thing are driver-based, and not available on every audio chip (not on my Realtek HDA for instance, and not on my Focusrite Saffire Pro14).

I tried looking for DirectShow desktop-grabbers to replace gdigrab, but those also had issues like proper resolution, framerate or whatever.

In the end I ended up with going (back sort of) with OBS-classic and OBS-studio. Smooth 60fps even with x264 compression the stuff in full-range 4:4:4. When I try to set advanced options in OBS Studio it breaks 4:4:4 mode which is a shame, but 4:2:0 works fine.

I now have OBS Studio set-up to capture to NVENC lossless-HEVC (still 4:2:0, so 'lossless') and I'm happy for now.

Music Fan
6th November 2016, 11:06
Ok, I will try this, I found a french tutorial (quite long) ;
https://www.youtube.com/watch?v=dYA0LgYx8Fk
This program is big ; 95 MB !

Music Fan
6th November 2016, 11:50
I tried this but its GUI is not clear at all ! :o
I didn't find how to select the codec (there is only x264), how to select a region on the screen (with the resolution automatically adapting to this zone), how to open the recording folder ... everything that the old and little HyperCam 2 has.:confused:
And when I record, I only get a black screen :confused:

edit : I found how to have something else than a black screen : in sources, add window capture.
Still searching how to select a region.

raffriff42
6th November 2016, 16:36
> its GUI is not clear at all !
No worse than VirtualDub or any other program - it's hard at first, then becomes easy.
Get the PDF Guide (https://obsproject.com/forum/resources/open-broadcaster-software-studio-multiplatform-help-guide-pdf.365/) to help answer basic questions.

> there is only x264
There are GPU h.264 encoders such as NVENC if you have suitable hardware. Output mode must be set to "Advanced" to see them.
You can also select "custom" ffmpeg encoders (see below), but they will be untested by the developers, and therefore likely to have stutter issues etc.

These are some very rough notes on how I have set up OBS Studio for recording.
There are MANY setup guides out there, this is mine. * suggest run as admin (https://www.google.com/search?q=windows+shortcut+run+as+admin)

* new Scene ('+' button); name "Scene 1" (or whatever)
* new Source ('+'); type=Display Capture; name "Display"
(there are other sources available; use this one to start)
* right-click, properties, capture cursor on/off (your preference)
* new Source ('+'); type=Audio Input; source=<speakers>(*); name "Speakers"
(*) (see my notes above re: "What U Hear", etc)
optional multichannel audio:
* new Source; type=Audio Input; source=<headset>; name "Headset Mic"
* Settings button...
* General button:
Enable system tray icon (will show a red dot when recording)
* Stream button:
(don't care)
* Output button:
* Output mode = Advanced
* Recording tab:
Type = standard
(if you choose "custom ffmpeg" YOU ARE ON YOUR OWN)
Path = <a large hard drive, with plenty of free space, dedicated to recording>
Format = mp4 (you can experiment with other options later)
Audio track = <enable one track per audio source>
Encoder = NVENC H.264 if available
Rescale output = unchecked?
Custom muxer settings = "movflags=frag_keyframe min_frag_duration=4000000"
(this makes the file more robust against damage if anything crashes)
Rate control = QCP
CQP = 16 (or whatever)
Keyframe interval = 0 (auto)
Preset = High performance (available choices vary with selected encoder)
Level = Auto
Use two pass = unchecked
GPU =
B-frames =
* Audio tab:
All tracks, bitrate = 256? (preferred AAC bit rate)
* Audio button:
Sample rate = 48kHz? (preferred sample rate 48/44.1)
Channels = stereo (?)
All "devices" DISabled (use audio Sources as explained above)
* Video button:
Base & output resolution = (your screen resolution)
Downscale = Bicubic (but does not apply if above = screes res.)
(you can downscale later by adding a Filter)
Common FPS values = 60? (preferred)
* Hotkeys button:
Minimum, set Start Recording and Stop Recording hotkeys
* Advanced button:
Priority = normal
* Video section:
Renderer = DX11? (preferred)
Color format = NV12
YUV Color space = 709
Range = Partial
* Recording section:
Filename formatting = "OBS-%CCYY-%MM-%DD-%hh-%mm-%ss"
* Stream section:
(don't care)
(end Settings)
* optional screen region:
right click source name, Filters, add ('+'), Crop/Pad
* optional resizing:
right click source name, Filters, add ('+'), Scaling/Aspect Ratio
* (if using multiple audio sources)
Mixer properties: put audio sources on separate "tracks" (audio streams)

dipje
6th November 2016, 21:06
OBS is not a capture studio, it's meant for streaming. So you can combine overlays of capture devices, webcams, game capture, screen capture, window capture, images, animations etc..

The result can then be encoded at a specified x264 bitrate and directly sent to a streaming service, _OR_ encoded in better archival settings and recorded to file.

So first thing, is to take care that in the settings (in the 'output' section) that you are changing settings for the _Recording_ tab, and not the _Streaming_ tab!!
X264 with near-lossless settings and low-cpu-usage presets are fast enough on modern CPUs to hit 60fps easily. It depends on what the rest of your system is doing though. It has the option to use QuickSync on more modern Intel CPU's (hardware encoder in Intel core 3xxx series and up I believe? Maybe even 2xxx series), NVENC (nvidia hardware encoder on nvidia 6xx/7xx/900/10xx series) and with a separate plugin it support the same on AMD cards as well.

It can be tricky finding the best settings for your system but see it as part of the fun :S.

Remember that in the settings in the 'Video' section is where you set the _final_ output resolution and the FPS. Set 'base' resolution and 'output' resolution to the same to prevent any scaling or whatever.

You can define for up to 4 audio devices, but most often you set up device 1 to point towards your desktop _output_ (which means it captures everything it outputs) and then set your recording setting to record audio device 1.

Then when you close the settings - back in the main screen so to speak - you make sure you add a 'scene' (top left box) and then one box to the right you add 'sources'.
'Display capture' is to capture an entire monitor (you can choose which after you gave the new source a name).
You can also do 'Window Capture' where you pick a process or window title or whatever and it will capture that.

If a 'Window capture' is added to your scene, you see it in the preview portion with a red bounding box. You can drag that bounding box to scale or position the window. You can also right-click it, and in the 'Transform' submenu you can choose 'reset' to go back to the native size, or you can go 'edit' to enter information by numbers if that's what you want. You can also right click a source and go to 'Filters' to add things like Crop, scale / aspect ratio, sharpen, chroma-keying, etc..

They said that only wanting to capture a portion of a source (like, only the top left 200x200 pixels of a monitor) will get easier in next releases, although no word yet when. For now you have to mess with the crop and scale transforms / filters.

And yes, transforms / filters are also changeable on a display-capture.


In the settings 'Output' section, if you set it to 'Advanced' and then go to the 'Recording' tab, you can choose 'custom output (ffmpeg)' where documentation is non-existent and trial-by-error is the key. But here you can chooce different video-encoders and audio-encoders, and is the only way I think (or one of the only ways) to capture raw PCM audio for instance, instead of it always being AAC compressed. The list of video-encoders is not that special though. There are a lot of uncompressed formats (which are real uncompressed) and so not that useful. I'm missing things like utvideo / huffyuv here (that ffmpeg surely supports). This makes x264 with a faster preset or mjpeg the most suitable options I guess (I didn't test much though). dnxhd / prores look nice but those are too slow for realtime 60fps capture in my findings. I didn't try 'vc2' though, might be interesting. Most are just picking x264 or one of the hardware-accelerated variants.

Every time I want to go x264 with the high444 preset and try to go rgb or yuv444 I get a video in which half of it is green, so not good. The only time I managed to avoid that is to keep the output mode to 'simple' and then picking the lossless or near-lossless x264 method in recording (and in the settings in the 'advanced' section make sure you have some I444 or RGB mode selected). If you're OK with 4:2:0 subsampling I found no issues.

Also note that 'obs studio' is the newer version or rewrite. The older OBS version (regular 'OBS', or 'OBS classic' or something) is still downloadable and is pretty much the same thing, although it may be a bit more spartan in the UI. But it might provide some options missing (For now) in the newer OBS studio.

Remember, the interface isn't simple at all for a simple desktop recorder.. since it's not meant to do that, it's meant to be a complete streaming studio solution.. so yes, it seems overcomplicated for what you want of it, but for it's purpose it's very straight forward :).

If you want to know more, better to open a separate topic for it or better yet go to the OBS forums to ask. More people there to help you I'm guessing.. here they know the x264 command line, not every GUI program ever that uses it's code :).

[b]edit:/[b]
Little addition here. I discovered they implemented the 'ALT' key in OBS studio now. In the preview section, you can grab one of the handles of the red bounding box, and with ALT pressed drag the line to _crop_.
So add a display capture, and then while holding ALT you can easily crop to a part you want to capture.

Music Fan
11th November 2016, 13:26
Thanks both of you, that helped me ! ;)

smok3
11th November 2016, 13:53
No wonder, with -crf 30. I would stay within the 18-22 range.

That goes without saying, however experiments show that anything lower than 30 is too much for my CPU (in specific usage case).