1

I have many videos in a folder, and I want to compress them all with one command.

To compress one video I use:

sudo apt-get install ffmpeg
ffmpeg -i input.mp4 output.mp4

But what if I have many videos I want to compress? I've tried the following:

ffmpeg -i ./videos ./compressed-videos

But then I get this error:

./videos: Is a directory
2

I would use bash for loop for this purpose - let's assume you are in the parent directory, that contains the directories videos/ and compressed/:

for f in videos/*mp4; do ffmpeg -i "$f" "compressed/${f##*/}"; done

Or if you want to convert any type of video file to mkv, you can use:

for f in videos/*; do ffmpeg -i "$f" "compressed/$(basename "${f%.*}").mkv"; done

In the above examples:

  • ${var##*/} will outputs all characters after the last slash /, so only the filename without the path will remains;
  • ${f%.*} will outputs all characters before the last dot, so the path and the filename will be kept but the file extension will be removed. Then the command substitution $(basename "/path/name") will outputs only the name without the path.

References:

| improve this answer | |

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.