PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label video-streaming. Show all posts
Showing posts with label video-streaming. Show all posts

Thursday, September 29, 2022

[FIXED] How do you display closed caption format of HLS video stream from an m3u8 URL

 September 29, 2022     closed-captions, ffmpeg, tvos, video, video-streaming     No comments   

Issue

I'm working on a Roku and TVOS app that is going to play HLS videos (VOD and live) as well as MP4. According to the Roku docs EIA-608 is supported on both and should also work on TVOS.

My question is, given URL to the m3u8 how can I tell what specific format (EIA-608,WebVTT etc) of closed captioning is being used in each stream?

Contents of the main m3u8 (note 1st stream says no CC, but it really does have it):

#EXTM3U
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=380000,RESOLUTION=400x228,CODECS="avc1.66.30, mp4a.40.2",CLOSED-CAPTIONS=NONE
http://d.com/i/video/2426832/2426832_,350,640,1000,2000,.mp4.csmil/index_0_av.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=750000,RESOLUTION=640x360,CODECS="avc1.77.30, mp4a.40.2",CLOSED-CAPTIONS=NONE
http://d.com/i/video/2426832/2426832_,350,640,1000,2000,.mp4.csmil/index_1_av.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1044000,RESOLUTION=1280x720,CODECS="avc1.64001f, mp4a.40.2",CLOSED-CAPTIONS=NONE
http://d.com/i/video/2426832/2426832_,350,640,1000,2000,.mp4.csmil/index_2_av.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=2127000,RESOLUTION=1280x720,CODECS="avc1.64001f, mp4a.40.2",CLOSED-CAPTIONS=NONE
http://d.com/i/video/2426832/2426832_,350,640,1000,2000,.mp4.csmil/index_3_av.m3u8

Contents of the 1st stream's m3u8

#EXTM3U
#EXT-X-TARGETDURATION:4
#EXT-X-ALLOW-CACHE:YES
#EXT-X-PLAYLIST-TYPE:VOD
#EXT-X-VERSION:3
#EXT-X-MEDIA-SEQUENCE:1
#EXTINF:4.000,
http://d.com/i/video/2426832/2426832_,350,640,1000,2000,.mp4.csmil/segment1_0_av.ts
...

I can use ffprobe -hide_banner to show the 1st program's stream has closed captioning. Ex:

Duration: 00:02:36.76, start: 0.100511, bitrate: 0 kb/s
  Program 0
    Metadata:
      variant_bitrate : 380000
    Stream #0:0: Video: h264 (Constrained Baseline) ([27][0][0][0] / 0x001B), yuv420p, 400x228 [SAR 1:1 DAR 100:57], Closed Captions, 29.97 fps, 29.97 tbr, 90k tbn, 59.94 tbc
    Metadata:
      variant_bitrate : 380000
    Stream #0:1: Audio: aac (LC) ([15][0][0][0] / 0x000F), 44100 Hz, mono, fltp, 48 kb/s
    Metadata:
      variant_bitrate : 380000

However, as you can see, Program 0 > Stream 0 just says that is has Closed captions - it does not list the type/spec of closed captioning technology being used.

How do I display the format of the Closed Captions?


Solution

WebVTT is what is know as a 'side car', or 'out of band' format. Meaning captions are in a separate file that you must download. You can see the URL for this file in the m3u8. Here the caption are part of the video stream itself. The only supported format in this case is EIA-608. ffmpeg support for 608 is pretty limited. The best tool I know of to for dealing with 608 is libcaption (full disclosure, I wrote it). I recently added a ts2srt example program. Fair warning its still sorta betaish.



Answered By - szatmary
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Tuesday, May 17, 2022

[FIXED] How to create a video stream from a single dynamic image in PHP

 May 17, 2022     php, video-streaming     No comments   

Issue

I have a single image file on my server which its content changes every 100 ms.

I can load and serve this image as usual:

$image = file_get_contents('path/to/image.jpg');
header('Content-type: image/jpeg');
echo $image;
<img src="image.php">

Thus, every time the user updates the screen (pressing F5 for example) the server would reply a different image. I can also use Java Script (using setInterval for example) to update the image continuously so the users needn't to update the screen themselves.

However, I need to serve this image as a CONTINUOUS FLOW such as a LIVE VIDEO STREAM in order to be shown as an HTML5 video instead of a static image.

