在我的
zsh
脚本中,我收到一个代表 Unix 纪元的字符串,例如:
myTimestamp="1719742786"
我想将该日期更改为格式化字符串,然后将其存储在变量中,以便可以在即将到来的
ffmpeg
脚本中使用。我就是这样做的:
theDate=$(date -u -r "$myTimestamp" "+%b %d, %H:%M:%S")
echo "$theDate"
将我想要的内容打印到屏幕上:
Jun 30, 06:19:48
但是当我尝试在 ffmpeg 脚本中使用此变量时,出现错误:
ffmpeg -y -i "$file" -vf \
"drawtext=text='$theDate':fontcolor=gray:fontsize=$fontSize:x=$width:y=$height:" \
"$output"
请注意,如果我将脚本中的
'$theDate'
更改为 '$myTimestamp'
之类的内容,我不会收到错误。
我得到的以及
Error initializing filters
,是这个(可能很重要?)错误:
提供文本和文本文件。请只提供一个
注意我使用的是 MacOS v14.5。
man date
提到了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.
我的完整脚本:
#!/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
正如评论中提到的: 问题在于 $theDate 字符串中有
:
(冒号),它们用作 ffmpeg 过滤器输入的字段分隔符。
要么转义冒号,要么使用不同的字符,要么将 $theDate 写入像 the_date.txt 这样的文件中,然后使用
ffmpeg -y -i "$file" -vf "drawtext=textfile=the_date.txt:... "
我将选择最后一个选项:写入文件并使用drawtext=textfile=... 如果您没有并行执行 ffmpeg 命令,则可以重复使用同一文件。 如果您并行执行 ffmpeg 命令,则必须创建唯一的文件名并在 ffmpeg 命令结束后删除它们,以免留下数千个文件。