使用FFMPEG在视频上叠加图像

问题描述 投票:0回答:1

我想用php在视频上使用ffmpeg覆盖一张图片。

$ffmpeg = FFMpeg\FFMpeg::create();
    $video = $ffmpeg->open($videoOriginalFilePath);
    $video
        ->filters()
        ->watermark($imageFilePath, array(
            'position' => 'absolute',
            'x' => 100,
            'y' => 0,
    ));
    $video->save(new FFMpeg\Format\Video\X264(), 'output.mp4');

这样做很好,但问题是我需要这个"$imageFilePath "不是一个真实的路径,而是一个由 imagick 创建的图像。有什么办法可以做到吗?

php ffmpeg imagick
1个回答
0
投票

我用下面的代码解决了这个问题。

// create temp file
$temp = new SplTempFileObject();

// get its file path
$tempPath = tempnam(sys_get_temp_dir(), $temp);

// open the file
$file = fopen($tempPath, "w");

// write the image to the temp file
$image->setImageFormat('png');
$image->writeImageFile($file);

// overlay the image using the temp file path
$ffmpeg = FFMpeg\FFMpeg::create();
$video = $ffmpeg->open($video);
$video
  ->filters()
  ->watermark($tempPath, array(
  'position' => 'absolute',
    'x' => $xOffset,
    'y' => $yOffset,
  ));

$video->save(new FFMpeg\Format\Video\X264(), 'output.mp4');
© www.soinside.com 2019 - 2024. All rights reserved.