ffmpeg没有返回持续时间,无法播放视频直到完成。通过PHP流图像2视频

时间:2014-02-17 22:12:23

标签: javascript php ffmpeg html5-video

我真的在与ffmpeg斗争。我正在尝试将图像转换为视频,我有一台我正在录制的ip摄像机。每张图像的记录是mjpegs 1帧。

我正在尝试在php中创建一个脚本,以便我可以从日期到日期重新创建视频,这需要通过image2pipe输入图像然后创建视频。

麻烦的是,ffmpeg确实会返回持续时间并启动统计数据,因此我无法在视频完成时完成任务或完成百分比。视频在完成之前不会播放,而且它不是一个非常好的UE。

关于如何解决这个问题的任何想法,视频格式可以是我对建议持开放态度的任何内容。

PHP:

//Shell command
exec('cat /image/dir/*.jpg | ffmpeg -y -c:v mjpeg -f image2pipe -r 10 -i - -c:v libx264 -pix_fmt yuv420p -movflags +faststart myvids/vidname.mp4 1>vidname.txt 2>&1')

//This is loaded via javascript when the video is loaded (which is failing due to stats being wrong
$video_play = "<video width=\"320\" height=\"240\" src=\"myvids/vidname.mp4\" type=\"video/mp4\"\
id=\"player1\" controls=\"controls\" preload=\"none\"></video>";

使用Javascript:

//Javascript to create the loop until video is loaded
<script>
           $(document).ready(function() {
                var loader = $("#clip_load").percentageLoader();
                $.ajaxSetup({ cache: false }); // This part addresses an IE bug. without it, IE will only load the first number and will never refresh
                var interval = setInterval(updateProgress,1000);
                function updateProgress(){ $.get( "'.base_url().'video/getVideoCompile_Process?l='.$vid_name.'-output.txt&t=per", function( data ) { if(data=>\'100\'){ $("#clip_load").html(\''.$video_play.'\'); clearInterval(interval); }else{loader.setProgress(data); } });                    }
            });
            </script>

PHP(页面通过javascript调用:

//This is the script which returns the current percentage
$logloc = $this->input->get('l');
$content = @file_get_contents($logloc);

if($content){
    //get duration of source
    preg_match("/Duration: (.*?), start:/", $content, $matches);

    $rawDuration = $matches[1];

    //rawDuration is in 00:00:00.00 format. This converts it to seconds.
    $ar = array_reverse(explode(":", $rawDuration));
    $duration = floatval($ar[0]);
    if (!empty($ar[1])) $duration += intval($ar[1]) * 60;
    if (!empty($ar[2])) $duration += intval($ar[2]) * 60 * 60;

    //get the time in the file that is already encoded
    preg_match_all("/time=(.*?) bitrate/", $content, $matches);

    $rawTime = array_pop($matches);

    //this is needed if there is more than one match
    if (is_array($rawTime)){$rawTime = array_pop($rawTime);}

    //rawTime is in 00:00:00.00 format. This converts it to seconds.
    $ar = array_reverse(explode(":", $rawTime));
    $time = floatval($ar[0]);
    if (!empty($ar[1])) $time += intval($ar[1]) * 60;
    if (!empty($ar[2])) $time += intval($ar[2]) * 60 * 60;

    //calculate the progress
    $progress = round(($time/$duration) * 100);
    if ($this->input->get('t')=='per'){
        echo $progress;
    }else{
            echo "Duration: " . $duration . "<br>";
            echo "Current Time: " . $time . "<br>";
    echo "Progress: " . $progress . "%";}
}else{ echo "cannot locate";}

由于

0 个答案:

没有答案