php - 使用 FFMPEG 在视频上叠加图像
问题描述
我正在尝试使用 ffmpeg 和 php 在视频上叠加图像:
$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');
推荐阅读
- java - org.gradle.tooling.BuildException:无法使用 Gradle 分发版“https://services.gradle.org/distributions/gradle-3.1-bin.zip”执行构建
- php - Prestashop - 模块中的 index.php?controller=404
- android - 如何更改在 TargetSDK 21 中完美运行的 TargetSDK 23 的按钮背景?
- python - Python 3:我尝试打开 pdf 时出现消息错误
- c# - 覆盖基类的内部 get-only 属性
- azure-active-directory - Azure Active Directory 中的可配置令牌生命周期(公共预览版)
- oauth-2.0 - KeyCloak 服务器的自省端点
- vba - Microsoft Excel VBA - 运行时错误 438
- python - 汇总海量数据
- javascript - 如何在反应中使用参数