视频录影
  # Smart之视频录影
# 1. 说明
范例通过视频解码的相关组件实现对视频的录制以及转码操作,可输出视频文件。
通过范例学习,可以掌握FSEnCoder录制视频的方式,并将其用作录制工具。
# 2. 设计明细
  开启Smart智慧控制平台,分别加入下插图之控件。或者通过点击菜单栏[文件]-[打开项目]选择项目打开该范例。

  ①:TMemo组件,控件名称为memoLog。
  ②:TSaveDialog组件,控件名称为SaveDialog1。
  ③:TOpenDialog组件,控件名称为OpenDialog1。
  ④:TFSEnCoder组件,控件名称为FSEnCoder1。
  ⑤:TTimer组件,控件名称为Timer1。
  ⑥:TButton组件,控件名称为Button1。
  ⑦:TButton组件,控件名称为Button2。
  ⑧:TComboBox组件,控件名称为cbDeviceType。
  ⑨:TButton组件,控件名称为Button7。
  ⑩:TButton组件,控件名称为Button3。
  (11):TButton组件,控件名称为Button4。
  (12):TButton组件,控件名称为Button6。
  (13):TButton组件,控件名称为Button5。
  (14):TEdit组件,控件名称为edtFOutFileName。
  (15):TButton组件,控件名称为Button9。
  (16):TCheckBox组件,控件名称为CheckBox1。
  (17):TButton组件,控件名称为Button8。
Main窗体属性设置
ClientHeight:窗体客户区高度=427。ClientWidth:窗体客户区宽度=627。
①memoLog属性设置
Height:设置图片高度=355。Width:设置图片宽度=618。
②SaveDialog1属性设置
Filter:设置文件过滤选项=全部文件|*.*。
⑥Button1属性设置、
Height:设置控件高度=25。Width:设置控件宽度=63。Caption:设置按钮显示的文字内容=打开文件。
⑦Button2属性设置
Height:设置控件高度=25。Width:设置控件宽度=63。Caption:设置按钮显示的文字内容=打开URL。
⑧cbDeviceType属性设置
Name:设置控件名称=cbDeviceType。Height:设置控件高度=21。Width:设置控件宽度=97。Items:设置下拉框的选项,双击该属性或者点击属性右侧的[...]打开编辑器,输入选项。
ItemIndex:设置默认选择的选项索引值=0。
⑨Button7属性设置
Height:设置控件高度=25。Width:设置控件宽度=63。Caption:设置按钮显示的文字内容=加载。
⑩Button3属性设置
Height:设置控件高度=25。Width:设置控件宽度=63。Caption:设置按钮显示的文字内容=开始。
(11)Button4属性设置
Height:设置控件高度=25。Width:设置控件宽度=55。Caption:设置按钮显示的文字内容=暂停。
(12)Button6属性设置
Height:设置控件高度=25。Width:设置控件宽度=51。Caption:设置按钮显示的文字内容=恢复。
(13)Button5属性设置
Height:设置控件高度=25。Width:设置控件宽度=59。Caption:设置按钮显示的文字内容=结束。
(14)edtFOutFileName属性设置
Height:设置控件高度=21。Width:设置控件宽度=331。
(15)Button9属性设置
Height:设置控件高度=25。Width:设置控件宽度=75。Caption:设置按钮显示的文字内容=播放录像。
(16)CheckBox1属性设置
Caption:设置选择框显示的文字内容=预览。
(17)Button8属性设置
Height:设置控件高度=25。Width:设置控件宽度=43。Caption:设置按钮显示的文字内容=选择。
# 3. 程序设计
# 3.1. 程序初始设置
该程序无初始设置。
# 3.2. 事件设置
- ⑥Button1-OnClick事件
 
  点击[打开文件]按钮,弹出选择文件对话框,选择文件后,将输入文件的地址回传给编码器控件。
procedure TMyHandler.Button1Click;
begin
  if FThis.OpenDialog1.Execute(0) then
  begin
     FThis.FSEncoder1.InFileName := FTHis.OpenDialog1.FileName;
  end;
end;
 2
3
4
5
6
7
- ⑦Button2-OnClick事件
 
  点击[打开URL]按钮,打开输入URL的对话框,选择后进行URL视频流的解码。
procedure TMyHandler.Button2Click;
var
 URL:string;
begin
    if FThis.FSEncoder1.InFileName = '' then
      URL := 'rtmp://58.200.131.2:1935/livetv/dftv'
    else
      URL := FThis.FSEncoder1.InFileName;
    
    if not InputQuery('Open', 'URL(or Filename)', URL) then
      // cancel open url
      Exit;
     FThis.FSEncoder1.InFileName := URL;    
