標籤:ant c語言 sync 屬性 type 其他 app store pch 產生
=====================================================
最簡單的基於FFmpeg的移動端範例系列文章列表:
最簡單的基於FFmpeg的移動端範例:Android HelloWorld
最簡單的基於FFmpeg的移動端範例:Android 視頻解碼器
最簡單的基於FFmpeg的移動端範例:Android 視頻解碼器-單個庫版
最簡單的基於FFmpeg的移動端範例:Android 推流器
最簡單的基於FFmpeg的移動端範例:Android 視頻轉碼器
最簡單的基於FFmpeg的移動端範例附件:Android 內建播放器
最簡單的基於FFmpeg的移動端範例附件:SDL Android HelloWorld
最簡單的基於FFmpeg的移動端範例:IOS HelloWorld
最簡單的基於FFmpeg的移動端範例:IOS 視頻解碼器
最簡單的基於FFmpeg的移動端範例:IOS 推流器
最簡單的基於FFmpeg的移動端範例:IOS 視頻轉碼器
最簡單的基於FFmpeg的移動端範例附件:IOS內建播放器
最簡單的基於FFmpeg的移動端範例:Windows Phone HelloWorld
=====================================================
本文記錄Windows Phone平台下基於FFmpeg的HelloWorld程式。該示範範例C語言的源碼來自於《最簡單的基於FFMPEG的Helloworld程式》。相關的概念就不再反覆記錄了。
因為在FFmpeg移動端開發方面僅僅有Android和IOS的實戰經驗。所以我一開始的時候僅僅做了Android和IOS的示範範例程式。
前兩天要參加微軟的Windows 10發布會,會前瀏覽資訊的時候發現Windows 10在視音頻處理方面已經加入了對FFmpeg的原生支援。同一時候微軟還發布了一個開源項目FFmpegInterop。專門用於給Windows 8.1/10 App編譯包括FFmpeg功能的類庫。出於好奇我便下載並搗鼓了一下FFmpegInterop這個project,終於總結了一個FFmpeg在Windows Phone 平台的 HelloWorld的示範範例程式。
Windows Phone平台下使用FFmpeg類庫的說明
Windows Phone平台(Windows App Store。Windows市集)使用FFmpeg類庫的流程例如以下所看到的。
1. 編譯FFmpeg類庫
(1)前提須要安裝VS2013和MSYS2(最好已經能夠成功使用這兩個工具編譯PC上使用的FFmpeg)。
(2)獲得FFmpegInteropproject(該project位於Github上面,地址為https://github.com/Microsoft/FFmpegInterop)。
(3)加入FFmpeg源碼。
從官網上下載源碼後,將源碼解壓到FFmpegInterop的ffmpeg檔案夾下。
(4)以管理員的身份啟動“VS2013 開發人員命令提示”控制台。切換到FFmpegInterop檔案夾。
(5)配置MSYS2。執行以下命令設定MSYS2_BIN環境變數(這裡依據MSYS2的安裝路徑不同而不同):
set MSYS2_BIN="E:\msys64\usr\bin\bash.exe"
(6)處理一個小Bug。須要把MSYS2中的link.exe改個名字(隨便改一個就可以,比如“link_msys.exe”)。因為MSYS2中的link.exe和VS2013中的link.exe重名了,假設不改名字的話會系統就會錯誤地使用MSYS2的link.exe(而不是VS2013的link.exe)。從而導致編譯失敗。不知道是不是全部的機子都有這個問題,當時確實卡了我一段時間。
(7)執行命令編譯類庫。
直接執行BuildFFmpeg.bat會列印協助菜單。執行以下這條語句就能夠編譯Windows8.1 x86平台的類庫。編譯成功後的類庫位於“ffmpeg\Build\Windows8.1\x86”檔案夾下。
BuildFFmpeg win8.1 x86
執行以下的語句會同一時候編譯Windows8.1 x86和x64平台的類庫。
BuildFFmpeg win8.1 x86 x64
其他的編譯命令不再詳述,能夠查看協助菜單。本步驟獲得的FFmpeg類庫就能夠用於Windows App Store程式的開發了。
PS:這裡產生的dll與平時用於控制台或者MFC程式中的dll是不一樣的。這裡的dll是加了AppContainer 的flag的dll。假設使用普通的控制台程式調用這裡產生的dll。就會報錯“0xc000a200”:
Error 0xc000a200: shows up when regular process (not inside an AppContainer) tries to load DLL that was marked with AppContainer flag。
(8)開啟samples檔案夾下的sln解決方式[可選]。這一步sln解決方式中包括了FFmpegInterop庫源碼project以及一些示範範例程式。這部分的源碼還沒有細看(本文暫不涉及這部分代碼的內容)。
2. 編寫Windows Phone平台下的程式
(1)建立一個“Windows市集”程式。位於“檔案->建立->項目->Visual C++->Windows市集->空白應用程式(XAML)”。
(2)將編譯後的類庫拷貝至該專案檔夾下(注意x86、x64等這些平台要相應)。
建立include檔案夾儲存標頭檔(*.h)。lib檔案夾儲存匯入庫檔案(*.lib);並將動態庫檔案(*.dll)直接拷貝至檔案夾下。
(3)配置類庫。分成以下3步:
a) 標頭檔配置
解決方式資源管理員->按右鍵項目->屬性面板
屬性面板->C/C++->常規->附加元件封裝括檔案夾。輸入“include”(剛才拷貝標頭檔的檔案夾)
b) 匯入庫配置
屬性面板->連結器->常規->附加庫檔案夾,輸入“lib”(剛才拷貝庫檔案的檔案夾)
屬性面板->連結器->輸入->附加依賴項,輸入“avcodec.lib; avformat.lib; avutil.lib; avdevice.lib; avfilter.lib; swresample.lib; swscale.lib”(匯入庫的檔案名稱)
c) 動態庫配置(重要而有特色的一步)
解決方式資源管理員->按右鍵項目->加入->現有項,將dll檔案加入進來
選中每一個dll檔案->按右鍵->屬性->常規->內容,設定為“是”
(4) 測試
a) 編輯介面
在MainPage.xaml中加入一個Buttonbutton,並加入一個“Button_Clicked()”響應函數。
<Page x:Class="testApp.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:testApp" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <StackPanel Margin="120,30,0,0"> <TextBlock HorizontalAlignment="Left" Text="My FFmpeg test" FontSize="36"/> <Button Content="Configure Info" Click="Button_Clicked"/> <TextBlock x:Name="greetingOutput"/> </StackPanel></Page>
b) 編輯代碼
在MainPage.xaml.cpp中加入一個Button_Clicked()的代碼。例如以下所看到的。
void MainPage::Button_Clicked(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e){//greetingOutput->Text = "Hello, Lei";USES_CONVERSION;String ^info = ref new String(A2W(avcodec_configuration()));Windows::UI::Popups::MessageDialog(info).ShowAsync();}
該代碼調用了avcodec_configuration()擷取FFmpeg類庫的配置資訊。然後調用MessageDialog()彈出一個對話方塊顯示這些資訊。注意當中使用了atlconv.h中的一個A2W()的宏。用於把char *轉換為Platform::String。
在MainPage.xaml.cpp頭部加入用到的標頭檔,例如以下所看到的。
#include <atlconv.h>extern "C"{#include "libavcodec/avcodec.h"}
在MainPage.xaml.h加入響應函數的聲明,例如以下所看到的。
public ref class MainPage sealed{public:MainPage();void Button_Clicked(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e);};
假設一切配置都沒有問題的話。Windows App的執行結果例如以所看到的。單擊左上方的button就會彈出FFmpeg類庫的配置資訊。
(5) 其他資源
有關Windows App C++開發的具體資源能夠參考MSDN上的《使用 C++ 建立你的第一個通用 Windows 應用》相關的文章。
源碼
Simplest FFmpeg WinPhone HelloWorld項目的檔案夾結構所看到的。C++源碼位於MainPage.xaml.cpp中。介面布局檔案為MainPage.xaml。
MainPage.xaml是介面布局檔案。內容例如以下所看到的。
<!-- Simplest FFmpeg WinPhone HelloWorld --><Page x:Class="simplest_ffmpeg_winphone_helloworld.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:simplest_ffmpeg_winphone_helloworld" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <StackPanel Margin="120,30,0,0"> <TextBlock HorizontalAlignment="Left" Text="Simplest FFmpeg WinPhone HelloWorld" FontSize="36"/> <TextBlock Text="Click button to see FFmpeg lib‘s information"/> <StackPanel Orientation="Horizontal" Margin="0,20,0,20"> <Button Content="Protocol" Click="click_protocol" Width="120" HorizontalAlignment="Left"/> <Button Content="AVFormat" Click="click_avformat" Width="120" HorizontalAlignment="Left"/> <Button Content="AVCodec" Click="click_avcodec" Width="120" HorizontalAlignment="Left"/> <Button Content="AVFilter" Click="click_avfilter" Width="120" HorizontalAlignment="Left"/> <Button Content="Configuration" Click="click_configuration" Width="120" HorizontalAlignment="Left"/> </StackPanel> <TextBlock x:Name="information" Margin="0,20,0,20"/> </StackPanel></Page>
MainPage.xaml.cpp是C++函數實現檔案。內容例如以下所看到的。
/** * 最簡單的Windows Phone平台下FFmpeg的HelloWorld範例 * Simplest FFmpeg WinPhone HelloWorld * * 雷霄驊 Lei Xiaohua * [email protected] * 中國傳媒大學/數字電視技術 * Communication University of China / Digital TV Technology * http://blog.csdn.net/leixiaohua1020 * * 本程式是移植FFmpeg到Windows App平台的最簡單程式。它能夠列印出FFmpeg類庫的下列資訊: * Protocol: FFmpeg類庫支援的協議 * AVFormat: FFmpeg類庫支援的封裝格式 * AVCodec: FFmpeg類庫支援的轉碼器 * AVFilter: FFmpeg類庫支援的濾鏡 * Configure: FFmpeg類庫的配置資訊 * * This is the simplest program based on FFmpeg in Windows App Platform. It can show following * informations about FFmpeg library: * Protocol: Protocols supported by FFmpeg. * AVFormat: Container format supported by FFmpeg. * AVCodec: Encoder/Decoder supported by FFmpeg. * AVFilter: Filters supported by FFmpeg. * Configure: configure information of FFmpeg. * */#include "pch.h"#include "MainPage.xaml.h"#include <atlconv.h>#define __STDC_CONSTANT_MACROSextern "C"{#include "libavcodec/avcodec.h"#include "libavformat/avformat.h"#include "libavfilter/avfilter.h"};using namespace simplest_ffmpeg_winphone_helloworld;using namespace Platform;using namespace Windows::Foundation;using namespace Windows::Foundation::Collections;using namespace Windows::UI::Xaml;using namespace Windows::UI::Xaml::Controls;using namespace Windows::UI::Xaml::Controls::Primitives;using namespace Windows::UI::Xaml::Data;using namespace Windows::UI::Xaml::Input;using namespace Windows::UI::Xaml::Media;using namespace Windows::UI::Xaml::Navigation;using namespace Windows::UI::Popups;MainPage::MainPage(){InitializeComponent();}/*** Protocol Support Information*/void MainPage::click_protocol(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e){//FIXstruct URLProtocol;char info[40000] = {0};av_register_all();struct URLProtocol *pup = NULL;//Inputstruct URLProtocol **p_temp = &pup;avio_enum_protocols((void **)p_temp, 0);while ((*p_temp) != NULL){sprintf_s(info, sizeof(info), "%s[In ][%10s]\n", info, avio_enum_protocols((void **)p_temp, 0));}pup = NULL;//Outputavio_enum_protocols((void **)p_temp, 1);while ((*p_temp) != NULL){sprintf_s(info, sizeof(info), "%s[Out][%10s]\n", info, avio_enum_protocols((void **)p_temp, 1));}USES_CONVERSION;String ^infostr = ref new String(A2W(info));information->Text = infostr;}/*** AVFormat Support Information*/void MainPage::click_avformat(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e){char info[40000] = { 0 };av_register_all();AVInputFormat *if_temp = av_iformat_next(NULL);AVOutputFormat *of_temp = av_oformat_next(NULL);//Inputwhile (if_temp != NULL){sprintf_s(info, sizeof(info), "%s[In ] %10s\n", info, if_temp->name);if_temp = if_temp->next;}//Outputwhile (of_temp != NULL){sprintf_s(info, sizeof(info), "%s[Out] %10s\n", info, of_temp->name);of_temp = of_temp->next;}USES_CONVERSION;String ^infostr = ref new String(A2W(info));information->Text = infostr;}/*** AVCodec Support Information*/void MainPage::click_avcodec(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e){char info[40000] = { 0 };av_register_all();AVCodec *c_temp = av_codec_next(NULL);while (c_temp != NULL){if (c_temp->decode != NULL){sprintf_s(info, sizeof(info), "%s[Dec]", info);}else{sprintf_s(info, sizeof(info), "%s[Enc]", info);}switch (c_temp->type){case AVMEDIA_TYPE_VIDEO:sprintf_s(info, sizeof(info), "%s[Video]", info);break;case AVMEDIA_TYPE_AUDIO:sprintf_s(info, sizeof(info), "%s[Audio]", info);break;default:sprintf_s(info, sizeof(info), "%s[Other]", info);break;}sprintf_s(info, sizeof(info), "%s %10s\n", info, c_temp->name);c_temp = c_temp->next;}USES_CONVERSION;String ^infostr = ref new String(A2W(info));information->Text = infostr;}/*** AVFilter Support Information*/void MainPage::click_avfilter(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e){char info[40000] = { 0 };avfilter_register_all();AVFilter *f_temp = (AVFilter *)avfilter_next(NULL);while (f_temp != NULL){sprintf_s(info, sizeof(info), "%s[%10s]\n", info, f_temp->name);}USES_CONVERSION;String ^infostr = ref new String(A2W(info));information->Text = infostr;}/*** Configuration Information*/void MainPage::click_configuration(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e){char info[10000] = { 0 };av_register_all();sprintf_s(info, sizeof(info), "%s\n", avcodec_configuration());USES_CONVERSION;String ^infostr = ref new String(A2W(avcodec_configuration()));//information->Text = infostr;MessageDialog(infostr).ShowAsync();}
執行結果
程式執行後介面例如以所看到的。單擊不同的button會顯示FFmpeg類庫不同方面的資訊。
單擊“Configure”button的時候會以訊息框的形式列印配置資訊。
下載
simplest ffmpeg mobile
項目首頁
Github:https://github.com/leixiaohua1020/simplest_ffmpeg_mobile
開源中國:https://git.oschina.net/leixiaohua1020/simplest_ffmpeg_mobile
SourceForge:https://sourceforge.net/projects/simplestffmpegmobile/
本解決方式包括了使用FFmpeg在移動端處理多媒體的各種範例:
[Android]
simplest_android_player: 基於安卓介面的視頻播放器
simplest_ffmpeg_android_helloworld: 安卓平台下基於FFmpeg的HelloWorld程式
simplest_ffmpeg_android_decoder: 安卓平台下最簡單的基於FFmpeg的視頻解碼器
simplest_ffmpeg_android_decoder_onelib: 安卓平台下最簡單的基於FFmpeg的視頻解碼器-單庫版
simplest_ffmpeg_android_streamer: 安卓平台下最簡單的基於FFmpeg的推流器
simplest_ffmpeg_android_transcoder: 安卓平台下移植的FFmpeg命令列工具
simplest_sdl_android_helloworld: 移植SDL到安卓平台的最簡單程式
[IOS]
simplest_ios_player: 基於IOS介面的視頻播放器
simplest_ffmpeg_ios_helloworld: IOS平台下基於FFmpeg的HelloWorld程式
simplest_ffmpeg_ios_decoder: IOS平台下最簡單的基於FFmpeg的視頻解碼器
simplest_ffmpeg_ios_streamer: IOS平台下最簡單的基於FFmpeg的推流器
simplest_ffmpeg_ios_transcoder: IOS平台下移植的ffmpeg.c命令列工具
simplest_sdl_ios_helloworld: 移植SDL到IOS平台的最簡單程式
[Windows]
simplest_ffmpeg_windowsphone_helloworld: Windows Phone平台下基於FFmpeg的HelloWorld程式
最簡單的基於FFmpeg的移動端範例:Windows Phone HelloWorld