如何在 OS X bash 的时间码中关闭冒号

How to close colons in timecodes for OS X bash

我正在尝试使用 ffmpeg 生成视频,这些视频显示源视频的烧录时间码。我在 OS X 中使用 bash 脚本。 视频 1 的起始时间码为 09:59:30:00

为了在 ffmpeg 中使用 drawtext 过滤器,我需要关闭冒号,因为它们用于分隔过滤器。

它们需要在脚本中采用这种格式 timecode='00\:00\:00\:00',它们最终将在实际终端中显示为这样 window:timecode='\''00\:00\:00\:00'\''

有没有什么方法可以使用 sed 或 awk 或类似的东西来转换存储在 $timecode 变量中的值?

我正在使用 ffprobe 生成时间码并将其添加为变量

$ timecode=($(ffprobe -v error -show_entries format_tags=timecode -of default=noprint_wrappers=1:nokey=1 "" ))
$ echo $timecode
09:59:30:00

当我以这种方式将变量 $timecode 添加到我的脚本时:

ffmpeg -i "" -c:v libx264 -crf 23 -pix_fmt yuv420p -vf drawtext="fontsize=45":"fontfile=/Library/Fonts/Arial\ Black.ttf:fontcolor=white:timecode="$timecode":rate=$framerate:boxcolor=0x000000AA:box=1:x=360-text_w/2:y=480" ""$mezzanine"/"$filenoext"_PRORES.mov"

当我使用 bash -x:

时,时间码没有关闭任何东西
+ ffmpeg -i /Users/kieranoleary/Downloads/AS11_DPP_HD_OEM_SAMPLE_136_1.mxf -c:v libx264 -crf 23 -pix_fmt yuv420p -vf 'drawtext=fontsize=45:fontfile=/Library/Fonts/Arial\ Black.ttf:fontcolor=white:timecode=09:59:30:00:rate=25/1:boxcolor=0x000000AA:box=1:x=360-text_w/2:y=480' /Users/kieranoleary/Downloads/AS11_DPP_HD_OEM_SAMPLE_136_1/mezzanine/AS11_DPP_HD_OEM_SAMPLE_136_1_PRORES.mov

我收到以下错误:

[Parsed_drawtext_0 @ 0x7f9dc242e360] Both text and text file provided. Please provide only one
[AVFilterGraph @ 0x7f9dc242e4e0] Error initializing filter 'drawtext' with args 'fontsize=45:fontfile=/Library/Fonts/Arial Black.ttf:fontcolor=white:timecode=09:59:30:00:rate=25/1:boxcolor=0x000000 A:box=1:x=360-text_w/2:y=480'
Error opening filters!

我会尝试以下方法:

$ IFS=: read -a timecode < <(ffprobe -v error -show_entries format_tags=timecode -of default=noprint_wrappers=1:nokey=1 "" )
$ printf -v timecode '%s\:%s\:%s\:%s' "${timecode[@]}"
$ echo "$timecode"
09\:59\:30\:00

当您实际调用 ffmpeg 时,您不需要那么多引号:

$ ffmpeg -i "" -c:v libx264 -crf 23 -pix_fmt yuv420p -vf \
    drawtext="fontsize=45:fontfile=/Library/Fonts/Arial Black.ttf:fontcolor=white:timecode=$timecode:rate=$framerate:boxcolor=0x000000AA:box=1:x=360-text_w/2:y=480" \
    "$mezzanine/${filenoext}_PRORES.mov"

您可以使用另一个数组作为中间变量以使命令调用更具可读性:

drawtext_options=(
    fontsize=45
    fontfile="/Library/Fonts/Arial Black.ttf"
    fontcolor=white
    timecode="$timecode"
    rate="$framerate"
    boxcolor=0x000000AA
    box=1
    x=360-text_w/2
    y=480
)

drawtext_options=$(IFS=:; echo "${drawtext_options[*]}")
ffmpeg -i "" -c:v libx264 -crf 23 -pix_fmt yuv420p -vf \
    drawtext="$drawtext_options" \
    "$mezzanine/${filenoext}_PRORES.mov"