Home > Back-end >  Add logo or watermark to Converted video using ffmpeg and PHP
Add logo or watermark to Converted video using ffmpeg and PHP

Time:03-28

We are converting videos to MP4 using FFMPEG.

We did a lot of research, however we cannot figure out how to add the company logo as a logo or a water mark to the converted video

PHP code

<?php 
$uploads_dir = 'original/';
$file_name = basename($_FILES['file']['name']);
$output_name = explode('.', $file_name)[0];
$uploaded_file = $uploads_dir . $file_name;
$convert_status = ['mp4' => 0, 'webm' => 0];

if(isset($_POST['submit'])) {
  if(move_uploaded_file($_FILES['file']['tmp_name'], $uploaded_file)) {
    // Make sure to get the correct path to ffmpeg
    // Run $ where ffmpeg to get the path
    $ffmpeg = '/bin/ffmpeg';
    
    // MP4
    $video_mp4 = $output_name . '.mp4';
    exec($ffmpeg . ' -i "' . $uploaded_file . '" -vcodec h264 -acodec libfdk_aac "./converted/' . $video_mp4 . '" -y 1>convert.txt 2>&1', $output, $convert_status['mp4']);

    // Debug
    // echo '<pre>' . print_r($output, 1) . ' </pre>';

   

    // Debug
    // echo '<pre>' . print_r($output, 1) . ' </pre>';
  }
}
?>

The logo we want to add is on: https://propeview.com/wp-content/uploads/2021/08/logo-whiteb.png

CodePudding user response:

Example your video.mp4 has resolution 1280 x 720,
and overlay logo.png on video.mp4 at position x = 0 and y = 0.

For many scenario and stable, you should learn about ffprobe for analyze inputs first

ffmpeg -i video.mp4 -i logo.png -filter_complex
   "[1:v]scale=w=1280:h=720,
    [0:v]overlay=x=0:y=0:format=yuv420[out]"
   -map 0:a -map [out] -y output.mp4

or overlay without scale

ffmpeg -i video.mp4 -i logo.png -filter_complex
   "[1:v][0:v]overlay=x=0:y=0:format=yuv420[out]"
   -map 0:a -map [out] -y output.mp4

Document: scale, overlay
Note [1:v] and [0:v]: 1 and 0 are index of inputs. v is video stream, a is audio stream

  • Related