dateffmpegzsh

Zsh script using formatted date string in FFMPEG command


In my zsh script, I am receiving a string that represents a Unix epoch, like:

myTimestamp="1719742786"

I want to change that date into a formatted string, and then store it in a variable so it can be used in an upcoming ffmpeg script. I'm doing it this way:

theDate=$(date -u -r "$myTimestamp" "+%b %d, %H:%M:%S")
echo "$theDate"

which prints to my screen what I want:

Jun 30, 06:19:48

but when I try to use this variable in my ffmpeg script, I get errors:

ffmpeg -y -i "$file" -vf \
        "drawtext=text='$theDate':fontcolor=gray:fontsize=$fontSize:x=$width:y=$height:" \
        "$output"

Note that if I change out '$theDate' in the script (to something like '$myTimestamp'), I do not get errors.

What I do get, along with Error initializing filters, is this (possibly important?) error:

Both text and text file provided. Please provide only one

Note I am on MacOS v14.5. The man date mentions The date utility is expected to be compatible with IEEE Std 1003.2 (“POSIX.2”). With the exception of the -u option, all options are extensions to the standard.

My full script:

#!/bin/zsh
outDir="printed"
width=200
height=650
fontSize=95

for file in initial/*.png; do
    filename=$(basename "$file")
    prefix="${filename%.*}"
    theDate=$(date -u -r "$prefix" "+%b %d, %H:%M:%S")
    output="$outDir/$filename"

    echo "$theDate"

    ffmpeg -y -i "$file" -vf \
    "drawtext=text='$theDate':fontcolor=gray:fontsize=$fontSize:x=$width:y=$height:" \
       "$output"

done
exit

Solution

  • As mentioned in the comment: The problem is the fact that in the $theDate string there are : (colons), which are used as field separators for ffmpeg filters input.

    Either you escape the colons, or use a different character, or you write $theDate in a file like the_date.txt and after that use

    ffmpeg -y -i "$file" -vf  "drawtext=textfile=the_date.txt:... "
    

    I'll choose the last option: write to file and use drawtext=textfile=... If you are not executing ffmpeg commands in parallel, you can reuse the same file. If you are executing ffmpeg commands in parallel, you have to create unique file names and delete them after ffmpeg command ends, to not leave there thousands of files.