Some examples I`ve found so far use PHP-FFMpeg library for stream videos. It turns out that those examples require that I have a video file at hand (a file in the OS or a URL to a file) instead of a single (dynamic) image as I've described above.

I found this example for how to use PHP to streaming. It looks promisssing. But again the code supposes I have a video file url which I haven't.

I'm wondering if is it possible to adapt this code to my needs. For example, how to adapt the setHeader() method to the scenario where there are no begin and end? And considering that I have loaded the image contents using file_get_contents or so, how to change stream() properly? Or, at other hand, is there other way to serve this image as a video stream?


Solution

Well, I just found a solution for my needs:

$identifier = "an_identifier";

//set headers
header('Accept-Range: bytes');
header('Connection: close');
header('Content-Type: multipart/x-mixed-replace;boundary=' . $identifier);
header('Cache-Control: no-cache');

// loop to continuously serve an image
while(true) {

    $image = load_image_contents();

    echo "--" . $identifier . "\r\nContent-Type: image/jpeg\r\nContent-Length: ".strlen($image)."\r\n\r\n".$image;
    flush();
    usleep(50000);

}

On the browser side I just set a regular image tag:

<img src="image.php">


Answered By - Duloren
Answer Checked By - Pedro (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Saturday, March 12, 2022

[FIXED] Video streaming with php. Why is the response slow?

 March 12, 2022     html5-video, php, symfony, video-streaming     No comments   

Issue

I have a big problem. I have a video streamer site. The site has public and private videos, private videos can be seen after purchase.

Here you can see the directory structure:

    /public_html
    |__/vendor
    |__/src
    |__/public
    |__/product
       |__/sample-video
          |__/video.mp4

Well, as you can see the videos are located outside the public folder. The goal is to make them inaccessible directly.

Here is the twig:

<video id="myVideo">
   <source src="/products/12?video=10&cache={{ random(50, 10000) }}" type="video/webm" />
   <source src="/products/12?video=10&cache={{ random(50, 10000) }}" type="video/mp4" />
</video>

The video controller: https://pastebin.com/sne1mSHH

And the video stream logic: https://pastebin.com/2Sxd7Nqq

Here's the problem:

I'm going to the site. You switch between pages. Perfectly good. I go to a page with a video still good and after I start the video, the page is not good.. The page just loads and does nothing. I can't switch between pages anymore. I have to close the incognito mode and start a new session.

Until now I thought that the session lock was the problem, but I close the write session.

I tried opening the video in a new tab. Also the error is until the video stream is finished, the page does not handle the request.

So while the video is playing, while I can't send a new request. What else can I attach to make the error more transparent? Help pls!


Solution

I would personnally not use PHP to stream the video. The problem is that you'll have to many PHP processes locked for reading and streaming a big file instead of handling logic. You will also have a PHP timeout during this process.

Instead, I would use the Sendfile module:

  • You install an Apache module or other kind of Sendfile module for your web server, NGINX or whatever.

  • In PHP, you do the logic for the protection and just send a HTTP header to say you want Sendfile to handle the streaming. This way your PHP code stops running and its the web server that handles the transmission of the file.

Something like this:

<?php
if (has_access_to_the_video($file))
{
    // Send the right HTTP headers.
    header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
    header('Cache-Control: post-check=0, pre-check=0', false);
    header('Pragma: no-cache');
    header('Content-type: ' . mime_content_type($file));
    header('Content-Length: ' . filesize($file));

    // Make sure you have X-Sendfile module installed on your server.
    header('X-Sendfile: ' . $file);
    exit;
}
else
{
    // Generate your 403 error or whatever.
}

This may help you:

  • https://symfony.com/doc/current/components/http_foundation.html then search for sendfile in the page.

  • https://github.com/mostafaznv/php-x-sendfile

  • PHP File Serving using X-Sendfile

  • https://www.h3xed.com/programming/how-to-use-x-sendfile-with-php-apache

Another remark regarding your video sources

For the <video> tag, you are declaring two <source> elements, one with type="video/webm" and the other with type="video/mp4" but both point to the same URL. In your PHP logic, I don't see any handling of the desired content type so your server is probably returning the same video file for both codecs and this is problematic. I would add the video content type in the URL and on the server side, do the internal redirection with Sendfile to the correct file (MP4, OGG, WEBM, etc). If the browser requests a WEBM file and recieves a MP4 H.264 instead, I assume it will not load correctly.

It seems that MP4 H.264 is widely handled by know, so you could just stick with one source. See the current support here:

  • https://caniuse.com/mpeg4
  • https://caniuse.com/webm
  • https://caniuse.com/?search=ogg


Answered By - Patrick Janser
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Tuesday, March 1, 2022

[FIXED] Decent Video Chat API?

 March 01, 2022     api, lamp, video, video-streaming     No comments   

Issue

Just wondering. I'm looking to build a small web application with a single page. It will essentially be a video chat page so I'm looking for an API I can use or any other solutions?

This would be run on a LAMP stack.


Solution

A SO search reveals lots of similar questions which are worth checking/contributing to.

It depends if you want something free or are prepared to pay but some things that come up are (mind I havent used any of these myself):

  • http://www.tokbox.com/
  • http://code.google.com/apis/talk/open_communications.html#developer
  • http://www.process-one.net/en/blogs/article/oneteam_media_server_by_processone
  • https://sites.google.com/site/webrtc/ (Now at: https://webrtc.org/ )
  • https://www.skype.com/en/developer/
  • http://farsight.freedesktop.org/wiki/
  • https://www.twilio.com/docs/api/video
  • Some MS libraries are mentioned here: Developing a Video Chat Application with high quality video streaming
  • Apple had promised to open up FaceTime but so far nothing has happened

To get up and running quickly it seems tokbox would be most suitable.



Answered By - dgorissen
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home
View mobile version

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing