0

I am working with files that are named after dates, such as "2020-02-24-16-13.h264". I am trying to get MP4Box to process all of these files in a bash script, but I can't figure out how to name the output files properly.

Essentially, I would like the script to automatically take the name of the .h264 file and output to the same name, with .mp4

The command I am using to do it manually is:

MP4Box -add /mnt/usb/Convert/2020-02-24-16-13.h264 /mnt/usb/Converted/2020-02-24-16-13.mp4

I cannot switch to ffmpeg in my situation as a workaround.

I am very much hoping this is a simple command tweak but any input is greatly appreciated!

3
  • and the expected output is? – Jetchisel Feb 25 '20 at 4:20
  • Unless I'm misunderstanding you, the expected output is the same exact name as the input file, but instead ending with .mp4. Running this manually per each file works as expected, I would like it to automatically know what to name each output file based off of the input (.h264 file) though. – Jared Feb 25 '20 at 4:31
  • Ok, understood, see my answer. – Jetchisel Feb 25 '20 at 4:34
2

Something like this.

for f in /mnt/usb/Convert/2020-02-24-16-13.h264; do
   echo MP4Box -add "$f" "${f%.*}.mp4"
done

output

MP4Box -add /mnt/usb/Convert/2020-02-24-16-13.h264 /mnt/usb/Convert/2020-02-24-16-13.mp4

If you have a lot of files ending in *.h264

for f in /mnt/usb/Convert/*.h264; do
   echo MP4Box -add "$f" "${f%.*}.mp4"
done

If you're inside /mnt/usb/Convert use this.

for f in *.h264; do
  echo MP4Box -add "$f" "${f%.*}.mp4"
done

To ouput the converted files to the Converted directory, which the OP's original question (sorry for not noticing that.)

for f in /mnt/usb/Convert/2020-02-24-16-13.h264; do
  n=${f/Convert/Converted}
  echo MP4Box -add "$f" "${n%.*}.mp4"
done

Output.

MP4Box -add /mnt/usb/Convert/2020-02-24-16-13.h264 /mnt/usb/Converted/2020-02-24-16-13.mp4

To deal with all the files in batch.

for f in /mnt/usb/Convert/*.h264; do
  n=${f/Convert/Converted}
  echo MP4Box -add "$f" "${n%.*}.mp4"
done

An alternative to the ${string/search/replace} bash specific P.E.

for f in /mnt/usb/Convert/*.h264; do
  n=${f##*Convert}
  m=${f%Convert*}
  echo MP4Box -add "$f" "${m}Converted${n%.*}.mp4";
done

The above (last example) is not restricted to bash syntax but should work in any POSIX compliant shell.

  • Remove the echo if you're satisfied with the output.

  • See parameter expansion PAGER='less +/^[[:space:]]*parameter\ expansion' man bash

9
  • Thank you so much! This worked perfectly. Just as one last thing, how would I change it to output to /Converted instead of /Convert if i'm executing from my home directory? – Jared Feb 25 '20 at 4:55
  • I have updated the answer, sorry for not noticing that the output directory is different from the input. – Jetchisel Feb 25 '20 at 5:24
  • No problem at all! My naming scheme isn't the best either quite honestly. Ill try the new fix out as soon as I get the chance. Thank you for all your help! – Jared Feb 25 '20 at 5:24
  • I'm using the very last for statement in your post under "To deal with all the files in batch", and it seems to be giving me the same output as before so far. Using echo to see the output shows that the mp4 files are still outputting to the "Convert" directory. If it helps clarify anything, my Converted and Convert folders have the same parent directory. – Jared Feb 25 '20 at 5:46
  • The echo output has the same directory? It does work on my example yes? – Jetchisel Feb 25 '20 at 5:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.