'ffmpeg conversion executable file output ".MOD: no files in such directory"

I installed ffmpeg using Homebrew in macOS 12.2. I have a folder of MOD files from an old camera that I need to convert to MOV or MP4 to edit them, I have a .bat file in my Windows machine that I can run on a folder that has MOD files to batch convert them to MP4 or MOV that I can easily tweak but I'm kinda clueless with how to do this on my new (and first) Mac, I came across this script that I managed to use once through the terminal but tried putting it in a executable file so I can easily run it and copy it in every folder that I need to for new projects. When I created the exec file I'm not sure if I did it correctly though.

for i in *.MOD;
  do name=`echo "$i" | cut -d'.' -f1`
  echo "$name"
  ffmpeg -i "$i" "${name}.MOV"
done

This script is inside an exec file that I edited using TextEdit, it works sometimes if I go to the directory through the terminal and run it but if I run it double clicking it I get a ".MOD: Not such file or directory" even though I placed the exec on a folder that has multiple .MOD files.

Here is the full terminal output

Last login: Sat Apr 23 20:00:38 on ttys001
/Users/anwar/Movies/FILMS/ANWAR/BLUE/old/batmodmp4 ; exit;
anwar@MacBook-Pro-de-Anwar ~ % /Users/anwar/Movies/FILMS/ANWAR/BLUE/old/batmodmp4 ; exit;
*
ffmpeg version 5.0 Copyright (c) 2000-2022 the FFmpeg developers
  built with Apple clang version 13.0.0 (clang-1300.0.29.30)
  configuration: --prefix=/opt/homebrew/Cellar/ffmpeg/5.0 --enable-shared --enable-pthreads --enable-version3 --cc=clang --host-cflags= --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libdav1d --enable-libmp3lame --enable-libopus --enable-librav1e --enable-librist --enable-librubberband --enable-libsnappy --enable-libsrt --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvmaf --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libspeex --enable-libsoxr --enable-libzmq --enable-libzimg --disable-libjack --disable-indev=jack --enable-videotoolbox --enable-neon
  libavutil      57. 17.100 / 57. 17.100
  libavcodec     59. 18.100 / 59. 18.100
  libavformat    59. 16.100 / 59. 16.100
  libavdevice    59.  4.100 / 59.  4.100
  libavfilter     8. 24.100 /  8. 24.100
  libswscale      6.  4.100 /  6.  4.100
  libswresample   4.  3.100 /  4.  3.100
  libpostproc    56.  3.100 / 56.  3.100
*.MOD: No such file or directory

Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.


Solution 1:[1]

When you use cd in a session in Terminal, you're changing the 'working directory', so the script is able to resolve *.MOD as files in that working directory. When you run a script via double-click in Finder, the working directory starts as your home directory /Users/anwar. Since the the location of the shell script does not affect the current directory, it's unable to find any files that match the pattern.

Some possible ways to make this work:

Option 1

Do everything in Terminal, and use cd. With a little practice, you'll find that it's a flexible environment that provides better feedback, and it will be easier to customize. By setting some preferences, you can open Terminal tabs with the working directory set as a folder from Finder.

Option 2

Hard-code the working directory into each copy of the script:

cd '/Users/anwar/Movies/FILMS/ANWAR/BLUE/old'
for i in *.MOD; do
  name=`echo "$i" | cut -d'.' -f1`
  echo "$name"
  ffmpeg -i "$i" "${name}.MOV"
done
Option 3

Change the script so that the working directory is based on the script location:

#!/usr/bin/env zsh

cd "$0:h:a"
print "working directory:"
pwd

for infile in *.MOD; do
  outfile=${infile:r}.MOV
  print "infile: $infile"
  print "outfile: $outfile"
  ffmpeg -i "$infile" "$outfile"
done

This uses some zsh parameter expansions and modifiers. Note that the hashbang (the line starting with #!) must be the first line in the file.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gairfowl