1

I'm trying to generate videos using ffmpeg that display a burnt in timecode of the source video. I'm using a bash script in OS X. video 1 has a starting timecode of 09:59:30:00

In order to use the drawtext filter in ffmpeg, I need to close the colons, as they are used to delimit the filter.

They need to be in this format in the script timecode='00\:00\:00\:00' and they will ultimately show up like this in the actual terminal window: timecode='\''00\:00\:00\:00'\''

Is there some way to transform the values stored in the $timecode variable, using sed or awk, or something similar?

I'm using ffprobe to generate the timecode and adding it as a variable

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

When I add the variable $timecode in this fashion to my script:

ffmpeg -i "$1" -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"

the timecode shows up without anything closed when I use 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

and I get the following error:

[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!
chepner
  • 497,756
  • 71
  • 530
  • 681
Tandy Freeman
  • 528
  • 5
  • 15

1 Answers1

3

I would try the following:

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

When you actually call ffmpeg, you don't need quite so many quotes:

$ ffmpeg -i "$1" -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"

You might use another array as an intermediate variable to make the command invocation more readable:

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 "$1" -c:v libx264 -crf 23 -pix_fmt yuv420p -vf \
    drawtext="$drawtext_options" \
    "$mezzanine/${filenoext}_PRORES.mov"
chepner
  • 497,756
  • 71
  • 530
  • 681
  • Thanks for such a detailed answer, your array is so much more readable and elegant. However, I still have the same issue. Your script results in the same error, with `timecode=09\:59\:30\:00` showing up in the actual terminal window. I wa able to get timecode printing once I manually renamed the terminal output from the script to `timecode='\''09\:59\:30\:00'\''` . – Tandy Freeman Sep 01 '15 at 20:56
  • I just needed to wrap your timecode line in double quotes and all is well. Thank you so much! – Tandy Freeman Sep 01 '15 at 21:32