Recent

Author Topic: How to use Lazarus to record video from other windows?  (Read 10072 times)

BosseB

  • Sr. Member
  • ****
  • Posts: 468
Re: How to use Lazarus to record video from other windows?
« Reply #30 on: November 27, 2020, 03:54:49 pm »
Sorry for dropping out of here for some time...
I went off to figuring out how to select a specific area of a specific windows (via its handle) visually so I could get the data needed to test ffmpeg usage. But I did not finish that and then the Trump circus distracted me totally...

I see that you have asked me to be more specific, so to clarify from where I try to get my video downloads from:
(Note: when I write video I mean both images and audio...)

1) Youtube videos - easy, just use youtube-dl. I have scripted that for ease of use. It downloads in a very short time too. Can be used also on a number of other sites to download existing full videos.

2) Other sites where youtube-dl fails. Some of these can be done by the FFx add-on "Video DownloadHelper", but for me only with FireFox on Windows. And it records in real time so one must be there exactly when it airs and wait for the full playing time. And it has a 30% failure rate, when one ends the recording it often results in a video where the "moov atom" is missing in the mp4 rendering it unplayable.

3) Youtube live streams - youtube-dl fails here since it cannot rewind to the start of the stream.
Instead I have found another (windows only) tool named yRewind, which can be set to download from a specific time of day for a duration of up to 75 min. It works fine as long as the stream is running, so it is possible to go back on an existing stream and download a specific hour of video.
About 6 months ago I could use Streamlink to do this, but then something happened and now streamlink is incapable of starting the video download from an earlier time even if the Firefox view can be moved backwards hours...

All of the above I have used on open sites without specific logins required.

4) Cable streaming - I also have a cable account available where I can view the shows when logged on and also older shows for maybe a week or so. But to do this I have to be located in the USA so I have to set up my VPN connection there to get the proper IP address in the US, I guess.
With this I can view the content on my Windows PC screen but I would rather do it on my TV. Cannot be done because casting to the TV or to Chromecast is not available.
None of the download tools I have tried work....

So if I can set something up such that the software captures what is playing inside the defined screen/window area plus the corresponding audio into an mp4 file for a specified start/duration time, then I can let it run overnight since what I want to get hold of airs from about 18:00 to 23:30 ET and this is in the middle of the night over here in Europe.
Upon getting up I could then edit out the commercials in preparation for viewing the content...

--
Bo Berglund
Sweden

BosseB

  • Sr. Member
  • ****
  • Posts: 468
Re: How to use Lazarus to record video from other windows?
« Reply #31 on: November 28, 2020, 10:31:00 am »
@Mr.Madguy
Of course, I'm always talking about Recording Video together with Audio and synchronized.

@All of You
Here I recommended BosseB to use this FFmpeg-CommandLine to record a ScreenRegion with Audio:
Code: Text  [Select][+][-]
  1. ffmpeg -f gdigrab -framerate 25 -offset_x 60 -offset_y 200 -video_size 815x443 -show_region 1 -i desktop -f dshow -i audio="SoundcardName" -vcodec libx264 -pix_fmt yuv420p -preset ultrafast "grabbed.mkv"

To record an App's Window, You simply replace
-offset_x 60 -offset_y 200 -video_size 815x443 -show_region 1 -i desktop
with
-i title=AppWindowTitle/Caption

Does anyone know better FFmpeg-CommandLines for faster ScreenRecording with Audio ?
@metis
I have now ways to:
- Put a selection rectangle on screen so the video output can be defined easily
- Record the video (no audio yet) appearing inside this rectangle to an mp4 file using ffmpeg command

