PHP HTML5 compatible MP4 video using FFMPEG

2019-04-10 11:59发布

问题:

Hi I am using FFMPEG to convert the uploaded video with PHP.

echo "conversion exercise started...<br/><br/>";

/* looping through all files in the directory */
if ($handle = opendir('assets/uploaded_videos')) {
    while (false !== ($entry = readdir($handle))) {

        /* filtering the desired extensions */
        if ($entry != "." && $entry != ".." && in_array(substr($entry, strrpos($entry, '.')), array(".wmv", ".mpg", ".mpeg", ".flv", ".ogg", ".mp4")))
        {
            $filename = substr($entry, 0, strrpos($entry, '.'));

            //$command = "ffmpeg -i assets/uploaded_videos/$entry -vcodec libx264 assetss/videos/$filename.mp4";

            $command = "ffmpeg -i assets/uploaded_videos/$entry -vcodec mpeg4 -acodec libfaac files/videos/$filename.mp4";

            echo $command."<br />";

            shell_exec($command."> /dev/null 2>/dev/null &");
        }
    }
    closedir($handle);
}

I have embedded the player in view file like this:

<video width="350" poster="<?php echo $first_video['thumb_path'];?>" controls>
    <source src="<?php echo $first_video['video_path']; ?>" />
    <span id="silverlight_player_for_fallback"></span>
</video>

Now, when I run in IE10, the player gives me invalid source error. I am having this issue with both libx264 and mpeg4 MP4 codecs.

Any ideas whats going wrong?

Update

Following Ian's direction, I finally get it working. I have used baseline-level3 profile with libx264. You can provide extra parameters but I guess profile is the key! I experimented couple of profiles and observed that all HTML5 videos on vimeo and youtube use this baseline L3 profile.

Anyone struggling with MP4 can consider the following command for conversion:

/* following command converted all my uploaded *.wmv files to mp4 */
$command = "ffmpeg -i files/uploaded_videos/$entry -vcodec libx264 -profile:v baseline -level 3 files/videos/$filename.mp4";

回答1:

Have you tested the generated MP4 file in other browsers (that support MP4) such as Chrome and Safari? The first step is to ensure that the file actually plays.

You could also save the generated MP4 and try and drag it into Chrome/Safari and then IE10 (if the other browser works) to see if it can play. This way you can attempt to see if it's the encoding that's the issue.

There are many different flavours (known as profiles) of MP4, not all can play in browsers.