0

I have a quick shell script that is taking a list of input and output names line by line and providing them to ffmpeg for conversion. Some of the output paths have spaces in them, and I receive one of two errors, no matter how I escape or double quote the path with spaces.

Input File

file1.avi "output path"/file1.mp4
file1.avi "output\ path"/file1.mp4
file1.avi "output path/file1.mp4"
file1.avi output\ path/file1.mp4

convert.sh

while read -r file1 file2; do ffmpeg -i "$file1" -c copy -bsf:a aac_adtstoasc $file2; done <list

One error that I receive is:

"output path"/file1.mp4: No such file or directory

The other error that I receive is:

Unable to find a suitable output format for '"output'

The variations in the input file lines are there to show the things that I have tried in order to escape the space. I have also tried the same with adding double quotes around file2 in the shell script.

6
  • Does this answer your question? Why does my shell script choke on whitespace or other special characters?
    – jesse_b
    Commented Feb 12, 2021 at 20:30
  • I read over that a few times, and I suppose that I'm not grasping what needs to be done in order to solve my problem. I also tried the "$(file2)" variation and came up with a similar error.
    – sirEgghead
    Commented Feb 12, 2021 at 20:39
  • That is unfortunate. I have control over everything. I suppose that I will output to underscored paths and manually rename them later. I appreciate all the help guys!
    – sirEgghead
    Commented Feb 12, 2021 at 21:00
  • The backslash in output\ path should enable you to read the output file name correctly. In the ffmpeg command, you also have to use quotes around the shell variable to protect the space. You put quotes around $file1, why not $file2? Commented Feb 12, 2021 at 21:03
  • Bash's read command is documented at gnu.org/software/bash/manual/html_node/Bash-Builtins.html: "The backslash character ‘\’ may be used to remove any special meaning for the next character read and for line continuation". Commented Feb 12, 2021 at 21:06

1 Answer 1

1

Provided your filenames do not contain literal backslash characters, then if you use backslash escapes (but not quotes) in the list file

file1.avi output\ path/file1.mp4

then you may omit the -r so that read does interpret the escape sequences (as spaces) rather than as the literal character sequence \ . Then you can simply double-quote the expansions in the usual way:

while read file1 file2; do 
  ffmpeg -i "$file1" -c copy -bsf:a aac_adtstoasc "$file2"
done <list

To illustrate:

$ cat list
file1.avi output\ path/file1.mp4

then

$ while read file1 file2; do 
    printf '==%s==\n==%s==\n' "$file1" "$file2"
  done <list
==file1.avi==
==output path/file1.mp4==
3
  • I actually just figured it out. The -r was causing me trouble. It's always the little things that cause issues when lack of sleep is in play. :) Thank you.
    – sirEgghead
    Commented Feb 12, 2021 at 22:23
  • @sirEgghead yeah it's especially confusing since -r usually is the right thing when reading arbitrary strings from a file Commented Feb 12, 2021 at 22:28
  • Yeah it's my default when using read in a script. Perhaps I should leave all of this alone and get rest next time. lol. Thanks again!
    – sirEgghead
    Commented Feb 12, 2021 at 22:29

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.