0

我正在使用 Windows Azure 媒体服务来上传视频文件、编码,然后发布它们。我使用 Windows Azure 媒体服务示例代码对文件进行编码,我发现当我使用该代码将“.mp4”文件转换为 Apple HLS 时,它在 iOS 设备中无法正常运行。只播放音频,看不到视频。然而,如果我使用 Windows Azure 媒体服务门户在 HLS 中编码和发布文件,它们在 iOS 设备上工作得非常好(音频和视频播放)!

几天来我一直在努力解决这个问题,真的很感激有人可以指导我进行编码过程(通过代码)吗?

这是我到现在为止的!

static IAsset CreateEncodingJob(IAsset asset)
    {


        // Declare a new job.
        IJob job = _context.Jobs.Create("My encoding job");
        // Get a media processor reference, and pass to it the name of the 
        // processor to use for the specific task.
        IMediaProcessor processor = GetLatestMediaProcessorByName("Windows 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 SD 4x3", 
            TaskOptions.ProtectedConfiguration);
        // 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 MP4 asset",
            true,
            AssetCreationOptions.None);

        // Launch the job. 
        job.Submit();

        // Checks job progress and prints to the console. 
        CheckJobProgress(job.Id);

        // Get an updated job reference, after waiting for the job 
        // on the thread in the CheckJobProgress method.
        job = GetJob(job.Id);

        // Get a reference to the output asset from the job.
        IAsset outputAsset = job.OutputMediaAssets[0];


        return outputAsset;


    }


 static IAsset CreateMp4ToSmoothJob(IAsset asset)
    {

        // Read the encryption configuration data into a string. 
        string configuration = File.ReadAllText(Path.GetFullPath(_configFilePath + @"\MediaPackager_MP4ToSmooth.xml"));



        //Publish the asset.
        //GetStreamingOriginLocatorformp4(asset.Id);


        // Declare a new job.
        IJob job = _context.Jobs.Create("My MP4 to Smooth job");
        // Get a media processor reference, and pass to it the name of the 
        // processor to use for the specific task.
        IMediaProcessor processor = GetLatestMediaProcessorByName("Windows Azure Media Packager");

        // Create a task with the encoding details, using a configuration file. Specify 
        // the use of protected configuration, which encrypts sensitive config data.
        ITask task = job.Tasks.AddNew("My Mp4 to Smooth Task",
            processor,
            configuration,
            TaskOptions.ProtectedConfiguration);
        // Specify the input asset to be encoded.
        task.InputAssets.Add(asset);
        // Add an output asset to contain the results of the job.
        task.OutputAssets.AddNew("Output Smooth asset",
            true,
            AssetCreationOptions.None);

        // Launch the job. 
        job.Submit();

        // Checks job progress and prints to the console. 
        CheckJobProgress(job.Id);


        job = GetJob(job.Id);
        IAsset outputAsset = job.OutputMediaAssets[0];

        // Optionally download the output to the local machine.
        //DownloadAssetToLocal(job.Id, _outputIsmFolder);

        return outputAsset;
    }

    // Shows how to encode from smooth streaming to Apple HLS format.
    static IAsset CreateSmoothToHlsJob(IAsset outputSmoothAsset)
    {
        // Read the encryption configuration data into a string. 
        string configuration = File.ReadAllText(Path.GetFullPath(_configFilePath + @"\MediaPackager_SmoothToHLS.xml"));


        //var getismfile = from p in outputSmoothAsset.Files
        //                 where p.Name.EndsWith(".ism")
        //                 select p;

        //IAssetFile manifestFile = getismfile.First();

        //manifestFile.IsPrimary = true;

        var ismAssetFiles = outputSmoothAsset.AssetFiles.ToList().Where(f => f.Name.EndsWith(".ism", StringComparison.OrdinalIgnoreCase)).ToArray();

        if (ismAssetFiles.Count() != 1)
            throw new ArgumentException("The asset should have only one, .ism file");

        ismAssetFiles.First().IsPrimary = true;
        ismAssetFiles.First().Update();




        //Use the smooth asset as input asset


        IAsset asset = outputSmoothAsset;



        // Declare a new job.
        IJob job = _context.Jobs.Create("My Smooth Streams to Apple HLS job");
        // Get a media processor reference, and pass to it the name of the 
        // processor to use for the specific task.
        IMediaProcessor processor = GetMediaProcessor("Smooth Streams to HLS Task");

        // Create a task with the encoding details, using a configuration file.
        ITask task = job.Tasks.AddNew("My Smooth to HLS Task", processor, configuration, TaskOptions.ProtectedConfiguration);

        // Specify the input asset to be encoded.
        task.InputAssets.Add(asset);

        // Add an output asset to contain the results of the job.
        task.OutputAssets.AddNew("Output HLS asset", true, AssetCreationOptions.None);

        // Launch the job. 
        job.Submit();

        // Checks job progress and prints to the console. 
        CheckJobProgress(job.Id);

        // Optionally download the output to the local machine.
        //DownloadAssetToLocal(job.Id, outputFolder);

        job = GetJob(job.Id);
        IAsset outputAsset = job.OutputMediaAssets[0];

        return outputAsset;
    }
4

1 回答 1

2

为了转换为与 iOS 兼容的 HLS,您必须使用 Smooth Streaming Source,这将是 HLS 的基础。所以你的步骤是:

  1. 将您的源转换为高质量的 H.264 (MP4)
  2. 将步骤 (1) 的结果转换为 Microsoft Smooth Streaming
  3. 将步骤 (2)(平滑流)的结果转换为 HLS

HLS 与 Microsoft Smooth Streaming 非常相似。因此,它需要具有不同比特率的源块。通过 MP4 进行 HLS 转换将无济于事。

令人遗憾的是,微软在门户网站中提供了此类explorative功能。management这会导致用户感到困惑。它在场景下的作用正是我向您建议的 - 首先获得高质量的 MP4,然后将其转换为 Microsoft Smooth Streaming,然后在 Smooth Streaming 上进行 HLS。但是用户认为 HLS 是通过 MP4 执行的,这是完全错误的。

如果我们看一下这里的在线文档,我们会看到任务预设被命名Convert Smooth Streams to Apple HTTP Live Streams。从这里我们必须弄清楚 HLS 的正确来源是 Microsoft Smooth Stream。根据我的经验,一个好的平滑流只能从一个好的 H.264 源 (MP4) 产生。如果您尝试将非 H.264 源转换为平滑流,则结果很可能是错误。

您可以使用小工具WaMediaWeb(github 上的源代码,持续交付到 Azure 网站)进行试验,现场直播:http ://wamediaweb.azurewebsites.net/ - 只需提供您的媒体帐户和密钥。查看GitHub 上的自述文件以了解一些细节,例如什么来源产生什么结果。

顺便说一句,您可以将任务堆叠在单个作业中,以避免不断寻找作业结果。该方法task.OutputAssets.AddNew(...)实际上返回一个 IAsset,您可以将其用作另一个任务的 InputAsset,并将该任务添加到同一个作业中。如果您查看示例,它会在某些时候执行此操作。它在创建 HLS 流方面也做得很好,在 iOS 上使用 iPad2 和 iPhone 4 进行了测试。

于 2012-12-03T13:31:12.250 回答