从flv生成php中的缩略图的问题

时间:2011-03-01 12:57:42

标签: php flv

我正在尝试从flv文件生成缩略图,但它不起作用....因为,在tmp文件夹中没有创建缩略图文件:

function getVideoDuration( $vid )
{
    global $config;

    $flv = $config['FLVDO_DIR']. '/' .$vid. '.flv';
    if ( file_exists($flv) ) {
        exec($config['mplayer']. ' -vo null -ao null -frames 0 -identify "' .$flv. '"', $p);
        while ( list($k,$v) = each($p) ) {
            if ( $length = strstr($v, 'ID_LENGTH=') ) {
                break;
            }
        }

        $lx = explode('=', $length);

        return $lx['1'];
    }

    return false;
}

function regenVideoThumbs( $vid )
{
    global $config;

    $err        = NULL;
    $duration   = getVideoDuration($vid);
    if ( !$duration ) {
        $err = 'Failed to get video duration! Converted video not found!?';
    }

    $fc     = 0;
    $flv    = $config['FLVDO_DIR']. '/' .$vid. '.flv';
    if ( $err == '' ) {
        settype($duration, 'float');
        $timers = array(ceil($duration/2), ceil($duration/2), ceil($duration/3), ceil($duration/4));
        @mkdir($config['TMP_DIR']. '/thumbs/' .$vid);

        foreach ( $timers as $timer ) {
            if ( $config['thumbs_tool'] == 'ffmpeg' ) {
                $cmd = $config['ffmpeg']. ' -i ' .$flv. ' -f image2 -ss ' .$timer. ' -s ' .$config['img_max_width']. 'x' .$config['img_max_height']. ' -vframes 2 -y ' .$config['TMP_DIR']. '/thumbs/' .$vid. '/%08d.jpg';
            } else {
                $cmd = $config['mplayer']. ' ' .$flv. ' -ss ' .$timer. ' -nosound -vo jpeg:outdir=' .$config['TMP_DIR']. '/thumbs/' .$vid. ' -frames 2';
            }
            exec($cmd); echo $cmd; die();
            $tmb    = ( $fc == 0 ) ? $vid : $fc. '_' .$vid;
            $fd     = $config['TMB_DIR']. '/' .$tmb. '.jpg';
            $ff     = $config['TMP_DIR']. '/thumbs/' .$vid. '/00000002.jpg';
            if ( !file_exists($ff) )
                $ff = $config['TMP_DIR']. '/thumbs/' .$vid. '/00000001.jpg';
            if ( !file_exists($ff) )
                $ff = $config['BASE_DIR']. '/images/default.gif';

            createThumb($ff, $fd, $config['img_max_width'], $config['img_max_height']);
            ++$fc;
        }
    }

    return $err;
}

在运行此操作时,我总是将$ff设置为

$config['BASE_DIR']. '/images/default.gif'; 

出于测试目的,我在命令执行后添加了echo ...并显示

/usr/local/bin/mplayer /home/localtesttube/public_html/dev/flvideo/1251.flv -ss 10 -nosound -vo jpeg:outdir=/home/localtesttube/public_html/dev/tmp/thumbs/1251 -frames 2

1 个答案:

答案 0 :(得分:1)

尝试使用ffmpeg:

<?PHP

exec("/usr/local/bin/ffmpeg -i <PATH_TO_FLV_FILE> -ss <TIME_IN_VIDEO> -t 00:00:01 -s 320x240 -r 1 -pix_fmt rgb24 -f image2 -vcodec mjpeg -y");
if (is_file("/tmp/00000001.jpg")) {
    $gd_image = imagecreatefromjpeg("/tmp/00000001.jpg");
    unlink("/tmp/00000001.jpg");
    if ($gd_image) {
        imagejpeg($gd_image, '/path/to/screenshot/screenshot.jpg');
        imagedestroy($gd_image);
    }
}

?>

要获取文件的持续时间,请使用mplayer:

<?PHP

$duration_run = exec("/usr/local/bin/mplayer -identify <PATH TO FLV FILE> -vc null -ac null -vo null -ao null -frames 0 | /usr/bin/grep ^ID_LENGTH|/usr/bin/cut -d = -f 2", $duration);
$duration = round($duration[0]);

?>