zoukankan      html  css  js  c++  java
  • 微软Azure的多媒体编码服务示例

     

    这篇文章是介绍 Azure 媒体服务编程系列之一。以前的主题是如何: 获得媒体处理器.

    为服务器上的媒体内容,可以对内容与大量的媒体编码和格式使用 Azure Media 编码器进行编码。您还可以使用编码器提供的媒体服务的合作伙伴 ;第三方编码器可以使用通过Azure 市场。您可以指定编码任务,通过使用编码器预设的字符串,或通过使用配置文件的详细的信息。

    MP4 自适应比特率集

    编码,建议你到夹层文件到 MP4 自适应比特率设置,然后使用动态的包装,以提供您的内容。更多的信息,请参阅创建与媒体服务 SDK.net 编码作业动态打包交付内容.

    MP4 编码

    下面的方法将上载单个资产并创建作业,以编码到 MP4 使用资产"H264 宽带 720 p"预设,将创建一个单一的 MP4 使用 H264 编码在 720p 分辨率:

        static IJob CreateEncodingJob(string inputMediaFilePath, string outputFolder)
        {
            //Create an encrypted asset and upload to storage.
            IAsset asset = CreateAssetAndUploadSingleFile(AssetCreationOptions.StorageEncrypted, 
                inputMediaFilePath);
    
            // Declare a new job.
    
            IJob job = _context.Jobs.Create("My encoding job");
        
            // Get a reference to the Azure Media Encoder
            IMediaProcessor processor = GetLatestMediaProcessorByName("Azure Media Encoder");
        
            // Create a task with the encoding details, using a string preset.
            ITask task = job.Tasks.AddNew("My encoding task",
                processor,
                "H264 Broadband 720p",
                _protectedConfig);
        
            // Specify the input asset to be encoded.
            task.InputAssets.Add(asset);
        
            // Add an output asset to contain the results of the job. 
            // This output is specified as AssetCreationOptions.None, which 
            // means the output asset is in the clear (unencrypted). 
            task.OutputAssets.AddNew("Output asset", AssetCreationOptions.None);
        
            // Use the following event handler to check job progress.  
            job.StateChanged += new EventHandler<JobStateChangedEventArgs>(StateChanged);
        
            // Launch the job.
            job.Submit();
        
            // Optionally log job details. This displays basic job details
            // to the console and saves them to a JobDetails-JobId.txt file 
            // in your output folder.
            LogJobDetails(job.Id);
        
            // Check job execution and wait for job to finish. 
            Task progressJobTask = job.GetExecutionProgressTask(CancellationToken.None);
            progressJobTask.Wait();
        
            // If job state is Error, the event handling 
            // method for job progress should log errors.  Here we check 
            // for error state and exit if needed.
            if (job.State == JobState.Error)
            {
                Console.WriteLine("
    Exiting method due to job error.");
                return job;
            }
        
            // Perform other tasks. For example, access the assets that are the output of a job, 
            // either by creating URLs to the asset on the server, or by downloading. 
            return job;
        }
    
        private static void StateChanged(object sender, JobStateChangedEventArgs e)
        {
            Console.WriteLine("Job state changed event:");
            Console.WriteLine("  Previous state: " + e.PreviousState);
            Console.WriteLine("  Current state: " + e.CurrentState);
            switch (e.CurrentState)
            {
                case JobState.Finished:
                Console.WriteLine();
                Console.WriteLine("Job is finished. Please wait while local tasks or downloads complete...");
                break;
                case JobState.Canceling:
                case JobState.Queued:
                case JobState.Scheduled:
                case JobState.Processing:
                    Console.WriteLine("Please wait...
    ");
                    break;
                case JobState.Canceled:
                case JobState.Error:
    
                    // Cast sender as a job.
                    IJob job = (IJob)sender;
    
                    // Display or log error details as needed.
                    LogJobStop(job.Id);
                    break;
                default:
                    break;
            }
        }

    流媒体编码

    如果你想要对某个光媒体的视频编码有两个选项:

    • 直接编码为流媒体
    • 编码为 MP4,然后转换为流媒体

    直接编码为流媒体使用以上所示的代码,但使用流媒体编码器预设之一。编码器预设的完整列表,请参见任务预设字符串 Azure 媒体编码器.

    若要将 MP4 转换为流媒体,请使用 Azure 媒体包。Azure 媒体包装不支持字符串预设,因此你必须在 XML 中指定的配置选项。可以在任务预设 Azure 媒体包装找到 MP4 转换为流媒体所需的 XML。复制并粘贴到您的项目中名为 MediaPackager_MP4ToSmooth.xml 的文件的 XML。下面的代码演示如何将 MP4 资产转换为流媒体。下面是一个简单示例:

    private static IJob ConvertMP4toSmooth(IAsset assetToConvert, string configFilePath)
     {
        // Declare a new job to contain the tasks
        IJob job = _context.Jobs.Create("Convert to Smooth Streaming job");
        // Set up the first Task to convert from MP4 to Smooth Streaming. 
        // Read in task configuration XML
        string configMp4ToSmooth = File.ReadAllText(Path.GetFullPath(configFilePath + @"MediaPackager_MP4ToSmooth.xml"));
        // Get a media packager reference
        IMediaProcessor processor = GetLatestMediaProcessorByName("Azure Media Packager");
        // Create a task with the conversion details, using the configuration data
        ITask task = job.Tasks.AddNew("My Mp4 to Smooth Task",
               processor,
               configMp4ToSmooth,
               TaskOptions.None);
        // Specify the input asset to be converted.
        task.InputAssets.Add(assetToConvert);
        // Add an output asset to contain the results of the job.
        task.OutputAssets.AddNew("Streaming output asset", AssetCreationOptions.None);
        // Use the following event handler to check job progress. 
        // The StateChange method is the same as the one in the previous sample
        job.StateChanged += new EventHandler<JobStateChangedEventArgs>(StateChanged);
        // Launch the job.
        job.Submit();
        // Check job execution and wait for job to finish. 
        Task progressJobTask = job.GetExecutionProgressTask(CancellationToken.None);
        progressJobTask.Wait();
        // Get a refreshed job reference after waiting on a thread.
        job = GetJob(job.Id);
        // Check for errors
        if (job.State == JobState.Error)
        {
            Console.WriteLine("
    Exiting method due to job error.");
        }
        return job;
    }
  • 相关阅读:
    设计模式六大原则
    .net Stream篇(七)
    .net Stream篇(六)
    .net Stream篇(五)
    .net Stream篇(四)
    Leetcode 18
    Leetcode 16
    Leetcode 15
    Leetcode 12
    Leetcode 9
  • 原文地址:https://www.cnblogs.com/sennly/p/4178409.html
Copyright © 2011-2022 走看看