for loop work at multiple directory depth - bash

I have the following for loop in bash:
for file in "$1"/*PM.mov ; do
ffmpeg -i "$file" -an -f framemd5 "${file}.framemd5.txt"
done
I want to adjust it so that it will run on any *PM.mov file in a given directory regardless of the directory depth of that file. Right now the loop only runs on the top level of the directory. How do I change that?

Two possibilities:
Use globstar (and nullglob while we're at it):
shopt -s globstar nullglob
for file in "$1"/**/*PM.mov ; do
ffmpeg -i "$file" -an -f framemd5 "${file}.framemd5.txt"
done
Use find properly (but the previous one is better, as it's only a minor change to your code):
find "$1" -type f -name "*PM.mov" -exec sh -c 'file=$1; ffmpeg -i "$file" -an -f framemd5 "$file.framemd5.txt"' sh {} \;

use the find cmd
for file in $(find $1 -name "*PM.mov") ; do
ffmpeg -i "$file" -an -f framemd5 "${file}.framemd5.txt"
done

Related

Code must find all MP4 files within subdirectory to compress using ffmpeg

I have a drive with a lot of MP4 files which are tough to go through folder by folder and compress.
I'm trying to make a script that runs in terminal that will open a designated folder, find all .mp4 files in the subfolder, and compress the files using specs I designate with ffmpeg. Obviously, the output files should be much lower in size if done right. I'm drafting a code which I have an idea about below but I'm not too good with BASH and/or PERL.
for f in $(find ../ -iname '*.avi'); do
n=$(echo $f|sed -e 's/.avi/_cbr.mp4/i');
echo "ffmpeg [options] -i $f $n";
done
output:
ffmpeg [options] -i ../1hourjob/videncode/sound10s.avi ../1hourjob/videncode/sound10s_cbr.mp4
ffmpeg [options] -i ../1hourjob/videncode/t003.avi ../1hourjob/videncode/t003_cbr.mp4
ffmpeg [options] -i ../ffmpeg/Masha.avi ../ffmpeg/Masha_cbr.mp4
ffmpeg [options] -i ../ffmpeg/window.avi ../ffmpeg/window_cbr.mp4
I'm wondering if I can even make some sort of GUI for this too. I feel a bit lost.
You can recursively traverse the directories in bash like this:
avi_to_mp4() {
cd "$1"
for f in *; do
if [[ -d "$f" ]]; then
avi_to_mp4 "$f"
elif [[ "$f" == *.avi ]]; then
newf="${f:0: -4}.mp4"
echo "$f" to "$newf" # run your command here
fi
done
cd ..
}
avi_to_mp4 "$1"

Use parameter expansions in a command run from "find | xargs" to prevent output overwriting

I have this bash script that is looking for mp4 files in subfolders with certain names and saves frames of those videos as jpeg.
#!/bin/bash
find ../folder -type f -iname '*C00*.mp4' | xargs -I %% ffmpeg -i %% -vf fps=1 -q:v 3 "../frames/_${i%.*}_frame%d.jpg"
The problem is that everytime the script finishes one video the .jepg output files of the next videos are overwriting the existing ones.
How can I prevent that?
Here's a quick stab which creates a directory with the same name as the input file with any .mp4 extension trimmed off.
#!/bin/bash
find ../folder -type f -iname '*C00*.mp4' -print0 |
xargs -r0 sh -c 'for f; do
d="../frames/${f%.[Mm][Pp]4}"
mkdir "$d" || { echo "$d already exists" >&2; exit 123; }
ffmpeg -i "$f" -vf fps=1 -q:v 3 "$d/frame%d.jpg"
done' _

Bash script that lists files in a directory doesn't work

I made a bash script because I need to convert a lot of files in a directory from .MOV to .mp4 format.
I created this script for the purpose:
#!/bin/bash
touch .lista
ls -1 "$1" | grep -i .MOV > .lista
list= `pwd`/.lista
cd "$1"
while read -r line;
do filename=${line%????}
ffmpeg -i "$line" -vcodec copy -acodec copy "$filename.mp4"; done < $list
rm .lista
This script is supposed to convert me each .MOV file into the directory indicated by $1, but it doesn't work, it converts me only one file, then it terminates. I can't understand why. What's wrong with that?
It's better to simply loop using globs:
for file in "$1"/*.MOV; do
ffmpeg -i "$file" ... "${file%.*}.mp4"
done
Why you shouldn't parse the output of ls.
Do them all fast and succinctly in parallel with GNU Parallel like this:
parallel --dry-run ffmpeg -i {} -vcodec copy -acodec copy {.}.mp4 ::: movies/*MOV
Sample Output
ffmpeg -i movies/a.MOV -vcodec copy -acodec copy movies/a.mp4
ffmpeg -i movies/b.MOV -vcodec copy -acodec copy movies/b.mp4
If that looks good, do it again but without --dry-run.
Note how easily GNU Parallel takes care of all the loops, all the quoting and changing the extension for you.
Your code is working for me. I cannot see any error. But I can suggest you a better approach. Don't use ls to get the filenames, it is not a good idea. Also, you can avoid changing dir.
#!/bin/bash
for line in $(find "$1" -maxdepth 1 -type f -iname "*.mov")
do
ffmpeg -i "$line" -vcodec copy -acodec copy "${line%????}.mp4"
done
You don't need to start by touching the file. In any case, you don't need a file at all, you can use a for loop to iterate over the files returned by find directly. With find, I'm already selecting all the files in the specified folder that have the expected extension.
Here I add a one-liner that should avoid problems with spaces:
find "$1" -maxdepth 1 -type f -iname "*.mov" -print0 | xargs -0 -n 1 -I{} bash -c "F={}; ffmpeg -i \"\$F\" -vcodec copy -acodec copy \"\${F%.*}\".mp4"

Running a bash script recursively and performing operations on all files within the subdirectories

I'm trying to convert flac files into wav files using ffmpeg. The flac files are located in various subdirectories.
/speech_files
/speech_files/201/speech1.flac
/speech_files/201/speech2.flac
/speech_files/44/speech45.flac
/speech_files/44/speech109.flac
/speech_files/66/speech200.flac
/speech_files/66/speech33.flac
What I want after the script runs is the following
/speech_files
/speech_files/201/speech1.wav
/speech_files/201/speech2.wav
/speech_files/44/speech45.wav
/speech_files/44/speech109.wav
/speech_files/66/speech200.wav
/speech_files/66/speech33.wav
I can get my script to work within one directory but I'm having a hard time getting it to run from the top level directory (speech_files) and work it's way through all the subdirectories. Below is the script I'm using.
#!/bin/bash
for f in "./"/*
do
filename=$(basename $f)
if [[ ($filename == *.flac) ]]; then
new_file=${filename%?????}
file_ext="_mono_16000.wav"
wav_file_ext=".wav"
ffmpeg -i $filename $new_shits$wav_file_ext
ffmpeg -i $new_file$wav_file_ext -ac 1 -ar 16000 $new_file$file_ext
rm -f $filename
rm -f $new_file$wav_file_ext
fi
done
Use find from the top level directory and filter by using *.flac.
for f in $(find . -name "*.flac"); do
echo "$f" # f points to each file
# do your logic here
done
Using bash only :
#!/bin/bash
DIR="/.../speech_files"
process() {
filename=$(basename "$1")
# ...
}
for f in n "${DIR}"/*/*.flac; do
process "$f"
done
Using find which is recursive and more efficient to do that kind of task to me :
find "${DIR}" -type f -a -iname "*.flac" -exec ... {} \;

How to pipe multiple files to ffmpeg?

I am trying to make a bash script that searches all subfolders on given path for .mov files and converts them with ffmpeg and outputs them in an destination folder, keeping the clip name.
I'm very new to scripting and I'm having a hard time finding out how to solve this.
So far I've tried using ls and find to output the filepaths, but have no idea how to pipe this to ffmpeg in the right way.
Any clues?
Edit:
got some sucess with this:
#!/bin/bash
echo "drop source folder: "
read source
echo "drop destination folder: "
read des
find "$source" -name '*.mov' -exec sh -c 'ffmpeg -i "$0" -vcodec prores -profile:v 0 -an "$des/${0%%.mov}.mov"' {} \;
exit;
but, the it seems to output to the source folder asking for a overwrite. How can i setup the parameters correctly so it outputs to the "destination folder" and keeps the filenames?
You could start with this:
#!/bin/bash
shopt -s extglob || {
echo "Unable to enable exglob."
exit 1
}
TARGETEXT='.avi'
TARGETPREFIX='/path/to/somewhere/' ## Make sure it ends with /.
while IFS= read -r FILE; do
BASE=${FILE##*/}
NOEXT=${BASE%.*}
TARGETFILEPATH=${TARGETPREFIX}${NOEXT}${TARGETEXT}
echo ffmpeg -i "$FILE" "$TARGETFILEPATH" ## Remove echo if it's already correct.
done < <(exec find -type f -name '*.mov') ## You could also use -iname '*.sh' instead.
Of course you could use a custom directory to search for the files:
find /path/to/directory -type f -name '*.mov'
something like this should do the job:
for f in *.mov; do ffmpeg -i "$f" -vcodec copy -acodec copy "/desination/${f%.mov}.mp4"; done

Resources