How to escape colon sign from variable in shell?

Hello,
Below script works fine when I manually enter required information for each file. When it comes to shell in auto mode, it gives various errors.
I am under ubuntu 14.04 / trusty.

manual_run.sh:

#!/bin/bash
/usr/bin/ffmpeg -start_at_zero -copyts -i nicki.mp4 -c:v mpeg2video \
-b:v 500k -minrate 500k -maxrate 500k \
-vf "[in]drawtext=fontsize=12:fontcolor=White:fontfile='/usr/share/fonts/truetype/dejavu/DejaVuSerif.ttf':\
text='%{pts\:gmtime\:0\:%M\\\\\:%S}':x=60:y=20, \
drawtext=fontsize=12:fontcolor=Yellow:fontfile='/usr/share/fonts/truetype/dejavu/DejaVuSerif.ttf':\
text='nicki':x=360:y=20,\
drawtext=fontsize=12:fontcolor=White:fontfile='/usr/share/fonts/truetype/dejavu/DejaVuSerif.ttf':\
text='00\:05\:22.22':x=660:y=20:[out]" \
-metadata service_provider="stream" -metadata service_name="Info Kanalen" -f mpegts \
 /var/www/html/output.mp4

auto_sh:

#!/bin/bash
while read -r COL1 COL2
do
/usr/bin/ffmpeg -start_at_zero -copyts -i "$COL1" -c:v mpeg2video \
-b:v 500k -minrate 500k -maxrate 500k \
-vf "[in]drawtext=fontsize=12:fontcolor=White:fontfile='/usr/share/fonts/truetype/dejavu/DejaVuSerif.ttf':\
text='%{pts\:gmtime\:0\:%M\\\\\:%S}':x=60:y=20, \
drawtext=fontsize=12:fontcolor=Yellow:fontfile='/usr/share/fonts/truetype/dejavu/DejaVuSerif.ttf':\
text='"$COL1"':x=360:y=20,\
drawtext=fontsize=12:fontcolor=White:fontfile='/usr/share/fonts/truetype/dejavu/DejaVuSerif.ttf':\
text='"$COL2"':x=660:y=20:[out]" \
-metadata service_provider="stream" -metadata service_name="Info Kanalen" -f mpegts \
/var/www/html/$(basename "${COL1/.mp4}")_t.mp4
done < duration

duration:

restaurant.mp4 00:02:30.51
nicki.mp4 00:05:22.22

The difference between manual and auto sh files are subjected to column2 . Seems like I am unable to escape colon ( : ) sign as it was assigned to a variable.

output_error1:

[Parsed_drawtext_2 @ 0x286de80] Could not load font "02": cannot open resource
[AVFilterGraph @ 0x266a400] Error initializing filter 'drawtext' with args 'fontsize=12:fontcolor=White:fontfile=/usr/share/fonts/truetype/dejavu/DejaVuSerif.ttf:text=00:02:30.51:x=660:y=20:'
Error reinitializing filters!
Failed to inject frame into filter network: Invalid argument
Error while processing the decoded data for stream #0:0

output_error2:

icki.mp4: No such file or directory

Also, I do not understand why it gives no file found error , why the letter n for nicki is missed etc ..

I'd kindly appreciate your help

Many thanks
Boris

That missing n could indicate some non-printing control char in your input file. Sure there's none? How did you create that file?

If you need the colons in your input file escaped, you could use

  • "process substitution" for your input file, like done < <(sed 's/:/\\:/g' duration)
  • "parameter expansion" for the COL2 variable, like inserting COL2=${COL2//:/\\:}; before the ffmpeg command.
1 Like

Dear Rudic,
Thanks for your answer. You saved my time.
Lesson learnt:

  • New notation for me: done < <(sed 's/:/\\:/g' duration)
    -vf is not supported while loop

Kind regards
Boris

Many thanks
Boris

There's no particular reason -vf shouldn't work in a while loop. It's just very tricky to get all the quoting and escaping right. Shove printf "\t%s\n" in front of ffmpeg to see what its really doing - it will print each separate argument on its own line. You'll be able to tell if your quotes broke somewhere.