Ammar Abdul Aziz
Ammar Abdul Aziz

Reputation: 116

Stream/Encode videos using Azure Media Services and Node Js

I have an application that uses AWS Elastic Transcoder to encode videos uploaded to S3 buckets into HLS streaming formats using Lambda functions :

var AWS = require('aws-sdk');
var eltr = new AWS.ElasticTranscoder({ apiVersion: '2012–09–25', region: 'ap-south-1' });

exports.handler = function (event, context) {
    var key = event.Records[0].s3.object.key;
    let srcKey = decodeURIComponent(key.replace(/\+/g, " ")); //the object may have spaces  
    let newKey = key.split('.')[0].replace('protected-video-input/', '')

    eltr.createJob({
        PipelineId: pipelineId,
        OutputKeyPrefix: 'protected-video-output/' + newKey + '/',
        Input: {
            Key: srcKey,
            FrameRate: 'auto',
            Resolution: 'auto',
            AspectRatio: 'auto',
            Interlaced: 'auto',
            Container: 'auto'
        },
        Outputs: [
            {
                Key: newKey + '_64k_audio',
                PresetId: '1351620000001-200071',
                SegmentDuration: "10"
            },
            {
                Key: newKey + '_360',
                PresetId: '1593703351160-e26c00',
                SegmentDuration: "10"
            },
            {
                Key: newKey + '_480',
                PresetId: '1593703253941-idqn5g',
                SegmentDuration: "10"
            },
            {
                Key: newKey + '_720',
                PresetId: '1593702727032-5klbqi',
                SegmentDuration: "10"
            },
            {
                Key: newKey + '_1080',
                PresetId: '1593702631383-73kckt',
                SegmentDuration: "10"
            },
            {
                Key: newKey + '.mp4',
                PresetId: '1351620000001-000020'
            },
        ],
        Playlists: [
            {
                Format: 'HLSv3',
                Name: newKey,
                OutputKeys: [
                    newKey + '_64k_audio',
                    newKey + '_360',
                    newKey + '_480',
                    newKey + '_720',
                    newKey + '_1080'
                ]
            }
        ]
    });
};

I replaced the S3 bucket with Azure Blob Storage in order to upload my video files.

Please help with ANY of the following :

  1. Any examples of which method/function of Azure Media Services should I use for converting to HLS format in node js?
  2. Should the videos be stored in Blob Storage or Media Services Assets?

Upvotes: 0

Views: 242

Answers (1)

johndeu
johndeu

Reputation: 2512

I have loads of encoding samples written in Typescript and Node.js for Azure Media services up here: https://github.com/Azure-Samples/media-services-v3-node-tutorials/tree/main/VideoEncoding

I would recommend just using the Content Aware Encoding preset. You can move most of the code from these samples into an Azure Function easily.
There is an article showing how to do this here: https://learn.microsoft.com/en-us/azure/media-services/latest/integrate-azure-functions-dotnet-how-to

To answer your second question - yes, the videos will be uploaded directly into blob containers. That is essentially what an AMS "Asset" is, but you first create the Asset through the AMS SDK, and then ask for the container URL to upload content to. That is easy to figure out in the sample above. Let me know if that helps.

To stream the content as HLS, you have to enable a Streaming Endpoint on your AMS account. Once that is done, you can then create a Streaming Locator on the Asset and use the HLS version of the URL.
That is also demonstrated in the streaming samples. Take a look at the basic upload, encode and stream sample to get a good feel for how that works. https://github.com/Azure-Samples/media-services-v3-node-tutorials/blob/main/Streaming/StreamFilesSample/index.ts

Upvotes: 1

Related Questions