轉(zhuǎn)換媒體文件代碼 | 您所在的位置:網(wǎng)站首頁(yè) › 屬牛的跟屬兔的婚姻配嗎 › 轉(zhuǎn)換媒體文件代碼 |
轉(zhuǎn)換媒體文件代碼
項(xiàng)目
07/14/2023
可以使用 Windows.Media.Transcoding API 將視頻文件代碼從一種格式轉(zhuǎn)換為另一種格式。 轉(zhuǎn)換代碼是指轉(zhuǎn)換數(shù)字媒體文件,例如將視頻或音頻文件從一種格式轉(zhuǎn)換為另一種格式。 這通常是通過(guò)解碼文件,然后再重新編碼文件實(shí)現(xiàn)的。 例如,你可以將 Windows Media 文件轉(zhuǎn)換為 MP4 文件,以使此文件可以在支持 MP4 格式的便攜式設(shè)備上播放。 或者,你可以將高清視頻文件轉(zhuǎn)換為較低分辨率的文件。 在此情況下,重新編碼的文件可能使用與原始文件相同的編解碼器,但是其編碼配置文件不同。 設(shè)置你的項(xiàng)目以進(jìn)行轉(zhuǎn)換代碼除了由默認(rèn)項(xiàng)目模板引用的命名空間,你還需要引用這些命名空間,以便使用本文中的代碼轉(zhuǎn)換媒體文件代碼。 using Windows.Storage; using Windows.Media.MediaProperties; using Windows.Media.Transcoding; 選擇源文件和目標(biāo)文件應(yīng)用確定要轉(zhuǎn)換代碼的源文件和目標(biāo)文件的方式取決于實(shí)現(xiàn)。 此示例通過(guò)使用 FileOpenPicker 和 FileSavePicker 讓用戶能夠選取源文件和目標(biāo)文件。 var openPicker = new Windows.Storage.Pickers.FileOpenPicker(); openPicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.VideosLibrary; openPicker.FileTypeFilter.Add(".wmv"); openPicker.FileTypeFilter.Add(".mp4"); StorageFile source = await openPicker.PickSingleFileAsync(); var savePicker = new Windows.Storage.Pickers.FileSavePicker(); savePicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.VideosLibrary; savePicker.DefaultFileExtension = ".mp4"; savePicker.SuggestedFileName = "New Video"; savePicker.FileTypeChoices.Add("MPEG4", new string[] { ".mp4" }); StorageFile destination = await savePicker.PickSaveFileAsync(); 創(chuàng)建媒體編碼配置文件編碼配置文件包含確定如何編碼目標(biāo)文件的設(shè)置。 轉(zhuǎn)換文件代碼時(shí),此文件中包含的選項(xiàng)最多。 MediaEncodingProfile 類提供用于創(chuàng)建預(yù)定義編碼配置文件的靜態(tài)方法: 用于創(chuàng)建純音頻編碼配置文件的方法 方法 配置文件 CreateAlac Apple 無(wú)損音頻編解碼器 (ALAC) 音頻 CreateFlac 免費(fèi)無(wú)損音頻編解碼器 (FLAC) 音頻。 CreateM4a AAC 音頻 (M4A) CreateMp3 MP3 音頻 CreateWav WAV 音頻 CreateWmv Windows Media 音頻 (WMA) 用于創(chuàng)建音頻/視頻編碼配置文件的方法 方法 配置文件 CreateAvi AVI CreateHevc 高效率視頻編碼 (HEVC) 視頻,也稱為 H.265 視頻 CreateMp4 MP4 視頻(H.264 視頻加 AAC 音頻) CreateWmv Windows Media 視頻 (WMV)以下代碼為 MP4 視頻創(chuàng)建配置文件。 MediaEncodingProfile profile = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.HD720p);靜態(tài) CreateMp4 方法創(chuàng)建 MP4 編碼配置文件。 此方法的參數(shù)為視頻指定目標(biāo)分辨率。 在這種情況下,VideoEncodingQuality.hd720p 表示在每秒 30 幀情況下的 1280 x 720 像素。 (“720p”表示每幀 720 個(gè)逐行掃描行。)用于創(chuàng)建預(yù)定義配置文件的其他方法均遵循此模式。 或者,你可以通過(guò)使用 MediaEncodingProfile.CreateFromFileAsync 方法來(lái)創(chuàng)建與現(xiàn)有媒體文件相匹配的配置文件。 或者,如果你知道所需的確切編碼設(shè)置,則可以創(chuàng)建一個(gè)新的 MediaEncodingProfile 對(duì)象,然后填寫配置文件詳細(xì)信息。 轉(zhuǎn)換文件代碼若要轉(zhuǎn)碼文件,請(qǐng)創(chuàng)建新的 MediaTranscoder 對(duì)象并調(diào)用 MediaTranscoder.PrepareFileTranscodeAsync 方法。 傳入源文件、目標(biāo)文件和編碼配置文件。 然后,調(diào)用從異步轉(zhuǎn)換代碼作中返回的 PrepareTranscodeResult 對(duì)象上的 TranscodeAsync 方法。 MediaTranscoder transcoder = new MediaTranscoder(); PrepareTranscodeResult prepareOp = await transcoder.PrepareFileTranscodeAsync(source, destination, profile); if (prepareOp.CanTranscode) { var transcodeOp = prepareOp.TranscodeAsync(); transcodeOp.Progress += new AsyncActionProgressHandler(TranscodeProgress); transcodeOp.Completed += new AsyncActionWithProgressCompletedHandler(TranscodeComplete); } else { switch (prepareOp.FailureReason) { case TranscodeFailureReason.CodecNotFound: System.Diagnostics.Debug.WriteLine("Codec not found."); break; case TranscodeFailureReason.InvalidProfile: System.Diagnostics.Debug.WriteLine("Invalid profile."); break; default: System.Diagnostics.Debug.WriteLine("Unknown failure."); break; } } 響應(yīng)代碼轉(zhuǎn)換進(jìn)度你可以注冊(cè)要在異步進(jìn)度 TranscodeAsync 發(fā)生更改時(shí)響應(yīng)的事件。 這些事件是通用 Windows 平臺(tái) (UWP) 應(yīng)用的異步編程框架的一部分,且不特定于 API 代碼轉(zhuǎn)換作。 void TranscodeProgress(IAsyncActionWithProgress asyncInfo, double percent) { // Display or handle progress info. } void TranscodeComplete(IAsyncActionWithProgress asyncInfo, AsyncStatus status) { asyncInfo.GetResults(); if (asyncInfo.Status == AsyncStatus.Completed) { // Display or handle complete info. } else if (asyncInfo.Status == AsyncStatus.Canceled) { // Display or handle cancel info. } else { // Display or handle error info. } } |
CopyRight 2018-2019 實(shí)驗(yàn)室設(shè)備網(wǎng) 版權(quán)所有 |