end;
 2
3
4
5
6
7
8
9
10
11
12
13
14
- ⑨Button7-OnClick事件
 
  点击[加载]按钮,加载视频。
procedure TMyHandler.Button7Click;
begin
  FThis.FSEncoder1.Load;
end;
 2
3
4
- ⑩Button3-OnClick事件
 
  点击[开始]按钮,开始进行视频编码,如果预览选项被勾选,则打开界面显示视频。
procedure TMyHandler.Button3Click;
begin
   FThis.FSEncoder1.PreviewVideo := FThis.CheckBox1.Checked;
   FThis.FSEncoder1.Start(1);
end;
 2
3
4
5
- (11)Button4-OnClick事件
 
  点击[暂停]按钮,暂停视频的编码播放。
procedure TMyHandler.Button4Click;
begin
  FThis.FSEncoder1.Pause;
end;
 2
3
4
- (12)Button6-OnClick事件
 
  点击[恢复]按钮,恢复视频的编码播放。
procedure TMyHandler.Button6Click;
begin
  FThis.FSEncoder1.Resume;
end;
 2
3
4
- (13)Button5-OnClick事件
 
  点击[结束]按钮,停止视频的编码播放。
procedure TMyHandler.Button5Click;
begin
  FThis.FSEncoder1.Stop;
end;
 2
3
4
- ⑧cbDeviceType-OnChange事件
 
  点击设备类型下拉框,选择对应的设备类型。
procedure TMyHandler.cbDeviceTypeChange;
begin
   if FThis.cbDeviceType.Text = 'dtCamera' then
   begin
      FThis.FSEncoder1.DeviceType := dtCamera;
   end
   else if FThis.cbDeviceType.Text = 'dtDesktop' then
   begin
      FThis.FSEncoder1.DeviceType := dtdesktop;
   end
   else if FThis.cbDeviceType.Text = 'dtFile' then
   begin
      FThis.FSEncoder1.DeviceType := dtFile;
   end
   else if FThis.cbDeviceType.Text = 'dtWeb' then
   begin
      FThis.FSEncoder1.DeviceType := dtWeb;
   end;
end;
 2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
- ⑤Timer1-OnTimer事件
 
计时器触发事件,每隔一秒更新日志内容至日志编辑框中。
procedure TMyHandler.Timer1Timer;
begin
  FThis.memoLog.Lines.Assign(FThis.FSEncoder1.Log);
end;
 2
3
4
- (17)Button8-OnClick事件
 
  点击[选择]按钮,打开文件保存选项的对话框,在其中选择目录后确认返回,将输出的文件目录传递至文件输出文本框中。
procedure TMyHandler.Button8Click;
begin
 if FThis.SaveDialog1.Execute(0) then
 begin
   FThis.edtFOutFileName.Text := FThis.SaveDialog1.FileName;
   FThis.FSEncoder1.OutFileName := FThis.edtFOutFileName.Text;
 end;
end;
 2
3
4
5
6
7
8
- (15)Button9-OnClick事件
 
  点击[播放录像]按钮,打开文件输出目录中的文件以进行视频播放。
procedure TMyHandler.Button9Click;
var
 T:TForm;
 F:TFSPlayer;
 P:TPanel;
begin
 T := TForm.Create(nil);
 try
   T.Position := poScreenCenter;
   T.BorderStyle := bsDialog; 
   T.Caption := FThis.FSEncoder1.OutFileName;
   F:=TFSPlayer.Create(T);
   P:=TPanel.Create(T);
   P.Parent := T;
   P.Align := alClient;
   F.VideoPanel := P.Name;
   F.Open(FThis.FSEncoder1.OutFileName,false);
   T.ShowModal;
 finally
   T.Free;
 end;
end;
 2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 4. 运行结果
通过工具栏保存,将程序保存为 sdb 项目文件。
使用鼠标点击工具栏运行(Run),测试运行结果。

  程序运行后,在设备类型处选择输入视频源的类型,如果是本地视频则选择dtFile,并点击[打开文件]按钮以选择视频文件;如果是网络视频则选择dtWeb,并点击[打开URL],在对话框中输入地址。
dtCamera表示视频源来自于相机,dtDesktop表示视频源来自于桌面。选择完成后,点击[加载]按钮,在日志栏中如果出现Can do convert说明视频加载成功,可以进行编解码操作。点击[开始]按钮进行编解码操作,并将解码的视频按钮格式重新进行编码输出至指定的目录。如果预览被勾选则会显示视频预览界面。在编解码的过程中可以选择[暂停]、[恢复]、[结束]按钮进行对应操作。在视频编解码操作结束后,可点击[播放录像]打开对话框查看录制的视频。