Skip to content

Commit

Permalink
[EncodingAndPackagingExample] Add HLS output format support
Browse files Browse the repository at this point in the history
  • Loading branch information
xieyubo committed Nov 29, 2023
1 parent c18dceb commit 7c1c8ab
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public async Task EncodeAndPackageAsync(Uri mp4BlobUri, Uri outputStorageUri, Ca

// Prepare ffmpeg command lines.
var mpdFile = $"{Path.GetFileNameWithoutExtension(inputFile)}.mpd";
var hlsFile = $"{Path.GetFileNameWithoutExtension(inputFile)}.m3u8";
FFMpegArgumentProcessor ffmpegCommand;

if (ffprobeAnalyse.VideoStreams != null)
Expand All @@ -105,16 +106,24 @@ public async Task EncodeAndPackageAsync(Uri mp4BlobUri, Uri outputStorageUri, Ca
.WithCustomArgument("-s:v:1 1280x720")
.WithCustomArgument("-s:v:2 1920x1080")
.WithCustomArgument("-adaptation_sets \"id=0,streams=v id=1,streams=a\"")
/* HLS related settings, comments out if you don't need generate hls playlist */
.WithCustomArgument("-hls_playlist 1")
.WithCustomArgument($"-hls_master_name {hlsFile}")
/* HLS settings done */
.ForceFormat("dash"));
}
else
else
{
// For audio only stream.
ffmpegCommand = FFMpegArguments
.FromFileInput(inputFile)
.OutputToFile(mpdFile, overwrite: true, args => args
.WithAudioCodec(AudioCodec.Aac)
.WithCustomArgument("-adaptation_sets \"id=0,streams=a\"")
/* HLS related settings, comments out if you don't need generate hls playlist */
.WithCustomArgument("-hls_playlist 1")
.WithCustomArgument($"-hls_master_name {hlsFile}")
/* HLS settings done */
.ForceFormat("dash"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public async Task EncodingAndPackagingToolTest()
await process.WaitForExitAsync();
Assert.Equal(0, process.ExitCode);

// Verify dash related files.
// We should have the output mpd file.
var blob = new BlobClient(new Uri($"{outputContainerUri}/bunny.640x480.15fps.mpd"), _azureCrendentail);
using (var stream = await blob.OpenReadAsync())
Expand Down Expand Up @@ -109,6 +110,24 @@ public async Task EncodingAndPackagingToolTest()
}
}

// Verify hls related files.
// We should have the output master hls file.
blob = new BlobClient(new Uri($"{outputContainerUri}/bunny.640x480.15fps.m3u8"), _azureCrendentail);
using (var stream = await blob.OpenReadAsync())
{
Assert.True(stream.Length > 400);
}

// We should have 4 hls playlist file.
for (var i = 0; i < 4; ++i)
{
blob = new BlobClient(new Uri($"{outputContainerUri}/media_{i}.m3u8"), _azureCrendentail);
using (var stream = await blob.OpenReadAsync())
{
Assert.True(stream.Length > 1000);
}
}

// Delete the container if success.
var container = new BlobContainerClient(new Uri(outputContainerUri), _azureCrendentail);
await container.DeleteAsync();
Expand Down

0 comments on commit 7c1c8ab

Please sign in to comment.