Use Delphi to write a general video conversion tool to make video format conversion easier

A simple video format conversion program written in Delphi, which uses TComboBox, TOpenDialog and TSaveDialog components to select the conversion format, select the source video file and select the target video file. The program also uses the TEdit component to allow the user to enter parameters, and then splices the information from these components into a conversion command and runs it in a DOS window.

procedure TForm1.FormCreate(Sender: TObject);
begin
  // 添加选项到ComboBox下拉框中
  ComboBox1.Items.Add('H264视频转ts视频流');
  ComboBox1.Items.Add('H264视频转mp4');
  ComboBox1.Items.Add('ts视频转mp4');
  ComboBox1.Items.Add('mp4视频转flv');
  ComboBox1.Items.Add('转换文件为3GP格式');
  ComboBox1.Items.Add('转换文件为3GP格式 v2');
end;
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ShellAPI;

type
  TForm1 = class(TForm)
    ComboBox1: TComboBox;
    Button1: TButton;
    OpenDialog1: TOpenDialog;
    SaveDialog1: TSaveDialog;
    Edit1: TEdit;
    Edit2: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    function GetDosCommand(Command: string): string;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function TForm1.GetDosCommand(Command: string): string;
begin
  // 将命令转换为可执行的DOS命令
  Result := 'cmd.exe /c ' + Command;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  Command, SourceFile, TargetFile: string;
begin
  // 显示打开文件对话框,选择源视频文件
  if OpenDialog1.Execute then
    SourceFile := OpenDialog1.FileName
  else
    Exit;

  // 显示保存文件对话框,选择目标视频文件
  if SaveDialog1.Execute then
    TargetFile := SaveDialog1.FileName
  else
    Exit;

  // 根据ComboBox的选项获取转换命令
  case ComboBox1.ItemIndex of
    0: Command := 'ffmpeg -i ' + SourceFile + ' -vcodec copy -f mpegts ' + TargetFile; // H264视频转ts视频流
    1: Command := 'ffmpeg -i ' + SourceFile + ' -vcodec copy -f mp4 ' + TargetFile; // H264视频转mp4
    2: Command := 'ffmpeg -i ' + SourceFile + ' -acodec copy -vcodec copy -f mp4 ' + TargetFile; // ts视频转mp4
    3: Command := 'ffmpeg -i ' + SourceFile + ' -acodec copy -vcodec copy -f flv ' + TargetFile; // mp4视频转flv
    4: Command := 'ffmpeg -y -i ' + SourceFile + ' ' + Edit1.Text + ' ' + TargetFile; // 转换文件为3GP格式 
    5: Command := 'ffmpeg -y -i ' + SourceFile + ' -ac 1 -acodec libamr_nb -ar 8000 -ab 12200 -s 176x144 -b 128 -r 15 ' + TargetFile; // 转换文件为3GP格式 v2
  else
    ShowMessage('请选择一个转换格式');
    Exit;
  end;

  // 将Edit1中的参数添加到命令中
  if Trim(Edit1.Text) <> '' then
    Command := Command + ' ' + Edit1.Text;

  // 在DOS窗口中运行转换命令
  Command := GetDosCommand(Command);
  ShellExecute(Handle, 'open', 'cmd.exe', PChar(Command), nil, SW_HIDE);
end;

end.

In the above code, we use the TComboBox component to allow the user to select the conversion format, the TOpenDialog and TSaveDialog components to select the source and target video files, and the TEdit component to allow the user to input parameters. In the OnClick event of Button1, we obtain the conversion command to be executed according to the options of the ComboBox, and add the parameters in Edit1 to the command. Finally, we run the conversion command in a DOS window.

Operate as shown below: C:\delphisource\mytools\runpythoncode\Unit1.pas

  For example, the command generated by selecting mp4 to flv type is as follows:

C:\Users\86182\AppData\Local\JianyingPro\Apps\4.3.1.10241\ffmpeg.exe -i "C:\myApp\deepfakelivetemp\DeepFaceLive_NVIDIA\_internal\DeepFaceLive\build\samples\Asian woman.mp4" -acodec copy -vcodec copy -f flv C:\myimages\outputs\output.flv 

result:

 

Guess you like

Origin blog.csdn.net/winniezhang/article/details/132048674