1

If I wanted to loop through a list of nested directories and run a set commands, how would I do that?

My directory structure is like this:

  • videos
    • folder1 -> VTS_01_1.mp4
    • folder2 -> VTS_01_1.mp4
    • folder3 -> VTS_01_1.mp4 .... and so on

I need to loop through each folder and run the script below.. All of the .mp4 files are named VTS_01_1.mp4, but I'd like to do it with a *.mp4 wildcard condition just incase they aren't. The output file in each directory should be "VTS_01_h264.mp4". Ideas? I'm using CentOS 6.4.

ffmpeg -y -i "VTS_01_1.mp4" -an -pass 1 -threads 2 -vcodec libx264 -b 512k -flags +loop+mv4 -cmp 256 \
       -partitions +parti4x4+parti8x8+partp4x4+partp8x8+partb8x8 \
       -me_method hex -subq 7 -trellis 1 -refs 5 -bf 3 \
       -flags2 +bpyramid+wpred+mixed_refs+dct8x8 -coder 1 -me_range 16 \
           -g 250 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -qmin 10\
       -qmax 51 -qdiff 4 "video_tmp.mp4"



ffmpeg -y -i "VTS_01_1.mp4" -acodec libfaac -ar 44100 -ab 96k -pass 2 -threads 2 -vcodec libx264 -b 512k -flags +loop+mv4 -cmp 256 \
       -partitions +parti4x4+parti8x8+partp4x4+partp8x8+partb8x8 \
       -me_method hex -subq 7 -trellis 1 -refs 5 -bf 3 \
       -flags2 +bpyramid+wpred+mixed_refs+dct8x8 -coder 1 -me_range 16 \
           -g 250 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -qmin 10\
       -qmax 51 -qdiff 4 "video_tmp.mp4"



qt-faststart "video_tmp.mp4" "VTS_01_h264.mp4"
1
  • 1
    There is no reason for you to declare so many options. That's what the x264 presets are for. You can simplify the command to: ffmpeg -y -i input -an -pass 1 -vcodec libx264 -preset slow -b:v 512k -f mp4 /dev/null && ffmpeg -i input -pass 2 -vcodec libx264 -preset slow -b:v 512k -acodec libfaac -ar 44100 -b:a 96k -movflags faststart output.mp4 The first pass output can be sent to /dev/null, -b is ambiguous so you should specify the stream, and you can replace qt-faststart with the -movflags faststart option in the 2nd pass. Commented Nov 22, 2013 at 19:44

2 Answers 2

2

The find command is very powerful in such things, try:

find videos/ -name "*.mp4" -exec ffmpegScript {} \;

This finds all files (also in subdirectories) with .mp4 as ending and executes ffmpegScript nameOfMp4File, where nameOfMp4File is the name of the file that was found, one at a time. find takes care of the looping itself.

Now we need to define the ffmpegScript:

#!/usr/bin/env bash

inputFile="$1"
outputFile="$(dirname $1)"/VTS_01_h265.mp4

ffmpeg -y -i "$inputFile" -acodec libfaac -ar 44100 -ab 96k -pass 2 -threads 2 -vcodec libx264 -b 512k -flags +loop+mv4 -cmp 256 \
   -partitions +parti4x4+parti8x8+partp4x4+partp8x8+partb8x8 \
   -me_method hex -subq 7 -trellis 1 -refs 5 -bf 3 \
   -flags2 +bpyramid+wpred+mixed_refs+dct8x8 -coder 1 -me_range 16 \
       -g 250 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -qmin 10\
   -qmax 51 -qdiff 4 "video_tmp.mp4"

qt-faststart "video_tmp.mp4" "$outputFile"

The inputFile variable is set as first positional parameter passed to the ffmpegscript, the outputFile variable is set with the same path, but different basename.

Note: This script will overwrite your output files if there's more than one *.mp4 file in a directory. Also, I didn't try the whole script, since I don't have any *.mp4 files available here.

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

Comments

2
#!/usr/bin/env bash

set -x

#----------+code----------
find *.mp4 -maxdepth 3 -type f | while read files
do
./ffmpeg.sh "${files}"

qt-faststart "${files}_tmp.mp4" "VTS_01_h264.mp4"
done
#----------.code----------

And in ffmpeg.sh:-

#!/usr/bin/env bash

set -x

#----------+code----------
ffmpeg -y -i "${1}" -an -pass 1 -threads 2 -vcodec \
    libx264 -b 512k -flags +loop+mv4 -cmp 256 \
   -partitions +parti4x4+parti8x8+partp4x4+partp8x8+partb8x8 \
   -me_method hex -subq 7 -trellis 1 -refs 5 -bf 3 \
   -flags2 +bpyramid+wpred+mixed_refs+dct8x8 -coder 1 -me_range 16 \
   -g 250 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -qmin 10\
   -qmax 51 -qdiff 4 "${1}_tmp.mp4"
#----------.code----------

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.