0

I am trying to do the below tasks.

  1. Convert video from webm to mp4 format
  2. Resize the video
  3. Add watermark to the bottom right
  4. Generate the thumbnail

I am able to do it by running multiple shell commands. Is there a way to do all four tasks in one go. For example

$watermark = "https://dummyimage.com/150x50/bf1fbf/ffffff.png";
shell_exec("ffmpeg -i test.webm -vf scale=100:-1, {$watermark} -filter_complex overlay=x=(main_w-overlay_w):y=(main_h-overlay_h) test.mp4 -ss 00:00:01.000 -vframes 1 test.jpg");

1 Answer 1

2

Use a single instance of -filter_complex to do all filtering:

$watermark = "https://dummyimage.com/150x50/bf1fbf/ffffff.png";
shell_exec("ffmpeg -i test.webm -i '{$watermark}' -filter_complex '[0:v]scale=100:-2[bg];[1]rotate=-3*PI/180[fg];[bg][fg]overlay=x=(main_w-overlay_w):y=(main_h-overlay_h):format=auto,format=yuv420p,split=outputs=2[vid][img]' -map '[vid]' -map 0:a test.mp4 -map '[img]' -ss 00:00:01.000 -frames:v 1 test.jpg");
  • See FFmpeg Filtering Documentation.

  • Your ffmpeg must have support for the HTTPS protocol to use HTTPS inputs. See ffmpeg -protocols to check. Otherwise, download the PNG first before running ffmpeg.

Explanation of the filtergraph

  • [0:v]scale=100:-2[bg] - Take video from input #0 ([0:v]), scale to 100 pixels wide, auto-scale height to preserve aspect, but make sure it is divisible by 2 (a requirement of libx264). Label the scale output [bg] (you can give it any name).

  • [1]rotate=-3*PI/180[fg] Rotate input #1 ([1]) by -3 degrees. Name this [fg].

  • [bg][fg]overlay=x=(main_w-overlay_w):y=(main_h-overlay_h):format=auto - Overlay [bg] under [fg]. Place overlay in bottom right. format option allows it to auto select an appropriate pixel format for best quality.

  • format=yuv420p Use format filter to make pixel format yuv420p. This is for chroma-subsampling compatibility as non-FFmpeg based players can only support YUV 4:2:0 H.264.

  • split=outputs=2[vid][img] Split the output from the filtering into two identical streams named [vid] and [img]. This is required when making two outputs using the same filtered output because you can't re-use the same filter output more than once.

Sign up to request clarification or add additional context in comments.

16 Comments

Thank you so much for your reply. You mentioned a very good point regarding protocol. Yes, my ffmpeg supports https. Can you please explain the command a little bit. I noticed you used format as well.
Btw I just executed the code and it does not seem to work.
@AwaisZahid You need to first determine if the issue is the PHP code, or with the ffmpeg command, or both. It is always recommended to run the command in your terminal first (unscripted) to make sure it works before adding it to your code. Also, "does not seem to work" is never as helpful and informative as providing the errors.
First I apologize secondly I greatly appreciate the detailed explanation. Basically this is the only line of code I have on my PHP file and it runs silently. What I can assure is ffmpeg is operational on the server because when I run a single filter it works. I am sure there is no PHP error otherwise it will generate an error_log file. I am not sure if I can enable logs for ffmpeg. I am accessing it on a VPS through the web browser. Makes sense?
I wrapped two parts of the code in single quotes and it works. shell_exec("ffmpeg -i test.webm -i '{$watermark}' -filter_complex '[0:v]scale=360:-2[bg];[bg][1]overlay=x=(main_w-overlay_w):y=(main_h-overlay_h),format=yuv420p,split=outputs=2[vid][img]' -map [vid] test.mp4 -map [img] -ss 00:00:01.000 -vframes 1 test.jpg"); Though I do not understand why it was breaking without '
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.