0

I have a video of about 20 minutes length and want to show every second a different text using the drawtext filter. I used a java software to compute a very long ffmpeg command (more than 100,000 characters long). Pasting it into the PowerShell took a long time and then I got the error

Program 'ffmpeg.exe' failed to run: The filename or extension is too longAt line:1 char:1

So the command is obviously too long. Can I somehow outsource it into an external file? I'm not looking for the subtitles filter instead of the drawtext filter because I'm using special functionalities of drawtext.

principal-ideal-domain
  • 3,998
  • 8
  • 36
  • 73
  • [Apparently](https://stackoverflow.com/questions/3205027/maximum-length-of-command-line-string) the maximum length of a command line in Windows is roughly 8k, so you'll need to find some alternate way to do this. I don't think `ffmpeg` has a way to specify arbitrary command line options via a file (a common way to work around these kinds of constraints), but it would probably not be *too* hard to patch that in manually and use that special-built `ffmpeg` executable instead. – Joachim Sauer Apr 21 '22 at 13:00

2 Answers2

0

Create temporary utf-8 text files containing the text (one file per drawtext filter instance) and specify them when calling drawtext filter with its textfile option instead of the text option. See documentation.

Alternate Approach

Break down the video into shorter segments so each ffmpeg command is within 8191-character limit. Then, use the -f concat demuxer with the -c copy muxer option to combine the output video segments.

Unfortunately, either approach requires creation of a tempdir to manage intermediate files but the latter will produce less number of them.

kesh
  • 4,515
  • 2
  • 12
  • 20
  • Can you make a little example? So I need 1200 different files? – principal-ideal-domain Apr 21 '22 at 15:37
  • Yes (to 1200 files). Create a temp dir, create these files, and recursively kill the temp dir. Pretty easy to do in Python, but I don't know PowerShell to give you an example. – kesh Apr 21 '22 at 15:52
  • Creating the files is no problem (I can do that in Java). But I'd like to see how the ffmpeg command looks then. – principal-ideal-domain Apr 21 '22 at 16:16
  • Interestingly, I don't have the 8191-character limit. I tried to generate bigger and bigger commands and my biggest working command has 28878 characters. – principal-ideal-domain Apr 21 '22 at 16:18
  • I assume it's as simple as `ffmpeg ... -vf drawtext=textfile=tempdir/0.txt ...` if tempdir is within the working directory and `0.txt` being the file for the first second. With windows backslash dirsep, you may need to put the path in single quotes. (For the record, I knew of this option but never tested it myself...) – kesh Apr 21 '22 at 16:31
  • And `...` are the options and are not part of the text file? Then I don't save anything. The string is that long because of all the options. I'm using currently `drawtext=fontfile=C\\:/Windows/Fonts/arial.ttf:text='45.4 km/h':fontcolor=white:fontsize=200:box=1:boxcolor=black@0:boxborderw=50:x=40:y=h-th-25:shadowx=4:shadowy=4:enable='between(n,4125,4149)'`. So you see that the actual text is not the problem. – principal-ideal-domain Apr 21 '22 at 16:48
  • oh i see. then I'd try the alternate approach that I added to my post. Divide video into segments and copy-concatenate them – kesh Apr 21 '22 at 16:51
  • Yeah, but depending on the number of texts this might give very many segments. Maybe I want to place more control textes in the video. So in the end I might have lots of very short videos I have to concatenate. – principal-ideal-domain Apr 21 '22 at 16:56
  • Do you think there is a way using subtitles filter? So can I style the subtitles like when using the drawtext filter? – principal-ideal-domain Apr 21 '22 at 16:57
  • "lots of very short videos I have to concatenate." This shouldn't be too much issue as you can build `ffconcat` text file as you encode segments (at least to me). The concatenation will go pretty fast (should be bounded by your NVMe/SSD/HDD speed) – kesh Apr 21 '22 at 17:22
  • "subtitles" Look into SSA/ASS format. That format among the ones I've checked out in the past grants the most amount of format controls to user. – kesh Apr 21 '22 at 17:23
  • You're right. Concatenation doesn't take too much time. Nevertheless, I'm still looking for a better solution. My next idea: Is it possible to set the default font for ffmpeg? Then I could leave out a lot of characters for each text to draw. – principal-ideal-domain Apr 21 '22 at 21:28
0

I'm posting this (yet another) alternate approach as a separate answer because it is substantially different from the previous takes.

You can shorten the command line by prepending a sendcmd filter in front of each drawtext filter. The drawtext documentation shows an example of using sendcmd to alter the text:

sendcmd=c='56.0 drawtext reinit fontsize=56\:fontcolor=green\:text=Hello\\ World'

If you also check the sendcmd documentation, it lists the filename option:

filename, f

Set the filename of the commands to be read and sent to the other filters.

with an example:

Specify a list of drawtext and hue commands in a file.

# show text in the interval 5-10
5.0-10.0 [enter] drawtext reinit 'fontfile=FreeSerif.ttf:text=hello world',
        [leave] drawtext reinit 'fontfile=FreeSerif.ttf:text=';

A filtergraph allowing to read and process the above command list stored in a file test.cmd, can be specified with:

sendcmd=f=test.cmd,drawtext=fontfile=FreeSerif.ttf:text='',hue

So, instead of using the enable=between(start,end) option, you can use a single drawtext filter in the graph and compose a command text file to instruct it what to do.

kesh
  • 4,515
  • 2
  • 12
  • 20