I use the following code, which works to:
- add watermark
- convert (preroll,video,postroll) to .ts
- create concat txt file from new .ts files
- 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."'");