1

I'm using PHP code to retrieve the content of .m3u8 with a Signed URL from AWS CloudFront.

I can't be able to play the chunk .ts files inside it since you need also to sign their URL.

So, using PHP code, I re-write the contents of .m3u8

<?php
    $resourceKey = 'https://abcdefg.cloudfront.net/abcdef-1234.MOV_1000k*';
    $expires = time() + 172800;
    $customPolicy = <<<POLICY
{
    "Statement": [
        {
            "Resource": "{$resourceKey}",
            "Condition": {
                "DateLessThan": {"AWS:EpochTime": {$expires}}
            }
        }
    ]
}
POLICY;
    $signedUrl = $cloudFrontClient->getSignedUrl([
        'url' => $resourceKey,
        'policy' => $customPolicy,
        'private_key' => public_path().'/pk-ABCDEFGHIJ.pem',
        'key_pair_id' => 'ABCDEFGHIJ'
    ]);
    $signedUrl = str_replace('*', '.m3u8', $signedUrl);

    // get contents of the file and the query string
    $content = file_get_contents($signedUrl);
    parse_str(parse_url($signedUrl)['query'], $params);
    $qs = "?Policy=".$params['Policy']."&Signature=".$params['Signature']."&Key-Pair-Id=".$params['Key-Pair-Id'];

    // rewriting process
    $replaceThis = array(".m3u8", ".ts");
    $withThisValue = array(".m3u8".$qs, ".ts".$qs);
    $content = str_replace($replaceThis, $withThisValue, $content);
?>

I was able to sign the .ts inside the .m3u8 file. But the problem now is that, how can I convert $content to something playable to HTML5 video with VideoJS without downloading the file contents using file_put_contents? I don't want to save it on the directory. Just to convert it on blob data or decode it and play it Something like this. Is this possible?

<!DOCTYPE html>
<html>
<head>
    <title>My Site</title>
</head>
<body>

    <video id='hls-example' class="video-js vjs-default-skin" width="640" height="480" controls>
        <source src="<?php echo $content; ?>" type="application/x-mpegURL">
        Your browser does not support the video tag.
    </video>

    <script src="https://vjs.zencdn.net/7.2.3/video.js"></script>
    <script>
        var player1 = videojs('hls-example');
    </script>
</body>
</html>

PS: I tried AWS Lambda Edge. But, it doesn't work when there are many chunk .ts inside the m3u8 file. It returns an error that exceeds the Viewer Request Quota of 40KB.

Codeblooded Saiyan
  • 1,457
  • 4
  • 28
  • 54

0 Answers0