Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ffmpeg - Concat N commands

Tags:

ffmpeg

I have to run from 8 to 17 commands on a video to add some overlay texts and overlay images.

My problem is that actually I'm using a complex solution: I take the original video, I add some text, I take the output, I add more text, I take the output, I add an image, ... and so on.

The process is really slow and it takes around 20 minutes to add everything to a 2 minutes video (1-2 minutes for each iteration).

Those are some examples of my commands. Is there a way to concatenate them all to a single one?

Add one row of text between two timestamps

-i "video.mp4" -filter_complex "[0]split[base][text];[text]drawtext=fontfile=Arial.ttf:text='Some text':fontcolor=white:fontsize=32:box=1: [email protected]:boxborderw=5:x=(w-text_w)/2:y=(h-text_h)/2 - 15,format=yuva444p,fade=t=in:st=6:d=2:alpha=1,fade=t=out:st=19:d=2:alpha=1[subtitles]; [base][subtitles]overlay" output_text1.mp4

Add another row of text between the same two timestamps

-i "output_text1.mp4" -filter_complex "[0]split[base][text];[text]drawtext=fontfile=Arial.ttf:text='some more text':fontcolor=white:fontsize=32:box=1: [email protected]:boxborderw=5:x=(w-text_w)/2:y=(h-text_h)/2 + 15,format=yuva444p,fade=t=in:st=6:d=2:alpha=1,fade=t=out:st=19:d=2:alpha=1[subtitles]; [base][subtitles]overlay" output_text2.mp4

Add the first image overlay after the text with some effects

-i "output_text2.mp4" -loop 1 -t 1 -i "1.png" -filter_complex "[1:v]format=yuva422p,scale=1115x1980,setsar=1/1,pad=1.5*iw:1.5*ih:(ow-iw)/2:(oh-ih)/2:color=black@0,zoompan=z='min(zoom+0.0005,1.5)':s=223x396:d=475:x='iw/2-(iw/zoom/2)':y='ih/2-(ih/zoom/2)',fade=in:st=22:d=3.8:alpha=1,fade=out:st=37:d=3.8:alpha=1[im];[0][im]overlay=(main_w-overlay_w)/2:(main_h - overlay_h)/2:shortest=1" -pix_fmt yuv420p -c:a copy output_0.mp4

Add the second image overlay after the first with some effects

-i "output_0.mp4" -loop 1 -t 1 -i "2.jpg" -filter_complex "[1:v]format=yuva422p,scale=1490x1965,setsar=1/1,pad=1.5*iw:1.5*ih:(ow-iw)/2:(oh-ih)/2:color=black@0,zoompan=z='min(zoom+0.0005,1.5)':s=298x393:d=475:x='iw/2-(iw/zoom/2)':y='ih/2-(ih/zoom/2)',fade=in:st=41:d=3.8:alpha=1,fade=out:st=56:d=3.8:alpha=1[im];[0][im]overlay=(main_w-overlay_w)/2:(main_h - overlay_h)/2:shortest=1" -pix_fmt yuv420p -c:a copy output_1.mp4

Is there a way to concat all those commands? Or is there a guideline to respect to concat various commands?

Thanks!

like image 732
Pier Giorgio Misley Avatar asked Nov 08 '22 07:11

Pier Giorgio Misley


1 Answers

There are two possible cases

  1. You want a single output file (example 1).
  2. You want all intermediate files (example 2).

If you only need a single output file then you can specify a common output file name i.e. output_text.mp4 and separate the commands with ; this will execute all commands in same order taking last output as input.

Example 1:

-i "video.mp4" -filter_complex "[0]split[base][text];[text]drawtext=fontfile=Arial.ttf:text='Some text':fontcolor=white:fontsize=32:box=1: [email protected]:boxborderw=5:x=(w-text_w)/2:y=(h-text_h)/2 - 15,format=yuva444p,fade=t=in:st=6:d=2:alpha=1,fade=t=out:st=19:d=2:alpha=1[subtitles]; [base][subtitles]overlay" output_text.mp4; -i "output_text.mp4" -filter_complex "[0]split[base][text];[text]drawtext=fontfile=Arial.ttf:text='some more text':fontcolor=white:fontsize=32:box=1: [email protected]:boxborderw=5:x=(w-text_w)/2:y=(h-text_h)/2 + 15,format=yuva444p,fade=t=in:st=6:d=2:alpha=1,fade=t=out:st=19:d=2:alpha=1[subtitles]; [base][subtitles]overlay" output_text.mp4

If you need all intermediate files then write a shell script and declare the variable for all the output variable

Example 2:

1 = output_text1.mp4
2 = output_text2.mp4

-i "video.mp4" -filter_complex "[0]split[base][text];[text]drawtext=fontfile=Arial.ttf:text='Some text':fontcolor=white:fontsize=32:box=1: [email protected]:boxborderw=5:x=(w-text_w)/2:y=(h-text_h)/2 - 15,format=yuva444p,fade=t=in:st=6:d=2:alpha=1,fade=t=out:st=19:d=2:alpha=1[subtitles]; [base][subtitles]overlay" $1

-i $1 -filter_complex "[0]split[base][text];[text]drawtext=fontfile=Arial.ttf:text='some more text':fontcolor=white:fontsize=32:box=1: [email protected]:boxborderw=5:x=(w-text_w)/2:y=(h-text_h)/2 + 15,format=yuva444p,fade=t=in:st=6:d=2:alpha=1,fade=t=out:st=19:d=2:alpha=1[subtitles]; [base][subtitles]overlay" $2

and so on.

I think this will solve your problem.

like image 128
LAV VISHWAKARMA Avatar answered Nov 29 '22 03:11

LAV VISHWAKARMA