But the suggested ffmpeg command does not work because my Windows system does not have an audio device "SoundcardName". It only shows two microphone sources (on the webcam and the microphone input. Neither of these are what I want, I want the system sound from the playing video. I am not annotating a silent movie...

This is what I get when I use the ffmpeg command to list what is available:
Code: Text  [Select][+][-]
  1. >ffmpeg  -hide_banner -list_devices true -f dshow -i dummy
  2. [dshow @ 000001a2acd8ca80] DirectShow video devices (some may be both video and audio devices)
  3. [dshow @ 000001a2acd8ca80]  "Live! Cam Socialize HD AF / ZiiCam (VF0690)"
  4. [dshow @ 000001a2acd8ca80]     Alternative name "@device_pnp_\\?\usb#vid_041e&pid_4086&mi_00#8&3b1b4f9d&0&0000#{65e8773d-8f56-11d0-a3b9-00a0c9223196}\global"
  5. [dshow @ 000001a2acd8ca80]  "USB Video Device"
  6. [dshow @ 000001a2acd8ca80]     Alternative name "@device_pnp_\\?\usb#vid_04f2&pid_b5ee&mi_00#6&217d0a68&0&0000#{65e8773d-8f56-11d0-a3b9-00a0c9223196}\global"
  7. [dshow @ 000001a2acd8ca80] DirectShow audio devices
  8. [dshow @ 000001a2acd8ca80]  "Microphone (VF0690 Live! Cam Socialize HD AF / ZiiCam)"
  9. [dshow @ 000001a2acd8ca80]     Alternative name "@device_cm_{33D9A762-90C8-11D0-BD43-00A0C911CE86}\wave_{53A8FF9D-38FB-4A49-8C89-101C65AF4E02}"
  10. [dshow @ 000001a2acd8ca80]  "Microphone (Conexant ISST Audio)"
  11. [dshow @ 000001a2acd8ca80]     Alternative name "@device_cm_{33D9A762-90C8-11D0-BD43-00A0C911CE86}\wave_{8DCB9CDA-02C2-4A0B-9ED2-7EF64F44E898}"
  12. dummy: Immediate exit requested

It seems like I am almost done now if I only could get the audio into the file.

Maybe you have a suggestion for a suitable addition to my ffmpeg command line to grab the audio being sent to my speakers? Preferably without having to have the sound on for the duration of the recording (can it be muted and still recorded to the file?)
If not then I will have to mute the speaker itself, which is always a possibility of course...
My test command that works well for the video part is this:
Code: Text  [Select][+][-]
  1. ffmpeg -hide_banner -f gdigrab -framerate 30 -offset_x 1104 -offset_y 195 -video_size 777x435 -show_region 1 -i desktop video.mp4
« Last Edit: November 28, 2020, 02:56:38 pm by BosseB »
--
Bo Berglund
Sweden

metis

  • Sr. Member
  • ****
  • Posts: 300
Re: How to use Lazarus to record video from other windows?
« Reply #32 on: November 28, 2020, 11:56:18 pm »
@BosseB

Code: [Select]
Record the video (no audio yet) + my Windows system does not have an audio device "SoundcardName""SoundcardName" is a PlaceHolder for the Name of YOUR SoundCard/AudioDevice.  ;)
Apparently, You succeeded in reading it out with 'ffmpeg.exe' as I explained to You here:
https://forum.lazarus.freepascal.org/index.php/topic,43411.msg382975.html#msg382975
-> GoTo "To get Your Soundcard's Name".

Before You start Recording the Screen, don't forget to set the AudioInput accordingly. Here's the easiest Way to do it:
On WinXP it's 'What U hear' as shown in attached 'WinXP - 'What U hear' enabled for ScreenRecording.jpg'.
For newer Win-OSs, like Your Win10, it's 'Stereo Mix' as explained here:
https://www.trishtech.com/2018/02/record-windows-desktop-screen-with-sound-using-ffmpeg/
-> GoTo "Right-click on speaker icon in Windows system tray and select Recording devices.".

That Site also explains, how to insert the Audio-InputDevice's Name into the FFmpeg-CommandLine.
(In Case of 'FFmpeg' does not recognize the SoundCard's Name, You have to use it's "Alternative name" instead.)

Finally, Your FFmpeg-CommandLine to record a Desktop-Region on Win10 should look like this:
-f gdigrab -framerate 25 -offset_x 1104 -offset_y 195 -video_size 777x435 -show_region 1 -i desktop -f dshow -i audio="Stereo Mix (Conexant ISST Audio)" -vcodec libx264 -pix_fmt yuv420p -preset ultrafast "grabbed.mp4"

Hope it works for You, now.  :)
« Last Edit: November 29, 2020, 12:51:15 am by metis »
Life could be so easy, if there weren't those f*** Details.
My FFmpeg4Lazarus = FFPlay4Laz + FFGrab4Laz + FFInfo4Laz

BosseB

  • Sr. Member
  • ****
  • Posts: 468
Re: How to use Lazarus to record video from other windows?
« Reply #33 on: November 29, 2020, 12:00:04 am »
So now I have found out why there was no audio recording device, it was disabled by Windows 10...
The way to enable it is:
- Open Control Panel/Sound
- Select tab Recording
- If there is no Stereo Mix shown then:
  - Right click the list and select "Show disabled devices"
- Now right click the Stereo Mix and select Enable

Additionally I believe one has to connect a speaker or headphones to the sound output jack.

Now the list of devices contains the Stereo Mix:
Code: Text  [Select][+][-]
  1. >ffmpeg  -hide_banner -list_devices true -f dshow -i dummy
  2. ...
  3. [dshow @ 000002250ce7ca80] DirectShow audio devices
  4. ...
  5. [dshow @ 000002250ce7ca80]  "Stereo Mix (Conexant ISST Audio)"
  6. [dshow @ 000002250ce7ca80]     Alternative name "@device_cm_{33D9A762-90C8-11D0-BD43-00A0C911CE86}\wave_{78482D89-8594-4C3E-8487-0431BF62E050}"
So the device setting in the ffmpeg command becomes in my laptop:
Code: Text  [Select][+][-]
  1. -f dshow -i audio="Stereo Mix (Conexant ISST Audio)"

With this in place I managed to record video and audio to the same mp4 file!  :D

But there are some issues still:
1) The audio volume is rather low, so on playback the volume needs to be raised considerably.
2) The video/audio sync is off by more than half a second, bad lip sync...
3) In both of the tests I have done the video source window suddenly became black after some time (different for these tests) and the recorded video is also black while audio continues.

I am doing the tests manually towards ffmpeg after extracting the coordinates using my window selection utility. So I have opened a command window for this, which spews out pages of scrolling data while recording.
I have no way except Ctrl-C in the window to stop ffmpeg, is that the correct way?
It might be that the window blackening happens when I return the cursor to the command window after some time away, don't know for sure.

Here is the last ffmpeg command I used to record about 4 minutes from the show playback:
Code: Text  [Select][+][-]
  1. ffmpeg -f gdigrab -framerate 25 -offset_x 995 -offset_y 355 -video_size 860x480 -show_region 1 -i desktop  -f dshow -i audio="Stereo Mix (Conexant ISST Audio)" -vcodec libx264 test_b.mp4

The audio part was grabbed from a post on StackExchange....

By the way, is there an ffmpeg command argument to tell it for how long to record, say one hour?

And where is the data stored while recording? The target file shows size 0 all the time while recording is in progress but assumes a normal size as soon as ffmpeg is stopped by Ctrl-C...
I hope it is not keeping it all in memory, because that would set a max recording size....

PS: When posting this reply I was warned that another post had been made so some items here might seem a bit off considering that intermediate post. DS
--
Bo Berglund
Sweden

metis

  • Sr. Member
  • ****
  • Posts: 300
Re: How to use Lazarus to record video from other windows?
« Reply #34 on: November 29, 2020, 12:04:24 am »
@BosseB

Quote
PS: When posting this reply I was warned that another post had been made...
It was me. :D I leave this Post there, though You apparently found out some Things by Yourself, because
it contains some more Infos, that might be interesting for the others.

Quote
With this in place I managed to record video and audio to the same mp4 file!
I told You from the Beginning, that ScreenRecording is easy with 'FFmpeg'.

Quote
The audio volume is rather low
The Input-Volumes/-Mutings are set in the RecordControl, and are independent from the Output-Volumes/-Mutings.
Additionally see the FFmpeg-Documentation: http://ffmpeg.org/ffmpeg-all.html -> GoTo "36.100 volume".

Quote
The video/audio sync is off by more than half a second, bad lip sync...
Should be due to the Latency of Your Soundcard. Got no Sync-Problems with my 'Sound Blaster Audigy'.
'FFmpeg' has CommandLine-Arguments to correct Sync-Issues,
see the FFmpeg-Documentation: http://ffmpeg.org/ffmpeg-all.html:
-> GoTo -itsoffset offset = "the corresponding streams are delayed by the time duration specified in offset."

Quote
no way except Ctrl-C in the window to stop ffmpeg, is that the correct way?
No, Recordings to mp4-Files must be finished with <q> (= quit), as already decribed in Page #1 of this Thread:
https://forum.lazarus.freepascal.org/index.php/topic,52019.msg382776.html#msg382776
-> GoTo "Hint: Currently, there are two Limitations with 'RunFFmpeg':".

Quote
Here is the last ffmpeg command I used to record
Take the one, that I told You above - it has Arguments for faster Recording.
Maybe that solves Your Sync-Problem.

Quote
is there an ffmpeg command argument to tell it for how long to record, say one hour?
see the FFmpeg-Documentation: http://ffmpeg.org/ffmpeg-all.html:
-> GoTo -timelimit duration = "Exit after ffmpeg has been running for duration seconds"
            + -t duration            = "limit the duration of data read from the input file."
            + -to position           = "Stop writing the output or reading the input at position."
            + -fs limit_size        = "Set the file size limit, expressed in bytes."
            + -ss position          = "seeks in this input file to position."
            + -sseof position     = "Like the -ss option but relative to the "end of file"."

Quote
And where is the data stored while recording? ... I hope it is not keeping it all in memory
The File is written directly to Disk after being buffered in Memory.
There are plenty of Ways to adjust the Buffer, like:
fflags nobuffer = "Reduce the latency introduced by buffering during initial input streams analysis."
Take a Look at the FFmpeg-Documentation for more BufferOptions.  :)
« Last Edit: December 01, 2020, 05:10:21 pm by metis »
Life could be so easy, if there weren't those f*** Details.
My FFmpeg4Lazarus = FFPlay4Laz + FFGrab4Laz + FFInfo4Laz

BosseB

  • Sr. Member
  • ****
  • Posts: 468
Re: How to use Lazarus to record video from other windows?
« Reply #35 on: November 29, 2020, 09:17:00 am »
@BosseB

Quote
With this in place I managed to record video and audio to the same mp4 file!
I told You from the Beginning, that ScreenRecording is easy with 'FFmpeg'.

Turns out that you were right!  :D
Thanks a lot!
Quote

Quote
The video/audio sync is off by more than half a second, bad lip sync...
Should be due to the Latency of Your Soundcard. Got no Sync-Problems with my 'Sound Blaster Audigy'.
AFAIK 'FFmpeg' has CommandLine-Arguments to correct Sync-Issues.
When I capture a video working off a VPN connection to the USA the display I grab from has no problems but the resulting video file has about a 700 ms timing difference.
Luckily I had already added a lip sync compensation function into my video editor so I can easily fix it there (using ffmpeg..) and create a new in-sync mp4. It is a quick operation too.
Quote

Quote
no way except Ctrl-C in the window to stop ffmpeg, is that the correct way?
No, Recordings to mp4-Files must be finished with <q> (= quit), as already decribed in Page #1 of this Thread:
https://forum.lazarus.freepascal.org/index.php/topic,52019.msg382776.html#msg382776
-> GoTo "Hint: Currently, there are two Limitations with 'RunFFmpeg':".

OK, so just hitting the q key while the command window is active styops the ffmpeg processing and it exits after creating the mp4 file.
I currently have used Ctrl-C, which sends a terminate message to the process (not the force one) and it actually exits the ffmpeg process AFTER fixing up the mp4 file too.
But I will now start using q instead. :D
Quote

Quote
Here is the last ffmpeg command I used to record
Take the one, that I told You above - it has Arguments for faster Recording.
Maybe that solves Your Sync-Problem.
The argument you refer to is this I assume:
-preset ultrafast
But it made the video "stutter" so I had to take it out...
Now just using the default.
Quote
Quote
is there an ffmpeg command argument to tell it for how long to record, say one hour?
Yes.
I now tested the -t argument and it seems only to work as a stop for ffmpeg if it is placed directly in front of the output file name in the end. But then it is very exact to the second.

Black capture area
I mentioned the grab region going black earlier, now I have tested it more in detail. Here is how it works:
I have the argument -show_region 1 in the ffmpeg command. This creates a rectangle on screen enclosing the captured area, which I thought is a nifty function.

- When I issue the command in the command window and the rectangle appears the command window immediately loses focus but it still displays ffmpeg progress inside of it.
- Exactly at the time I move back to the command window by just selecting it, then the capture area of the screen turns black but capture continues.
- After exiting the ffmpeg command (using Ctrl-C) the black area on the video capture region disappears and the video is again displayed.

So when I figured this out I removed the -show_region argument and now everything proceeds as expected, nothing happens when I select either window.

So the bottom line is: Do not use the -show_region argument except when testing your code.

PS: I created a little utility used to frame the capture region interactively and it produces the correct setting for the argument like this:
Code: Text  [Select][+][-]
  1. -offset_x 995 -offset_y 355 -video_size 860x480
ready to be pasted into the command line for ffmpeg....
So as long as I don't move the browser window where the video plays after using this it will correctly capture even though the border of the capture region is not visible.
« Last Edit: November 29, 2020, 10:11:18 am by BosseB »
--
Bo Berglund
Sweden

metis

  • Sr. Member
  • ****
  • Posts: 300
Re: How to use Lazarus to record video from other windows?
« Reply #36 on: November 29, 2020, 05:48:42 pm »
@BosseB

I made some Edits in my last Post. Check them out to get some more Answers.

Quote
have used Ctrl-C ... and it actually exits the ffmpeg process AFTER fixing up the mp4 file too.
If You see 'FFmpeg' like "Working" (so not Closing immediately), then the File should finish correctly.
So, obviously You may use <Ctrl+c>, too. Try out, whether there are still AV-Sync-Issues when closing properly with <q>.

Quote
The argument you refer to is this I assume: -preset ultrafast
No, it's the -pix_fmt yuv420p, too (YUV is AFAIK the fastest PixelFormat). I have no StutterProblems, at all.

Quote
I now tested the -t argument
I've added some more Options to stop Recording at the End of my last Post - Check them out.

Quote
it seems only to work ... if it is placed directly in front of the output file name in the end
That would be strange. The FFmpeg-Arguments are all delimited by " -", and then read out by a ParserCode, so
the Order shouldn't make any Difference so long as You put them at a valid Place in the CommandLine.

Quote
Black capture area ... Do not use the -show_region argument
Strange - I never had such Problems with this Feature.

Quote
Turns out that you were right!  :D  +  Thanks a lot!
As already mentioned in my FFPlay4Laz-Thread:
"Most Apps, that I've found for ScreenRecording or Converting are nothing else than
 Wizards, which compose a CommandLine and pass it to...FFmpeg, that's all.
 So, if You know, how to use the FFmpeg-CommandLines, You won't need anything else.
 There are lots of WebPages to show, how to do it, mostly for C-Programming, but
 hey, the CommandLines are the same with FPC."


That's why I wrote 'RunFFmpeg'.
It simply uses 'FFmpeg' as is to do anything, 'FFmpeg' can do for You...and this is a lot !
'RunFFmpeg' is like a Base for any App, that executes FFmpeg-Tasks:
By Modifying the FFmpeg-CommandLine, You need only one App, instead of using different Apps for each Task.
By Parsing the FFmpeg-Process's In- and Output, You may adapt that App totally to Your Needs.
There's only one Disadvantage in doing it this Way:
Parsing Input-/Output-Pipes is quite slow, so that the App won't be very responsive, but
that makes no big Difference so long as You are, e.g. Recording or Transcoding, because You start the Process only once.

Things are different with Streaming/Playing:
If You want a really fast and responsive Player, You have to use the FFmpeg-Libraries.
That's what I'm doing in my FFPlay4Laz-Player/Engine.

Have Fun with 'FFmpeg'  :)
« Last Edit: November 29, 2020, 05:56:01 pm by metis »
Life could be so easy, if there weren't those f*** Details.
My FFmpeg4Lazarus = FFPlay4Laz + FFGrab4Laz + FFInfo4Laz

BosseB

  • Sr. Member
  • ****
  • Posts: 468
Re: How to use Lazarus to record video from other windows?
« Reply #37 on: November 30, 2020, 09:21:46 pm »
Quote
Have Fun with 'FFmpeg'

So now I have spent some time to grab video off of a cable-TV stream playing in a FireFox window.
I have used an application I created in Lazarus to accurately find the pos and size of the rect to be grabbed and I have put this data into the ffmpeg command line you proposed except for the -preset item, which I left out.
The resulting videos could be processed in my video editor written using PasLibVlc, which means it borrows functions from VLC.

So I could cut out ads and adjust lip sync etc.

They played well in my editor as well as with VLC.
So I put them on my MiniDlna/web server in order to play on my LG Smart-TV like I have done with other Youtube downloads before.

However here comes the caveat:
These new videos refuse to play on the LG TV, it claims the files are invalid.

So I checked also by looking at them via Firefox toward my webserver (same directories as are on DLNA) and also here they refused to display in the browser, claiming invalid video file.

Finally I also tested to view via KODI running on a Raspberry Pi (again viewing the DLNA export from the server), here Kodi started to "buffer" the videos and there was a progress bar indicating it would take an hour or so...

So the videos can be viewed using my VLC based video editor, VLC itself and amazingly also in the Chrome browser.

But they will not show on the LG SmartTV, FireFox or Kodi on Raspberry Pi.
On my Samsung phone the video did not play if just clicked from the MyFiles app, but when using the VLC phone app it worked (I had copied the mp4 file to the phone).

So what could I do in order to fix this final problem?
I really want to use my TV to view the shows rather than the editor on my laptop...

PS: The videos grabbed from the screen are all much larger files than corresponding videos from YouTube. DS
--
Bo Berglund
Sweden

metis

  • Sr. Member
  • ****
  • Posts: 300
Re: How to use Lazarus to record video from other windows?
« Reply #38 on: December 01, 2020, 05:09:14 pm »
@BosseB

Quote
These new videos refuse to play on the LG TV, it claims the files are invalid ...
This is, what happens, when the FFmpeg-Process was not closed properly with <q>, so that the mp4-File could not be finished.
Some Containers, like mp4, need a Header and a Trailer (see FFmpeg's 'avformat_write_header()' and 'av_write_trailer()').
Without them, the generated File won't be valid (But then the File shouldn't be playable with other Players, of course).
Some other Containers, like AVI and MKV, don't need to be finished, so that they are playable, even if
the FFmpeg-Process was interrupted (brutally), means even if it was not closed properly.

Here some other Possibilities for not being playable...
The Device/Player does not support a Codec and/or the Container.
Take 'ffprobe.exe' or the VLC-Player or my FFmpeg-Player (-> press <i> = Info) to read out VideoData, and
check if Your Device/Player supports all of them.

Or Framerates higher than 25FPS (= Frames per Second) are not supported.
Or the VideoWidth and -Height must be "Power of Two".
Or the AspectRatio can not be arbitrary -> try Recording, e.g. with scale=-2:720
(BTW: The FFPlay4Laz-Player plays and optionally corrects anything :D - Sorry, but had to repeat it, again.)

If not successful, record Your Screen with "safe" Codecs and Container, like
mpeg2/mpeg4 for Video, mp2/mp3 for Audio and AVI/MPG for the ContainerFormat.
"Safe" means in this Context, that any Player - even elder ones - should be able to play them.

If still unsuccessful, ask in a Forum, that is specifically related to FFmpeg-Questions, like SuperUser.
('FFmpeg' does not have an own Forum.)

Quote
The videos grabbed from the screen are all much larger files
With ScreenRecording You will never obtain the Quality and CompressionRate of the original Video. 

BTW:
According to the FFmpeg-LogFile, that You've posted here, there is an "USB Video Device" connected to Your System.
What is that "USB Video Device" ?
If the VideoStream is available there, try to record it from there. Maybe You get a better VideoQuality and better AV-Sync then.

The FFmpeg-CommandLine should be then:
-f dshow -i video="USB Video Device":audio="Stereo Mix (Conexant ISST Audio)" -vcodec libx264 -pix_fmt yuv420p "grabbed.mp4"
Life could be so easy, if there weren't those f*** Details.
My FFmpeg4Lazarus = FFPlay4Laz + FFGrab4Laz + FFInfo4Laz

BosseB

  • Sr. Member
  • ****
  • Posts: 468
Re: How to use Lazarus to record video from other windows?
« Reply #39 on: December 01, 2020, 05:38:51 pm »
@BosseB
Or Framerates higher than 25FPS (= Frames per Second) are not supported.
Or the VideoWidth and -Height must be "Power of Two".
Or the AspectRatio can not be arbitrary -> try Recording, e.g. with scale=-2:720
(BTW: The FFPlay4Laz-Player plays and optionally corrects anything :D - Sorry, but had to repeat it, again.)
I made another set of tests today:
Code: Text  [Select][+][-]
  1. ffmpeg -hide_banner -f gdigrab -framerate 30 -offset_x 1257 -offset_y 330 -video_size 640x360 -i desktop  -f dshow -i audio="Stereo Mix (Conexant ISST Audio)" -vcodec libx264  -pix_fmt yuv420p mtp_20201201_1220.mp4
This means I did the following:
- Reduced the size of the recorded region to 640x360
- Changed framerate back to 30
These changes made the resulting video playable in both FireFox and my LG Smart-TV! :D :D
Quote
BTW:
According to the FFmpeg-LogFile, that You've posted here, there is an "USB Video Device" connected to Your System.
What is that "USB Video Device" ?
That is the Creative webcam sitting on top of my monitor. It is hooked up by USB and has both video and audio devices.

Now that I have recorded several videos I would like to try and connect the extraction to a specific window if that is possible.
So instead of using the desktop as video source I would like to specify a specific window (I have created a tool that finds the handle to a window).

If this is possible I hope I can set the grab rect relative to this window so the window can be moved on screen while recording.
And I also hope that then the video will not be affected by other windows covering the grabbed area, which happens pretty often now when doing other stuff while recording.
Just navigating the taskbar causes Windows to sometimes blank out most of the screen when trying to show how the running applications tied to the icon looks like....

Since you are further along the ffmpeg learning curve you may be able to say if this is possible at all:
1) Can I use the handle of a window as the source of the data?
2) Will the video be messed up in this case if the source window is moved while recording?
3) Can ffmpeg access the video data if the grabbed rect is partially or fully covered by another window?

I am assuming that the process owning the window is still outputting data to it...
« Last Edit: December 01, 2020, 05:40:29 pm by BosseB »
--
Bo Berglund
Sweden

metis

  • Sr. Member
  • ****
  • Posts: 300
Re: How to use Lazarus to record video from other windows?
« Reply #40 on: December 06, 2020, 07:26:14 pm »
@BosseB

Quote
Reduced the size of the recorded region to 640x360 ... made the resulting video playable
You may also use a predefined VideoSize -video_size ega (= 640x350) and/or
a predefined Framerate -framerate pal (= 25FPS).

Quote
a specific window if that is possible
I've already told You to replace
-offset_x 1257 -offset_y 330 -video_size 640x360 -i desktop
with
-i title=AppWindowTitle/Caption.

Quote
the window can be moved on screen while recording + 1) Can I use the handle of a window
With 'x11grab' (= Linux), You can move the recorded Region with the Mouse, see
-follow_mouse in the FFmpeg-Doc http://www.ffmpeg.org/ffmpeg-all.html.
I don't know, either if that works with 'gdigrab', nor if WindowHandles are allowed.
Never tried that - Try it out.

If 'gdigrab' does not meet Your Needs, check out other VideoInput-Devices, like UScreenCapture, or
an other one, see the FFmpeg-Wiki for ScreenRecording -> GoTo "list of alternative devices".
This Site also explains, how to speed up ScreenRecording.

Quote
2) Will the video be messed up... + 3) Can ffmpeg access the video data...
Try it out.  ;)
« Last Edit: December 06, 2020, 08:04:11 pm by metis »
Life could be so easy, if there weren't those f*** Details.
My FFmpeg4Lazarus = FFPlay4Laz + FFGrab4Laz + FFInfo4Laz

BosseB

  • Sr. Member
  • ****
  • Posts: 468
Re: How to use Lazarus to record video from other windows?
« Reply #41 on: February 20, 2021, 02:42:41 pm »
Back again for screen capture since the Internet stream I found back in December to record from using youtube-dl has been taken off-line...
I returned to my tests with ffmpeg and all of the above works fine, except for audio capture!
I can for the life of me not understand why the command I used before no longer works.  >:(

It captures the video for the correct time and using the correct size and looks very good, but the audio is muted even though I can hear it well from my speakers during recording.
Nothing in the mp4 file.

What can be the cause of this strange problem?

Here is the list of devices from ffmpeg view:
Code: Text  [Select][+][-]
  1. ffmpeg -hide_banner -list_devices true -f dshow -i dummy
  2. [dshow @ 0000017bb3b2cac0] DirectShow video devices (some may be both video and audio devices)
  3. [dshow @ 0000017bb3b2cac0]  "Live! Cam Socialize HD AF / ZiiCam (VF0690)"
  4. [dshow @ 0000017bb3b2cac0]     Alternative name "@device_pnp_\\?\usb#vid_041e&pid_4086&mi_00#8&3b1b4f9d&0&0000#{65e8773d-8f56-11d0-a3b9-00a0c9223196}\global"
  5. [dshow @ 0000017bb3b2cac0]  "USB Video Device"
  6. [dshow @ 0000017bb3b2cac0]     Alternative name "@device_pnp_\\?\usb#vid_04f2&pid_b5ee&mi_00#6&217d0a68&0&0000#{65e8773d-8f56-11d0-a3b9-00a0c9223196}\global"
  7. [dshow @ 0000017bb3b2cac0] DirectShow audio devices
  8. [dshow @ 0000017bb3b2cac0]  "Stereo Mix (Conexant ISST Audio)"
  9. [dshow @ 0000017bb3b2cac0]     Alternative name "@device_cm_{33D9A762-90C8-11D0-BD43-00A0C911CE86}\wave_{4A899ABA-476E-4380-AE13-E054139BD9D9}"
  10. [dshow @ 0000017bb3b2cac0]  "Microphone (Conexant ISST Audio)"
  11. [dshow @ 0000017bb3b2cac0]     Alternative name "@device_cm_{33D9A762-90C8-11D0-BD43-00A0C911CE86}\wave_{1486FF19-CE3C-458B-8CA4-59906203E073}"
  12. [dshow @ 0000017bb3b2cac0]  "Microphone (VF0690 Live! Cam Socialize HD AF / ZiiCam)"
  13. [dshow @ 0000017bb3b2cac0]     Alternative name "@device_cm_{33D9A762-90C8-11D0-BD43-00A0C911CE86}\wave_{53A8FF9D-38FB-4A49-8C89-101C65AF4E02}"
  14. dummy: Immediate exit requested

As you can see my enabled Windows mixer is present:
Code: Text  [Select][+][-]
  1. [dshow @ 0000017bb3b2cac0] DirectShow audio devices
  2. [dshow @ 0000017bb3b2cac0]  "Stereo Mix (Conexant ISST Audio)"
  3. [dshow @ 0000017bb3b2cac0]     Alternative name "@device_cm_{33D9A762-90C8-11D0-BD43-00A0C911CE86}\wave_{4A899ABA-476E-4380-AE13-E054139BD9D9}"

And here is how I used the audio source in my ffmpeg command:
Code: Text  [Select][+][-]
  1. ffmpeg -hide_banner -f gdigrab -framerate 30 -offset_x 479 -offset_y 352 -video_size 860x480 -i desktop  -f dshow -i audio="Stereo Mix (Conexant ISST Audio)" -vcodec libx264  -pix_fmt yuv420p -t 60 output.mp4

Any suggestions? I feel blinded since I cannot see why this should fail given that the audio is clearly audible when recording.

I am feeding my speakers from the audio output of a USB-C connected multi-adapter used as a docking station for my laptop. It has all external connections (Audio out, HDMI screen, Ethernet network) so I only have to connect the USB-C to the laptop when it sits on my desk.
And the mixer control affects the level of the audio on my speakers...

EDIT:
Here is the output while running a 60 second capture while also trying to record audio.
As before audio is not present in the resulting mp4 file. :(

Is there something in the output that indicates that the audio capture is having problems?

Code: Text  [Select][+][-]
  1. ffmpeg -hide_banner -f gdigrab -framerate 25 -offset_x 579 -offset_y 352 -video_size 860x480 -i desktop  -f dshow -i audio="Stereo Mix (Conexant ISST Audio)" -vcodec libx264 -pix_fmt yuv420p -t 60 output3.mp4
  2. [gdigrab @ 0000023b1794ef40] Capturing whole desktop as 860x480x32 at (579,352)
  3. Input #0, gdigrab, from 'desktop':
  4.   Duration: N/A, start: 1613836006.600091, bitrate: 330250 kb/s
  5.     Stream #0:0: Video: bmp, bgra, 860x480, 330250 kb/s, 25 fps, 25 tbr, 1000k tbn, 1000k tbc
  6. Guessed Channel Layout for Input Stream #1.0 : stereo
  7. Input #1, dshow, from 'audio=Stereo Mix (Conexant ISST Audio)':
  8.   Duration: N/A, start: 116369.016000, bitrate: 1411 kb/s
  9.     Stream #1:0: Audio: pcm_s16le, 44100 Hz, stereo, s16, 1411 kb/s
  10. Stream mapping:
  11.   Stream #0:0 -> #0:0 (bmp (native) -> h264 (libx264))
  12.   Stream #1:0 -> #0:1 (pcm_s16le (native) -> aac (native))
  13. Press [q] to stop, [?] for help
  14. [libx264 @ 0000023b179c8d00] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2
  15. [libx264 @ 0000023b179c8d00] profile High, level 3.0, 4:2:0, 8-bit
  16. [libx264 @ 0000023b179c8d00] 264 - core 160 - H.264/MPEG-4 AVC codec - Copyleft 2003-2020 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=15 lookahead_threads=2 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
  17. Output #0, mp4, to 'output3.mp4':
  18.   Metadata:
  19.     encoder         : Lavf58.44.100
  20.     Stream #0:0: Video: h264 (libx264) (avc1 / 0x31637661), yuv420p(progressive), 860x480, q=-1--1, 25 fps, 12800 tbn, 25 tbc
  21.     Metadata:
  22.       encoder         : Lavc58.90.100 libx264
  23.     Side data:
  24.       cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: N/A
  25.     Stream #0:1: Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s
  26.     Metadata:
  27.       encoder         : Lavc58.90.100 aac
  28. [gdigrab @ 0000023b1794ef40] Thread message queue blocking; consider raising the thread_queue_size option (current value: 8)
  29. frame= 1500 fps= 25 q=-1.0 Lsize=    3995kB time=00:01:00.01 bitrate= 545.3kbits/s dup=25 drop=8 speed=0.989x
  30. video:3027kB audio:917kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 1.296664%
  31. [libx264 @ 0000023b179c8d00] frame I:6     Avg QP:19.41  size: 48262
  32. [libx264 @ 0000023b179c8d00] frame P:408   Avg QP:21.80  size:  4848
  33. [libx264 @ 0000023b179c8d00] frame B:1086  Avg QP:24.15  size:   766
  34. [libx264 @ 0000023b179c8d00] consecutive B-frames:  1.4%  4.3%  5.8% 88.5%
  35. [libx264 @ 0000023b179c8d00] mb I  I16..4: 11.3% 50.5% 38.2%
  36. [libx264 @ 0000023b179c8d00] mb P  I16..4:  1.5%  2.9%  0.8%  P16..4: 31.7%  8.6%  5.9%  0.0%  0.0%    skip:48.5%
  37. [libx264 @ 0000023b179c8d00] mb B  I16..4:  0.1%  0.1%  0.0%  B16..8: 21.1%  1.1%  0.1%  direct: 0.8%  skip:76.8%  L0:40.0% L1:56.8% BI: 3.3%
  38. [libx264 @ 0000023b179c8d00] 8x8 transform intra:53.7% inter:82.5%
  39. [libx264 @ 0000023b179c8d00] coded y,uvDC,uvAC intra: 50.0% 67.3% 28.1% inter: 6.1% 10.4% 0.5%
  40. [libx264 @ 0000023b179c8d00] i16 v,h,dc,p: 19% 46%  9% 27%
  41. [libx264 @ 0000023b179c8d00] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 20% 20% 28%  4%  5%  5%  5%  5%  7%
  42. [libx264 @ 0000023b179c8d00] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 32% 28%  9%  4%  6%  6%  5%  5%  5%
  43. [libx264 @ 0000023b179c8d00] i8c dc,h,v,p: 47% 30% 19%  4%
  44. [libx264 @ 0000023b179c8d00] Weighted P-Frames: Y:2.2% UV:2.0%
  45. [libx264 @ 0000023b179c8d00] ref P L0: 62.5% 11.2% 18.4%  7.9%  0.0%
  46. [libx264 @ 0000023b179c8d00] ref B L0: 85.9% 10.4%  3.7%
  47. [libx264 @ 0000023b179c8d00] ref B L1: 95.0%  5.0%
  48. [libx264 @ 0000023b179c8d00] kb/s:413.25
  49. [aac @ 0000023b179cac80] Qavg: 44305.996
  50.  


« Last Edit: February 20, 2021, 04:56:11 pm by BosseB »
--
Bo Berglund
Sweden

BosseB

  • Sr. Member
  • ****
  • Posts: 468
Re: How to use Lazarus to record video from other windows?
« Reply #42 on: February 20, 2021, 05:46:55 pm »
UPDATE
It seems like the audio capture is not just connected to the use of the mixer "Stereo Mix (Conexant ISST Audio)" device...

With this enabled and visible in Windows I am not getting any audio until I switch off the speaker output to my docking station!

When this happens I hear the audio (very low volume) emanating from the speaker on my docked laptop under the table. And now the audio is captured into the resulting mp4 file!

In my understanding the output of a mixer device is what its controls are set to. So depending on which sound output device(s) you connect to the output  audio will be passed there.
So I understood the ffmpeg audio device being the mixer would mean that the output of the mixer is what is recorded.
But it really seems like the device ffmpeg uses is really not the stereo mixer output but the speaker INPUT of the built-in speaker!

So here there is the problem of how to actually capture the audio going into the external speakers driven by the audio output on the docking station (connected by USB-C)??
Anyone here knows how to do that?

Another set of capture questions:
1) How can I set a volume scaling factor when recording?
Right now the audio is probably 20 dB too weak.

2) Is it possible to adjust the lip sync while recording?
When I play back the video it gets OK with a -750 ms adjustment. I would prefer this to be applied into the recorded mp4 file directly.
--
Bo Berglund
Sweden

Mr.Madguy

  • Hero Member
  • *****
  • Posts: 844
Re: How to use Lazarus to record video from other windows?
« Reply #43 on: February 20, 2021, 08:59:36 pm »
Stereo mixer is tricky thing. It doesn't capture everything, your system plays. It's hardware "feedback" device, that is provided by your audio chip and therefore it captures only sound, that is played by this audio chip. For example: your computer has, let's say, Realtek audio chip. So only sound, that is played by this audio chip to it's outputs is captured. And it usually happens only if something is connected to audio connectors. Speakers for example. But if you'd connect USB headphones to your notebook and use them as default audio device - nothing would be captured.

That's why I can't wait for virtual microphone to be implemented in OBS. They have virtual camera, but virtual microphone is in development. Till then I have to use stereo mixer to direct sound to some software, that doesn't support audio capturing by itself. And it's clunky way. Because stereo mixer is very limited. OBS is powerful program, that would allow full control over all sound sources on your computer, only if it would support virtual microphone as it's output.
Is it healthy for project not to have regular stable releases?
Just for fun: Code::Blocks, GCC 13 and DOS - is it possible?

BosseB

  • Sr. Member
  • ****
  • Posts: 468
Re: How to use Lazarus to record video from other windows?
« Reply #44 on: February 20, 2021, 09:46:14 pm »
Yes, audio seems to be tricky...
But I found a thread on stackoverflow that discusses capturing video+audio where they mentioned an audio device named "virtual-audio-capturer" and of course it did not work when I tested it, unrecognized...
So I tried googling for it and found this github project from where I downloaded and installed it.

And now I could use the following command to capture a 60 seconds test video including audio:
Code: Text  [Select][+][-]
  1. ffmpeg -hide_banner -f gdigrab -framerate 25 -offset_x 564 -offset_y 85 -video_size 854x480 -i desktop  -f dshow -i audio="virtual-audio-capturer" -vcodec libx264 -pix_fmt yuv420p -t 60 output10.mp4

The resulting video plays just fine, but the audio now present and at the expected volume is not lip-synced.
But in VLC I could adjust the video/audio delay and found that by setting it to 750 ms the audio came into lip-sync.

So now I am looking for a command line argument to ffmpeg to use a preset delay between the channels to compensate for the lip-sync problem while recording.
Any suggestions?
--
Bo Berglund
Sweden

 

TinyPortal © 2005-2018