Reputation: 612
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:
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"
Upvotes: 1
Views: 1140
Reputation: 614
#!/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----------
Upvotes: 2
Reputation: 15300
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.
Upvotes: 2