1

I wrote a simple bash script to convert video files. When run, it first makes output directory and then iterates over all .mp4 files, converts them and puts the result to output directory.

mkdir output && for f in *.mp4; do echo $f; \
ffmpeg -loglevel warning -i $f -vf scale=1280:-2,fps=25 output/$f; done

I'd like to add to the script one feature: looping in all the sub-directories (but no the current directory itself), creating 'output` directory and executing conversion.

I managed to find all files meeting given criterion in all sub-directories by using this code:

for d in */; do
  for f in "$d"/*.mp4; do
    echo "$d"
    file="${f##*/}"
    echo "$file"
  done
done

The script prints the following, which is correct.

1/
01.mp4
2/
02.mp4

However, that's all I was able to do.

1 Answer 1

1

You can use find to search directories recursively.

$ find -mindepth 2 -type f ! -path '*/output/*' -name '*.mp4' \
  -exec bash -c 'mkdir -p "${1%/*}/output"; ffmpeg -loglevel warning -i "$1" -vf scale=1280:-2,fps=25 "${1%/*}/output/${1##*/}"' _ {} \;

Or a for loop with globstar:

#!/bin/bash

shopt -s globstar nullglob

for i in **/*.mp4; do
    if [[ $i != */output/* ]]; then
        mkdir -p "${i%/*}/output"
        ffmpeg -loglevel warning -i "$i" -vf scale=1280:-2,fps=25 "${i%/*}/output/${i##*/}"
    fi
done

5
  • $ find -mindepth 2 -type f -name '*.mp4' -exec \ bash -c 'ffmpeg -loglevel warning -i $1 -vf scale=1280:-2,fps=25 output/${1##*/}' _ {} \; ./pp/1-: No such file or directory. I tried to use "$1" as files have spaces but it didn't work either.
    – menteith
    Mar 26, 2020 at 19:13
  • Sorry, I meant "$1".
    – menteith
    Mar 26, 2020 at 19:18
  • Now it prints output/ok.mp4: No such file or directory.
    – menteith
    Mar 26, 2020 at 19:25
  • When I create output directory in a directory where an mp4 is located and then use cd .. and run your code, I still get that message.
    – menteith
    Mar 26, 2020 at 19:28
  • Unfortunately, it didn't do the trick. I'm getting : No such file or directory.
    – menteith
    Mar 26, 2020 at 19:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .