Recent

Author Topic: Problems with calling ffmpeg on linux to record video with a camera  (Read 428 times)

jianwt

  • Full Member
  • ***
  • Posts: 129
I want to use ffmpeg on linux to record video with the camera on my laptop and save the video as MP4 format. ffmpeg has been deployed in linux system, the camera can be identified by ffmpeg, also obtained the "DEV" permission, but only support "yuyv", video input device is "/dev/video0", the following code debugging for a long time can not save the video recording in the camera to the local. Please look at the code, thank you very much!
Code: Pascal  [Select][+][-]
  1.  
  2. unit Unit1;
  3.  
  4. {$mode objfpc}{$H+}
  5.  
  6. interface
  7.  
  8. uses
  9.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, Process, Unix;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     btnStart: TButton;
  17.     btnStop: TButton;
  18.     Memo1: TMemo;
  19.     SaveDialog1: TSaveDialog;
  20.     procedure btnStartClick(Sender: TObject);
  21.     procedure btnStopClick(Sender: TObject);
  22.     procedure FormCreate(Sender: TObject);
  23.   private
  24.   FRecording: Boolean;
  25.     FFmpegProcess: TProcess;
  26.   public
  27.          procedure StartRecording(const FileName: string);
  28.     procedure StopRecording;
  29.   end;
  30.  
  31. var
  32.   Form1: TForm1;
  33.  
  34. implementation
  35.  
  36. {$R *.lfm}
  37.  
  38. { TForm1 }
  39.  
  40. procedure TForm1.FormCreate(Sender: TObject);
  41. begin
  42.     btnStop.Enabled := False;
  43.   FRecording := False;
  44. end;
  45.  
  46. procedure TForm1.btnStartClick(Sender: TObject);
  47. var
  48.   VideoDevice, AudioDevice: string;
  49. begin
  50.   if SaveDialog1.Execute then
  51.   begin
  52.  
  53.     VideoDevice := '/dev/video0';
  54.     AudioDevice := 'hw:0';
  55.  
  56.     if not FileExists(VideoDevice) then
  57.     begin
  58.       ShowMessage('视频设备不存在: ' + VideoDevice);
  59.       Exit;
  60.     end;
  61.  
  62.     StartRecording(SaveDialog1.FileName);
  63.     btnStart.Enabled := False;
  64.     btnStop.Enabled := True;
  65.     FRecording := True;
  66.   end;
  67. end;
  68.  
  69. procedure TForm1.btnStopClick(Sender: TObject);
  70. begin
  71.    StopRecording;
  72. end;
  73.  
  74. procedure TForm1.StartRecording(const FileName: string);
  75. var
  76.   FfmpegCmd: string;
  77. begin
  78.   FFmpegProcess := TProcess.Create(nil);
  79.   FFmpegProcess.Executable := 'ffmpeg';
  80.   FFmpegProcess.Parameters.Add('-y');
  81.   FFmpegProcess.Parameters.Add('-f');
  82.   FFmpegProcess.Parameters.Add('v4l2');
  83.   FFmpegProcess.Parameters.Add('-input_format');
  84.    FFmpegProcess.Parameters.Add('yuyv422');
  85.  // FFmpegProcess.Parameters.Add('mjpeg');
  86.   FFmpegProcess.Parameters.Add('-i');
  87.   FFmpegProcess.Parameters.Add('/dev/video0');  // 视频输入设备
  88.  
  89.   // 添加音频输入(ALSA)
  90.   FFmpegProcess.Parameters.Add('-f');
  91.   FFmpegProcess.Parameters.Add('alsa');
  92.   FFmpegProcess.Parameters.Add('-i');
  93.   FFmpegProcess.Parameters.Add('hw:0');         // 音频输入设备
  94.  
  95.   // 设置编码参数
  96.   FFmpegProcess.Parameters.Add('-c:v');
  97.   FFmpegProcess.Parameters.Add('libx264');
  98.   FFmpegProcess.Parameters.Add('-preset');
  99.   FFmpegProcess.Parameters.Add('fast');
  100.   FFmpegProcess.Parameters.Add('-c:a');
  101.   FFmpegProcess.Parameters.Add('aac');
  102.  
  103.   // 输出文件
  104.   FFmpegProcess.Parameters.Add(FileName);    //.mp4
  105.  
  106.   FFmpegProcess.Options := [poUsePipes, poNoConsole];
  107.   FFmpegProcess.Execute;
  108. end;
  109.  
  110. procedure TForm1.StopRecording;
  111. begin
  112.   if FRecording and Assigned(FFmpegProcess) then
  113.   begin
  114.     try
  115.  
  116.       FFmpegProcess.Terminate(0);
  117.  
  118.      // FFmpegProcess.Write('q' + LineEnding, 1 + Length(LineEnding));
  119.       FFmpegProcess.WaitOnExit;
  120.  
  121.       if FFmpegProcess.ExitStatus = 0 then
  122.         ShowMessage('录制成功!')
  123.       else
  124.         ShowMessage('录制失败:' + IntToStr(FFmpegProcess.ExitStatus));
  125.     except
  126.       on E: Exception do
  127.       begin
  128.         ShowMessage('停止录制时出错: ' + E.Message);
  129.       end;
  130.     end;
  131.  
  132.     FreeAndNil(FFmpegProcess);
  133.     btnStart.Enabled := True;
  134.     btnStop.Enabled := False;
  135.     FRecording := False;
  136.   end;
  137. end;
  138. end.        
  139.  
  140.  

d2010

  • Full Member
  • ***
  • Posts: 117
Re: Problems with calling ffmpeg on linux to record video with a camera
« Reply #1 on: February 15, 2025, 07:00:58 am »
Hii.
Please how to read -volume and make animation
     with Read-Volume-Level ?
or
 Play Wav with Tee-Char-Animation bellow?

TRon

  • Hero Member
  • *****
  • Posts: 4148
Re: Problems with calling ffmpeg on linux to record video with a camera
« Reply #2 on: February 16, 2025, 10:26:52 pm »
@jianwt:
Do the parameters work as expected when you manually invoke ffmpeg from the command-line (not using Lazarus/FPC at all) ? e.g. does ffmpeg then work as expected ?

ffmpeg can be quite quirky with its parameters, especially when using TProcess parameters and/or runcommand.
Today is tomorrow's yesterday.

jianwt

  • Full Member
  • ***
  • Posts: 129
Re: Problems with calling ffmpeg on linux to record video with a camera
« Reply #3 on: February 17, 2025, 01:41:24 am »
@jianwt:
Do the parameters work as expected when you manually invoke ffmpeg from the command-line (not using Lazarus/FPC at all) ? e.g. does ffmpeg then work as expected ?

ffmpeg can be quite quirky with its parameters, especially when using TProcess parameters and/or runcommand.

I really do not know how the "TProcess parameter" runs, so I have not figured out this thing. I have implemented the real-time display of camera content with TImage in "Lazarus/FPC", so I think it is possible to record videos and save them locally with the same principle, but my efforts cannot.

So, please understand the friends to help solve this problem, thank you.

TRon

  • Hero Member
  • *****
  • Posts: 4148
Re: Problems with calling ffmpeg on linux to record video with a camera
« Reply #4 on: February 17, 2025, 08:47:45 pm »
Yes, I understand @jianwt but if you are not able to provide an answer to the question if ffpmeg works as expected from the terminal when using those parameters then I am not able to help.

I do not have the same setup to be able to verify and it is quite a waste of time to pursue something that perhaps does not even work in the first place.

You should at least be able to verify if ffmpeg is capable of accepting all these parameters as expected (to be sure: not by using FPC/Lazarus but only using the terminal/prompt).
Today is tomorrow's yesterday.

 

TinyPortal © 2005-2018