Convert all files in directory using FFMPEG
Solution 1
In general it is not safe to parse the output of ls
(see below) and similarly "*.*"
.
I suggest to use find
it protects you from unusual file name with special character.
#!/bin/bash
#Convert files using ffmpeg
OrDir="/Volumes/Misc/To Convert 2/"
find "$OrDir" -type f -exec /bin/bash -c \
'f2=$(basename "$1"); \
ffmpeg -i "$1" -c:v libx264 -crf 19 -preset slow -c:a aac -strict experimental -b:a 192k -ac 2 "/Volumes/Misc/Converted/${f2%.*}.m4v" ' _ {} \;
You can check it with a filename such as
cp myfile.mpg myfile$'\n'with_new_line.mpg
References
- Why is it not safe or wise to parse the output of
ls
?
There are a lot of references on internet.
You can have a quick understanding here.
You can read a lot more here
Solution 2
As the directory name contains spaces, you need to quote "$DIR"
to prevent spaces from being parsed as word separators.
I would also suggest you slightly change the for loop:
#!/bin/bash
DIR="/Volumes/Misc/To Convert 2"
cd "$DIR"
for i in *.*; do
ffmpeg -i "$i" -your_option "/Volumes/Misc/Converted/${i%.*}.m4v"
done
Related videos on Youtube
Tom
Updated on September 18, 2022Comments
-
Tom almost 2 years
I'm trying to write a script to convert all the files in a directory to m4v, whilst trying to keep similar (if not the same) quality and compress the file to reduce the overall size.
I've pieced together a small batch script but it won't run. Can anyone lend a hand/suggest how best to compress and retain quality?
#!/bin/bash #Convert files using ffmpeg DIR="/Volumes/Misc/To Convert 2" for i in $DIR; do ffmpeg -i "$i" -c:v libx264 -crf 19 -preset slow -c:a aac -strict experimental -b:a 192k -ac 2 "/Volumes/Misc/Converted/${i%.*}.m4v" done
EDIT: OUTPUT This is the output that I get when I try to run the shell script
ffmpeg version 2.6.1-tessus Copyright (c) 2000-2015 the FFmpeg developers built with Apple LLVM version 6.0 (clang-600.0.57) (based on LLVM 3.5svn) configuration: --cc=/usr/bin/clang --prefix=/Users/tessus/data/ext/ffmpeg/sw --as=yasm --extra-version=tessus --disable-shared --enable-static --disable-ffplay --enable-gpl --enable-pthreads --enable-postproc --enable-libmp3lame --enable-libtheora --enable-libvorbis --enable-libx264 --enable-libx265 --enable-libxvid --enable-libspeex --enable-bzlib --enable-zlib --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libxavs --enable-libsoxr --enable-libwavpack --enable-version3 --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-libvpx --enable-libgsm --enable-libopus --enable-libmodplug --enable-fontconfig --enable-libfreetype --enable-libass --enable-libbluray --enable-filters --disable-indev=qtkit --disable-indev=x11grab_xcb --enable-runtime-cpudetect libavutil 54. 20.100 / 54. 20.100 libavcodec 56. 26.100 / 56. 26.100 libavformat 56. 25.101 / 56. 25.101 libavdevice 56. 4.100 / 56. 4.100 libavfilter 5. 11.102 / 5. 11.102 libswscale 3. 1.101 / 3. 1.101 libswresample 1. 1.100 / 1. 1.100 libpostproc 53. 3.100 / 53. 3.100 /Volumes/Misc/ToConvert: Operation not permitted
-
Tom about 9 yearsThank you for this it worked perfectly (Had to remove the
.
in front of the path though. Is it used for anything? -
Hastur about 9 yearsYou're welcome. The
.
? ...Nope. just a typo (I try it on a local subdirectory so./what/ever/else
) -
slhck about 9 yearsYou may also want to link to mywiki.wooledge.org/ParsingLs (it's less verbose than the Unix.SE question)