MP4 plays when accessed directly, but not when read through PHP, on iOS

前端 未结 6 1809
花落未央
花落未央 2020-12-03 00:01

I use a PHP script to validate video requests before serving them. This script works as expected on the desktop, with Safari and Chrome. But on iOS, I get a broken play bu

6条回答
  •  臣服心动
    2020-12-03 00:12

    As was stated above to stream or playback MP4 videos using PHP, you will need to handle byte ranges if you want proper playback on Safari and iOS.

    rangeDownload() function mentioned in the previous answers does the job pretty well.

    I want to mention another piece of this puzzle - make sure the source in the video ends with .mp4 as in . This makes browser thing that it is a video file, and it starts treating it as one.

    Inside yourfile.php, you could grab the incoming reference for your file using $_SERVER['PATH_INFO'] or within REQUEST_URI. No need to pass it as a ?id=someId.mp4, direct slash approach looks more like a real file.

    To sum up, from my experience to serve a video file from PHP correctly you will need:

    • Byte range support. Browser tells server which part of the file it needs, and the server needs to respond with that byte range content.
    • Have your moov atom at the beginning of the file (you could use ffmpeg's -movflags +faststart or MP4Box)
    • Source attribute of video tag needs to look like an .mp4 file. Without this my videos were only playing in Chrome and not in Safari/iOS.
    • Direct HTML5 player, or you can use a library like videojs

    I wrote this based on my experience with serving thousands of videos on my music video website. While this might not be the case for all, but I found this cross-browser and cross-device setup work as expected.

提交回复
热议问题