Recent

Author Topic: FFGrab4Laz = How to record with FFmpeg and Lazarus  (Read 2832 times)

metis

  • Full Member
  • ***
  • Posts: 154
FFGrab4Laz = How to record with FFmpeg and Lazarus
« on: December 03, 2018, 01:56:36 pm »
@ALL, specially @Mike.Cornflake

'FFPlay4Laz' (= play MediaFiles with FFmpeg-Libraries and Lazarus)
http://forum.lazarus.freepascal.org/index.php/topic,26666.0.html
and
'FFInfo4Laz' (= read out MediaFile-Data with FFmpeg-Libraries and Lazarus)
http://forum.lazarus.freepascal.org/index.php/topic,22038.msg208700.html#msg208700
are on the Way.

Now, let's do Recording with FFmpeg and Lazarus - I call this Project FFGrab4Laz.

As a Starter, I wrote two small Apps, which grab the Desktop/Screen with FFmpeg, see
attached 'FFGrab4Laz_01.7z'.
(written with Lazarus v1.8.2 + FPC v3.0.4 and FFmpeg v3.4.2 on WinXP 32bit SP3)

> 'ScreenRecorder2FPC'
is a Console, that grabs the entire Screen to a VideoFile (Preset=25 Frames) with FFmpeg-LIBs.
It's my FPC-Port of the 'Screen recorder':
https://github.com/abdullahfarwees/screen-recorder-ffmpeg-cpp.
The Port is kept as closely as possible to the original C-Code to make it easier to compare with FPC,
just as I did with the Dranger-C-Files (= Decoding&Playing with FFmpeg-LIBs) some Years ago:
http://forum.lazarus.freepascal.org/index.php/topic,26666.msg196377.html#msg196377.
Adaptations and Modifications are denoted as "by moi", so that You can find them easily.

To compile it, You have to download the FFmpeg-PascalHeaders from
http://www.delphiffmpeg.com/headers/.
-> Download 'ffmpeg-delphi-pascal-headers-3.4.2.zip' and copy them into Dir [Libs\ffmpeg-3.4.2].

To run it, You have to download the FFmpeg-DLLs, e.g. from
http://www.delphiffmpeg.com/downloads/ ("Encoder and Player"), and
put them into the same Directory as the compiled Binary, that is [bin_ffmpeg-3.4.2\win32].

For a (Delphi)Pascal-Sample to encode Video and Audio with FFmpeg-LIBs,
open the DelphiProject "..\examples\muxing.dpr" in the downloaded FFmpeg-PascalHeaders-File.


> 'RunFFmpeg'
is a small Tool to check & run the FFmpeg Command Line Tools (= FFmpeg-CLIs):
http://ffmpeg.org/about.html -> GoTo "FFmpeg Tools".
It uses the Lazarus-TProcess-Component and is derived from the Lazarus-ProcessDemo:
http://wiki.lazarus.freepascal.org/Executing_External_Programs -> GoTo "See processdemo example".
(The Link does not work any more; therefore I've put the ProjectFiles into Dir [ProcessDemo] of the attached 7z-File.)

The DemoVersion (see attached 'RunFFmpegDemo.jpg') has preset CommandLines, to
grab any Part of the Screen to a VideoFile (= [Grab]), then check (= [Info]) and play (= [Play]) the File.
If You parse the FFmpeg-ProcessOutput, You can read out whatever You want.
If You change the CommandLines, You can do whatever FFmpeg is able to do, e.g.
[Grab]+[-i "grabbed.mkv" -map v frame%08d.png] will save the File's VideoFrames to PNG-ImageFiles.

'RunFFmpeg' requires 'ffmpeg.exe', 'ffprobe.exe' and 'ffplay.exe', which can be downloaded e.g. from
http://ffmpeg.zeranoe.com/builds/ ("static Builds" or better "shared Builds and DLLs").
To play the recorded File with 'ffplay.exe', You need 'SDL2.dll', too:
http://www.libsdl.org/download-2.0.php -> GoTo "Runtime Binaries".
Put all downloaded Files into the same Directory [bin_ffmpeg-3.4.2\win32].
Use the '-WG Switch' to open 'RunFFmpeg' with or w/o ConsoleWindow.

After Downloading and Compiling the ProjectFiles,
Your FFGrab4Laz-Directory should look as shown in attached 'FFGrab4LazFiles.jpg'.

For how to build FFmpeg-CommandLines in General, see the FFmpeg-Documentation:
http://ffmpeg.org/documentation.html -> GoTo "Command Line Tools Documentation".

For how to build FFmpeg-CommandLines with FPC, see the SourceFiles of 'WinFF', that is
an Open-Source MediaConverter, written with FPC, which uses 'ffmpeg.exe' and 'ffplay.exe' with 'TProcess':
https://www.biggmatt.com/winff/.

The FFmpeg-CommandLine-Arguments for Recording are the same as for Transcoding, only the Input changes.

 :)
« Last Edit: December 05, 2018, 03:03:35 pm by metis »
Life could be so easy, if there weren't those f*** Details.

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1248
Re: FFGrab4Laz = How to record with FFmpeg and Lazarus
« Reply #1 on: December 04, 2018, 12:11:27 am »
Nice work, many thanks
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

metis

  • Full Member
  • ***
  • Posts: 154
Re: FFGrab4Laz = How to record with FFmpeg and Lazarus
« Reply #2 on: December 05, 2018, 06:56:14 pm »
Hint:
'RunFFmpeg' is not only for Grabbing. It does anything You want 'FFmpeg' to do for You.

Here is an Example for Streaming with 'RunFFmpeg' on Win10:
Put http://tagesschau-lh.akamaihd.net/i/tagesschau_1@119231/index_1992_av-p.m3u8?sd=10&rebase -x 950 -y 600 -window_title " This is my window title"
into the Line next to [Play] and click [Play].
 :)
« Last Edit: March 28, 2019, 06:34:19 pm by metis »
Life could be so easy, if there weren't those f*** Details.

metis

  • Full Member
  • ***
  • Posts: 154
Re: FFGrab4Laz = How to record with FFmpeg and Lazarus
« Reply #3 on: June 03, 2019, 02:18:42 pm »
And here is an Example to convert a MPEG-Video to a h264/AAC mp4-File with 'RunFFmpeg':
Put -i "input.mpg" -c:v libx264 -b:v 800k -c:a aac -b:a 128k "output.mp4"
into the Line next to [Grab] and click [Grab].

Or the same Conversion with some more Arguments (StreamMapping, Scaling, AspectRatio, AudioPadding):
-i "input.mpg" -map 0:0 -filter:v:0 "scale=1280:720" -c:v:0 libx264 -b:v:0 800k -aspect:v:0 16:9 -map 0:1 -c:a:0 aac -b:a:0 160k -shortest "output.mp4"

 :)
« Last Edit: June 03, 2019, 04:09:04 pm by metis »
Life could be so easy, if there weren't those f*** Details.