3

I have a PHP script for stream a video from an url, and I want to get the time to control the flow.

Browsers makes HTTP requests with a range of bytes when jumping at a time of the video.

Request Headers

Accept:*/ *
Accept-Encoding:identity;q=1, *;q=0
Accept-Language:fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4
Connection:keep-alive
Host:h.com
If-Range:Tue, 20 Oct 2015 23:38:00 GMT
Range:bytes=560855038-583155711
Referer:http://h.com/7743a76d2911cdd90354bc42be302c6946c6e5b4
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.75 Safari/537.36

Response Headers

Accept-Ranges:bytes
Cache-Control:private, max-age=14400
Connection:Keep-Alive
Content-Length:22300674
Content-Range:bytes 560855038-583155711/605162520
Content-Type:video/mp4
Date:Tue, 10 May 2016 11:23:34 GMT
Expires:Tue, 10 05 2016 15:23:34 GMT
Keep-Alive:timeout=5, max=98
Last-Modified:Tue, 20 Oct 2015 23:38:00 GMT
Server:Apache/2.4.7 (Ubuntu)
X-Powered-By:PHP/5.5.9-1ubuntu4.16

How works that time to bytes conversion ?

On my PHP server I try to get the time from the byte request :

$time_second = $start_request_byte / $video_size_byte * $video_length_second;

But it's not the solution, it isn't exact... Any ideas ?

Thanks

  • `Content-Range` is what the server sends. The browser sends `Range`. Are you building the server that must respond to browsers’ range requests? It’s not very clear. – Vasiliy Faronov May 10 '16 at 09:59
  • What is used in the browser to display the video? An HTML ` – Vasiliy Faronov May 10 '16 at 09:59
  • The filesize has no relation with the video length. I can make an 1 hour long video with a filesize of 5MB. But also a video which only lasts 1 minute but is 500MB big. – Charlotte Dunois May 10 '16 at 10:22
  • @VasiliyFaronov Yes, my bad, it's "range", not "Content-Range". And it's a simple `video` element for display the video. – Brian Nydegger May 10 '16 at 11:06
  • @BrianNydegger That’s not a valid `Range` header. Can you show what the browser actually sends? – Vasiliy Faronov May 10 '16 at 11:19
  • @VasiliyFaronov You're right. I confused the request and the response. I change my post with a complete example – Brian Nydegger May 10 '16 at 11:32
  • Possible duplicate of [How can HTML5 video's byte-range requests (pseudo-streaming) work?](http://stackoverflow.com/questions/18178374/how-can-html5-videos-byte-range-requests-pseudo-streaming-work) – szatmary May 10 '16 at 15:16
  • This question has been ansered several times on stackoverlfow. http://stackoverflow.com/questions/18178374/how-can-html5-videos-byte-range-requests-pseudo-streaming-work – szatmary May 10 '16 at 15:17
  • @szatmary Yes, but there is no solution for implementation. I'll post mine. – Brian Nydegger May 11 '16 at 08:47
  • by the way these comments just link to another question that have a generic solution, that does not answer this question. In fact the right answer it is not to point to the documentation (googling it), it's about posting a solution with code, like the one below. – loretoparisi Nov 09 '18 at 10:59

1 Answers1

5

Ok, I found a solution using ffprobe !

Command

$ ffprobe -i 430079256.mp4 -show_frames \
-show_entries frame=pkt_pos \
-read_intervals 01:23%+#1 \
-of default=noprint_wrappers=1:nokey=1 \
-hide_banner -loglevel panic

Output

Offset byte => 1:23 of the video

209782270

Explain

  • -i 430079256.mp4 Video input
  • -show_frames Display information about each frame
  • -show_entries frame=pkt_pos Display only information about byte position
  • -read_intervals 01:23%+#1 Read only 1 packet after seeking to position 01:23
  • -of default=noprint_wrappers=1:nokey=1 Don't want to print the key and the section header and footer
  • -hide_banner -loglevel panic Don't want to print banner of ffprob, and hide meta on with "panic" who only show fatal errors
  • Nice!!! So assumed I want to use this for `start` and `end` of a `Content-Range` header I have to do for both. I have the values in seconds, so first I have to convert to minutes, right to match the `read_intervals` format? Thank you. – loretoparisi Nov 09 '18 at 10:54