0

I use the following code, which works to:

  1. add watermark
  2. convert (preroll,video,postroll) to .ts
  3. create concat txt file from new .ts files
  4. create mp4 ouput

variables

$filePath = '/home/mywebsite/public_html/assets/videos/';
$convertfile = $filePath.'convert-videos-2.txt';
$watermark = $filePath.'watermark.png';
$preroll = $filePath.'preroll.mp4';
$video = $filePath.'Video.mp4';
$video_watermark = str_replace(".mp4","-watermark.mp4",$video);
$newVideo = $filePath.'Video-New.mp4';
$postroll = $filePath.'postroll.mp4';
$input1 = $filePath.'input1.ts';
$input2 = $filePath.'input2.ts';
$input3 = $filePath.'input3.ts';

Add watermark to original mp4

$mark = "ffmpeg -y -i '".$video."' -i '".$watermark."' -filter_complex \"overlay=10:10\" '".$video_watermark."'";
exec($mark);

convert to ts (video transport stream)

exec("ffmpeg -i '".$preroll."' -c copy -bsf:v h264_mp4toannexb -f mpegts '".$input1."'");
exec("ffmpeg -i '".$video_watermark."' -c copy -bsf:v h264_mp4toannexb -f mpegts '".$input2."'");
exec("ffmpeg -i '".$postroll."' -c copy -bsf:v h264_mp4toannexb -f mpegts '".$input3."'");

create concatenate txt file, with new .ts files

$cmd = "echo \"file '".$input1."'\n";
$cmd.="file '".$input2."'\n";
$cmd.="file '".$input3."'\" > ".$convertfile;
exec($cmd);

create output mp4

exec("ffmpeg -f concat -safe 0 -y -i '".$convertfile."' -c copy -bsf:a aac_adtstoasc '".$newVideo."'");

This works but takes way to long. For an mp4 that is 1GB or more, can take upto 3 hours? The watermark alone takes 1hour plus. How can i optimize this code? combine commands? How do i solve?

I've tried adding the watermark when generate the second .ts file below, but not working, returns this error Streamcopy requested for output stream 0:0, which is fed from a complex filtergraph. Filtering and streamcopy cannot be used together.:

exec("ffmpeg -y -i '".$video."' -i '".$watermark."' -filter_complex \"overlay=10:10\" -c copy -bsf:v h264_mp4toannexb -f mpegts '".$input2."'");
user892134
  • 3,078
  • 16
  • 62
  • 128

1 Answers1

1

All this for a watermark? Easy answer is to be lazy and avoid doing all of this for a watermark that nobody will care about or notice anyway.

Anyway, no need for the .ts intermediates.

  1. Add watermark:

    ffmpeg -i input.mp4 -i watermark.png -filter_complex "overlay=10:10:format=auto,format=yuv420p" -c:a copy main.mp4
    

    This is the most time consuming step because adding a watermark requires filtering and filtering requires encoding. If you want faster encoding use a faster -preset (but make sure that does not result in different attributes compared to preroll.mp4 and postroll.mp4).

  2. Make input.txt containing:

    file 'preroll.mp4'
    file 'main.mp4'
    file 'postroll.mp4'
    
  3. Concatenate with the concat demuxer:

    ffmpeg -f concat -safe 0 -i input.txt -c copy -movflags +faststart output.mp4
    

    This assumes that all inputs have the same attributes and number of streams. If not, then individually conform each non-standard input by filtering and re-encoding before concatenation, or use the concat filter instead of the concat demuxer as shown in How to concatenate videos in ffmpeg with different attributes?

llogan
  • 121,796
  • 28
  • 232
  • 243