1

I've looked at this post and this post among a few others. They provided some information as to how to get started, unfortunately my use case is slightly more complex.

I have pairs of video files that I run ffmpeg commands on. The command I run transfers metadata from the original files to the converted files and looks as follows:

christian$ ffmpeg -i file_1.mov -i file_1.mp4 -map 1 -map_metadata 0 -c copy file_1_fixed.mp4

This post explains what the command does, should an explanation be required. Basically the use case is that both the original file and the converted file have the same name, but different extensions. How would I go about writing a shell script that finds all such pairs in a directory and composes and runs the command specified above for each pair?

I assume, from a logical point of view that I would need to loop through all the pairs, get the file names, do some sort of string manipulation (if this is even possible with shell scripting) compose the command and run it.

Any resources you could point me towards would be deeply appreciated. Just to clarify, some pseudo code:

for (file in directory) {
  string name = file.getname
  string command = "ffmpeg -i " + name + ".mov -i " + name + ".mp4 -map 1 
  -map_metadata 0 -c copy " + name + "_fixed.mp4"
  run(command)
}

Hope this makes sense, please let me know if I should clarify more. Thank you for reading.

1 Answer 1

0

As you tagged this question with bash I send you this sketch for a bash script. This should work in general but you may adjust it to you actual needs:

#!/usr/bin/bash
# for debugging remove the hash from next line:
#set -x
# loop over all .mov files
for FILE in *.mov; do
  FILE_MP4="${FILE/.mov/.mp4}"
  FILE_FIXED="${FILE/.mov/_fixed.mp4}"
  ffmpeg -i "$FILE" -i "$FILE_MP4" -map 1 -map_metadata 0 -c copy "$FILE_FIXED"
done

Notes/Hints:

  1. for FILE in *.mov loops over all files with extension .mov but no other files. This is good because it will even work if called multiple times in the same directory.

  2. The for loop will search in the current directory. You may use cd to change to a specific directory. (Handling of absolute or relative file paths instead of names is also possible...)

  3. The quotes are choosen with care. Quoting in bash is very powerful but definitely not easy.

  4. To check this script, you may prefix your ffmpeg command with command echo. This is like a dry run. (You will see what would be called without the echo "prefix".)

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much @Scheff! The script you provided worked right out of the box, and also taught me some things about shell scripting in the process!
I did have to remove "$FILE_FIXED" after -i in the command, other than that everything went smoothly.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.