且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在php中使用ffmpeg为视频添加水印

更新时间:2022-03-18 02:08:25

我正在使用 php-ffmpeg [Below : Composer Json]:

I am using php-ffmpeg [Below : Composer Json]:

{
    "require": {
        "php-ffmpeg/php-ffmpeg": "^0.6.1"
    }
}

并且使用这个 php-ffmpeg 库,您可以使用以下代码添加水印:

and Using this php-ffmpeg library you can add watermark's using the following codes:

<?php

function processVideo($videoSource,$reqExtension, $watermark = "")
{
    $ffmpeg = FFMpegFFMpeg::create();

    $video = $ffmpeg->open($videoSource);

    $format = new FFMpegFormatVideoX264('libmp3lame', 'libx264');

    if (!empty($watermark))
    {
        $video  ->filters()
                ->watermark($watermark, array(
                    'position' => 'relative',
                    'top' => 25,
                    'right' => 50,
                ));
    }

    $format
    -> setKiloBitrate(1000)
    -> setAudioChannels(2)
    -> setAudioKiloBitrate(256);

    $randomFileName = rand().".$reqExtension";
    $saveLocation = getcwd(). '/video/'.$randomFileName;
    $video->save($format, $saveLocation);

    if (file_exists($saveLocation))
        return "http://localhost/test/video/$randomFileName";
    else
        return "http://localhost/test/thumb/404.png";

}

echo $videoLocation =  processVideo("sample.mp4","mp4","favicon.png");

?>

[请根据需要更新位置.]

[Please, update the location according to your need.]