我想用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 创建的图像。有什么办法可以做到吗?
我用下面的代码解决了这个问题。
// 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');