Convert all files in directory using FFMPEG

1

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

Posted 2015-04-27T22:14:44.697

Reputation: 113

Answers

3

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

Hastur

Posted 2015-04-27T22:14:44.697

Reputation: 15 043

Thank you for this it worked perfectly (Had to remove the . in front of the path though. Is it used for anything? – Tom – 2015-04-29T08:18:28.523

You're welcome. The .? ...Nope. just a typo (I try it on a local subdirectory so ./what/ever/else) – Hastur – 2015-04-29T08:43:10.450

You may also want to link to http://mywiki.wooledge.org/ParsingLs (it's less verbose than the Unix.SE question)

– slhck – 2015-04-29T08:43:16.543

@slhck Thanks added references. I agree it's quicker and less scaring. – Hastur – 2015-04-29T08:49:07.773

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

Giorgio Bar

Posted 2015-04-27T22:14:44.697

Reputation: 21