-1

Here is my code:

#!/bin/bash

# https://askubuntu.com/a/343753
# http://wiki.bash-hackers.org/commands/builtin/read
# (use null character to separate a list of filenames)
# (sets IFS in the environment for just the read command)
log=./log.txt
OLDIFS=$IFS
find $1 -type f -name '*.MTS' -print0 |
while IFS= read -r -d '' input; do
    # set IFS to null string to preserve whitespace when $input is used
    IFS=''
    echo ""
    echo "input='${input}'"
    echo "`sha1sum "${input}"`"
    output="${input%.MTS}.mp4" # change extension to MTS
    ffmpeg -i "$input" -n -c:v copy -c:a aac -strict experimental -b:a 128k "$output" >> "$log" 2>&1
    if [ $? != 0 ]; then
        echo "FAILED: "$input" -> "$output""
    else
        touch -r "$input" "$output"  # copy original file's metadata
        echo "SUCCESS: "$input" -> "$output"" # indicate success
    fi

done
IFS=$OLFDIFS
echo "------"

Notice how the output changes based off whether I comment out the ffmpeg line or not: enter image description here

My question is, why does the second filename read in get messed up when the ffmpeg command is used?

Daniel E
  • 394
  • 3
  • 13

2 Answers2

0

A double quote cannot quote a double quote. Use a backslash like so: \". Examples:

echo "foo "bar     baz" bang"

Output, no quotes, no spaces:

foo bar baz bang

Now using backslashes:

echo "foo \"bar     baz\" bang"

Output, shows spaces.

foo "bar     baz" bang

The actual code has two lines like this:

echo "FAILED: "$input" -> "$output""

So change that to:

echo "FAILED: \"$input\" -> \"$output\""
agc
  • 7,973
  • 2
  • 29
  • 50
0

The answer as pointed out by Gordon is that I have to put a < /dev/null

at the very end of my ffmpeg command for the reason explained here

I did have some issues with double quoted strings but that wasn't the cause of this problem. Thanks everyone!

Daniel E
  • 394
  • 3
  • 13