1

I have the following bash script that I use for converting files to h.264 encoding, which falls apart whenever I pass a file with spaces in the name:

`

for File in "$@"
do
        Name=$(echo "$File" | cut -f 1 -d '.')
        ffmpeg -i $File -vcodec libx264 -acodec aac -pix_fmt yuv420p "h264_$Name.mp4"
done

`

Not a problem for any files I've written, but when I'm converting a lot of files from other sources that have included whitespace in the filenames, it could potentially become a real pain.

Is there a way I can modify this script to properly handle this condition? Or is there a way I can pass the parameters to the script?

Thanks!!! Kirk

I'm calling my script with commands line like:

  • ToH264MP4 "My Video Name With Spaces.mov"
  • ToH264MP4 *

It's failing because it's treating each filename portion separated by spaces as a unique input parameter, rather than passing the full filename.

  • 4
    quote `"$File"` – jordanm Oct 30 '22 at 06:21
  • 1
    Put a valid shebang and paste your script at https://shellcheck.net for validation/recommendation. – Jetchisel Oct 30 '22 at 06:30
  • Meaning quote the filename with spaces on-the command-line. Otherwise word-splitting occurs before your script gets the arguments. E.g. `bash script.sh "file one.mov" "file two.mov"` etc... – David C. Rankin Oct 30 '22 at 06:30
  • @Kirk Sealls Please try the follwoing script as a workaround. I put it in the link since this question was closed. The script will be available for 30 days from now https://easyupload.io/bznkpk – Vab Oct 30 '22 at 12:17

0 Answers0