視訊推流
# Smart之視訊推流
# 1. 說明
範例通過視訊解碼的相關元件實現視訊直播推流的操作。
通過範例學習,可以掌握FSEnCoder進行直播推流的方式,並將其用作視訊直播推流工具。
# 2. 設計明細
開啟Smart智慧控制平臺,分別加入下插圖之控制元件。或者通過點選功能表欄[檔案]-[打開專案]選擇專案打開該範例。

①:TButton元件,控制元件名稱為btnLoad。
②:TButton元件,控制元件名稱為btnStart。
③:TButton元件,控制元件名稱為btnPause。
④:TButton元件,控制元件名稱為btnResume。
⑤:TButton元件,控制元件名稱為btnStop。
⑥:TCheckBox元件,控制元件名稱為ckbPreview。
⑦:TEdit元件,控制元件名稱為edtInputOptions。
⑧:TEdit元件,控制元件名稱為edtInFileName。
⑨:TComboBox元件,控制元件名稱為cbDeviceType。
⑩:TButton元件,控制元件名稱為btnInputFile。
(11):TButton元件,控制元件名稱為FileInfo。
(12):TEdit元件,控制元件名稱為edtOutputOptions。
(13):TEdit元件,控制元件名稱為edtOutFileName。
(14):TSaveDialog元件,控制元件名稱為SaveDialog1。
(15):TOpenDialog元件,控制元件名稱為OpenDialog1。
(16):TFSEnCoder元件,控制元件名稱為FSEnCoder1。
(17):TTimer元件,控制元件名稱為Timer1。
(18):TMemo元件,控制元件名稱為memoLog。
(19):TButton元件,控制元件名稱為btnOutputFile。
Main窗體屬性設定
ClientHeight:窗體客戶區高度=427。ClientWidth:窗體客戶區寬度=627。
①btnLoad屬性設定
Height:設定控制元件高度=25。Width:設定控制元件寬度=73。Name:設定控制元件名稱=btnLoad。Caption:設定按鈕顯示的文字內容=載入。
②btnStart屬性設定
Height:設定控制元件高度=25。Width:設定控制元件寬度=63。Name:設定控制元件名稱=btnStart。Caption:設定按鈕顯示的文字內容=開始。
③btnPause屬性設定
Height:設定控制元件高度=25。Width:設定控制元件寬度=53。Name:設定控制元件名稱=btnPause。Caption:設定按鈕顯示的文字內容=暫停。
④btnResume屬性設定
Height:設定控制元件高度=25。Width:設定控制元件寬度=51。Name:設定控制元件名稱=btnResume。Caption:設定按鈕顯示的文字內容=恢復。
⑤btnStop屬性設定
Height:設定控制元件高度=25。Width:設定控制元件寬度=59。Name:設定控制元件名稱=btnStop。Caption:設定按鈕顯示的文字內容=結束。
⑥ckbPreview屬性設定
Caption:設定選擇框顯示的文字內容=預覽。Name:設定控制元件名稱=ckbPreview。Checked:設定是否顯示勾選,設定為True。
⑦edtInputOptions屬性設定
Height:設定控制元件高度=21。Width:設定控制元件寬度=316。Name:設定控制元件名稱=edtInputOptions。
⑧edtInFileName屬性設定
Height:設定控制元件高度=21。Width:設定控制元件寬度=393。Name:設定控制元件名稱=edtInFileName。
⑨cbDeviceType屬性設定
Name:設定控制元件名稱=cbDeviceType。Height:設定控制元件高度=21。Width:設定控制元件寬度=151。Items:設定下拉框的選項,雙擊該屬性或者點選屬性右側的[...]打開編輯器,輸入選項。
⑩btnInputFile屬性設定
Height:設定控制元件高度=25。Width:設定控制元件寬度=75。Name:設定控制元件名稱=btnInputFile。Caption:設定按鈕顯示的文字內容=Browse...。
(11)FileInfo屬性設定
Height:設定控制元件高度=25。Width:設定控制元件寬度=75。Name:設定控制元件名稱=FileInfo。Caption:設定按鈕顯示的文字內容=FileInfo。
(12)edtOutputOptions屬性設定
Height:設定控制元件高度=21。Width:設定控制元件寬度=547。Name:設定控制元件名稱=edtOutputOptions。
(13)edtOutFileName屬性設定
Height:設定控制元件高度=21。Width:設定控制元件寬度=467。Name:設定控制元件名稱=edtOutFileName。
(14)SaveDialog1屬性設定
Filter:設定檔案過濾選項=全部檔案|*.*。
(16)FSEnCoder1屬性設定
PushFlow:是否開啟推流模式,設定為True。
(18)memoLog屬性設定
Height:設定圖片高度=251。Width:設定圖片寬度=618。
(19)btnOutputFile屬性設定
Height:設定圖片高度=25。Width:設定圖片寬度=75。Name:設定控制元件名稱=btnOutputFile。Caption:設定按鈕顯示的文字內容=Browse...。
# 3. 程式設計
# 3.1. 程式初始設定
設定解碼器的初始模式。
constructor TMyHandler.Create(AOwner: TComponent);
begin
FThis :=TBaseForm(AOwner);
FThis.FSEncoder1.DeviceType := dtCamera;
FThis.edtInputOptions.Text := FThis.FSEncoder1.InputOptions.Text;
FThis.edtOutputOptions.Text := FThis.FSEncoder1.OutPutOptions.Text;
end;
2
3
4
5
6
7
# 3.2. 事件設定
- ①btnLoad-OnClick事件
點選[載入]按鈕,設定輸入輸出選項,並載入視訊。
procedure TMyHandler.btnLoadClick;
begin
FThis.FSEncoder1.PreviewVideo := FThis.ckbPreview.Checked;
FThis.FSEncoder1.InFileName := FThis.edtInFileName.Text;
FThis.FSEncoder1.OutFileName := FThis.edtOutFileName.Text;
FThis.FSEncoder1.InPutOptions.Text := FThis.edtInPutOptions.Text;
FThis.FSEncoder1.OutPutOptions.Text := FThis.edtOutPutOptions.Text;
FThis.FSEncoder1.Load;
end;
2
3
4
5
6
7
8
9
- ②btnStart-OnClick事件
點選[開始]按鈕,開始進行視訊播放並進行推流操作。
procedure TMyHandler.btnStartClick;
begin
FThis.FSEncoder1.Start(1);
end;
2
3
4
- ③btnPause-OnClick事件
點選[暫停]按鈕,暫停視訊的推流操作。
procedure TMyHandler.btnPauseClick;
begin
FThis.FSEncoder1.Pause;
end;
2
3
4
- ④btnResume-OnClick事件
點選[恢復]按鈕,恢復視訊的推流操作。
procedure TMyHandler.btnResumeClick;
begin
FThis.FSEncoder1.Resume;
end;
2
3
4
- ⑤btnStop-OnClick事件
點選[停止]按鈕,停止視訊的推流。
procedure TMyHandler.btnStopClick;
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;
FThis.edtInputOptions.Text := FThis.FSEncoder1.InputOptions.Text;
FThis.edtOutputOptions.Text := FThis.FSEncoder1.OutPutOptions.Text;
end
else if FThis.cbDeviceType.Text = 'dtDesktop' then
begin
FThis.FSEncoder1.DeviceType := dtdesktop;
FThis.edtInputOptions.Text := FThis.FSEncoder1.InputOptions.Text;
FThis.edtOutputOptions.Text := FThis.FSEncoder1.OutPutOptions.Text;
end
else if FThis.cbDeviceType.Text = 'dtFile' then
begin
FThis.FSEncoder1.DeviceType := dtFile;
FThis.edtInputOptions.Text := FThis.FSEncoder1.InputOptions.Text;
FThis.edtOutputOptions.Text := FThis.FSEncoder1.OutPutOptions.Text;
end
else if FThis.cbDeviceType.Text = 'dtWeb' then
begin
FThis.FSEncoder1.DeviceType := dtWeb;
FThis.edtInputOptions.Text := FThis.FSEncoder1.InputOptions.Text;
FThis.edtOutputOptions.Text := FThis.FSEncoder1.OutPutOptions.Text;
end;
end;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
- ⑩btnInputFile-OnClick事件
點選[Browse...]按鈕,打開瀏覽界面,選擇輸入的檔案目錄。
procedure TMyHandler.btnInputFileClick;
begin
if FThis.OpenDialog1.Execute(0) then
begin
FThis.edtInFileName.Text := FThis.OpenDialog1.FileName;
FThis.FSEncoder1.InFileName := FThis.edtInFileName.Text;
end;
end;
2
3
4
5
6
7
8
- (17)Timer1-OnTimer事件
計時器觸發事件,定時更新日誌資訊。
procedure TMyHandler.Timer1Timer;
begin
FThis.memoLog.Lines.Assign(FThis.FSEncoder1.Log);
end;
2
3
4
- (19)btnOutputFile-OnClick事件
點選[Browse..]按鈕,打開瀏覽界面,選擇輸出的檔案目錄。
procedure TMyHandler.btnOutputFileClick;
begin
if FThis.SaveDialog1.Execute(0) then
begin
FThis.edtOutFileName.Text := FThis.SaveDialog1.FileName;
FThis.FSEncoder1.OutFileName := FThis.edtOutFileName.Text;
end;
end;
2
3
4
5
6
7
8
# 4. 運行結果
通過工具欄儲存,將程式儲存為 sdb 專案檔案。
使用滑鼠點選工具欄運行(Run),測試運行結果。

程式運行后,在Device Types處選擇輸入視訊源的型別,如果是本地視訊則選擇dtFile,並點選Input file/URL處的[Browse...]按鈕以選擇視訊檔案;如果是網路視訊則選擇dtWeb,並在Input file/URL文字框中輸入地址。
dtCamera表示視訊源來自於相機,dtDesktop表示視訊源來自於桌面。選擇完成後,點選[載入]按鈕,在日誌欄中如果出現Can do convert說明視訊載入成功,可以進行編解碼操作。點選[開始]按鈕進行編解碼操作,並將解碼的視訊按鈕格式輸出推流至指定地址。如果預覽被勾選則會顯示視訊預覽界面。在編解碼的過程中可以選擇[暫停]、[恢復]、[結束]按鈕進行對應操作。