开发者

problem generating thumbnail in php from flv

开发者 https://www.devze.com 2023-02-13 22:47 出处:网络
I am trying to generate a thumbnail from a flv file, but it\'s not working....as in, there is no thumbnail file being created in the tmp folder:

I am trying to generate a thumbnail from a flv file, but it's not working....as in, there is no thumbnail file being created in the tmp folder:

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;
}

On running this, I always end up with $ff being set to

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

For testing purposes I added in the echo after the command is executed... and it shows

/usr/local/bin/mplayer /home/localtesttube/public_html/dev/flvideo/1251.flv -ss 10 -nosound -vo jpeg:outdir=/home/localtesttube/p开发者_开发技巧ublic_html/dev/tmp/thumbs/1251 -frames 2


Try it with 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);
    }
}

?>

To get the duration of the file use 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]);

?>
0

精彩评论

暂无评论...
验证码 换一张
取 消