Adding watermark to a video by using ffmpeg in php

2020-03-07 07:15发布

问题:

I created a video with group of images and mp3. But i want to add a watermark text to that video .i am using the below code to add the text.

exec('/usr/local/bin/ffmpeg -y -i output.mov -acodec libmp3lame -vcodec msmpeg4 \
-b:a 192k -b:v 1000k -ar 44100 \
-vf "drawtext=text=string1 string2 string3 string4 string5 string6 string7 :expansion=normal:fontfile=/usr/bin/DejaVuSerif-BoldItalic-webfont.ttf: y=0:x=h-(2*lh)-n: fontcolor=white: fontsize=40: box=1: boxcolor=0x00000000@1" \
-an IMG_0696.avi');

The issue with this is that the video file is creating with zero size. Can any one help me please. Or else Suggest me any other command to add watermark text to video. Thank you in advance...

回答1:

ffmpeg -i movie.mp4 -i logo.png -filter_complex overlay output.mp4


回答2:

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

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

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

<?php

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

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

    $format = new FFMpeg\Format\Video\X264('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.]



回答3:

Command:

    ffmpeg -i input.mp4 -i watermark.png -filter_complex 'overlay' output.mp4

Try this code in your controller.

  public function setWaterMark(Request $request_body){
      try {
        $watermark = Input::file('watermark');
        $input = Input::file('input');

        ffmpeg = "C:\\ffmpeg\\bin\\ffmpeg";
        $cmd = $ffmpeg . " -i " . $input . " -i " . $watermark . " -filter_complex 'overlay' " . $output_file;
        exec($cmd, $output);

        return $output_file;
  }


回答4:

$ffmpeg = FFMpeg\FFMpeg::create();
$video = $ffmpeg->open('video.mpg');
$video
    ->filters()
    ->resize(new FFMpeg\Coordinate\Dimension(320, 240))
    ->synchronize();
$video
    ->frame(FFMpeg\Coordinate\TimeCode::fromSeconds(10))
    ->save('frame.jpg');
$video
    ->save(new FFMpeg\Format\Video\X264(), 'export-x264.mp4')
    ->save(new FFMpeg\Format\Video\WMV(), 'export-wmv.wmv')
    ->save(new FFMpeg\Format\Video\WebM(), 'export-webm.webm');

Steps for install FFmpeg on system. http://drupalasia.com/article/how-add-watermark-video-using-php-and-ffmpeg



标签: php video ffmpeg