-   Linux - Newbie (
-   -   stuck at bash script to convert files with ffmpeg (

panseluta 03-10-2011 01:57 AM

stuck at bash script to convert files with ffmpeg

I have a samba share in my network with folders and subfolders. Every day users must add new video files DV AVI and MPEG2. DV AVI is to large to keep for a long time and I looked for an easy script to convert only DV AVI files to MPEG2.

I found two scripts each with one little problem:

First one converts the avi from main folder only and not from whole directory tree:



for file in *.avi
        ffmpeg -i "$file" -target dvd -aspect 4:3 -sameq "`basename "$file" .avi`.mpg";

Second one converts all files in the directory tree (every file in it's place) but the output file is named like this: name_of_the_file.avi.mpg



find -name '*.avi' -exec ffmpeg -i {} -target dvd -aspect 4:3 -sameq {}.mpg \;

How can I get an output without the .avi?

Big thanks!!

ozanbaba 03-10-2011 02:29 AM



find -name '*.avi' -exec ffmpeg -i {} -target dvd -aspect 4:3 -sameq `basename {} .avi`.mpg \;

How about this

jschiwal 03-10-2011 02:49 AM

You can use filename substitution;

I'd also recommend using better options for ffmpeg instead of sameq. It will use a larger bandwidth than necessary because it is trying to avoid any loss in quality using a lossy compression scheme.

Determine the optimum format and options for your needs or device and stick with it. The input doesn't matter much.

At work, I will convert commercials using ffmpeg.
This is the command I use for fixed 7MB/sec interlaced commercial spots:

ffmpeg -i <videoin> -f dvd -vcodec mpeg2video -pix_fmt yuv420p -vb 7000k -r 29.97 -minrate 7000k -maxrate 7000k -bufsize 800k -flags +ilme+ildct \
      -acodec mp2 -ab 224000 -ar 44100 -pass 1 -vol 256 -y <vidout>.mpg

This is probably too high of bandwidth for longer videos. Most of the options are learned by trial and error when something new crops up, and is left in. you may also want to include the size and aspect ration of the output video.

For variable rate videos, look at the qmin and qmax values instead of minrate and maxrate.

panseluta 03-10-2011 03:35 AM


Originally Posted by ozanbaba (Post 4285170)


find -name '*.avi' -exec ffmpeg -i {} -target dvd -aspect 4:3 -sameq `basename {} .avi`.mpg \;

How about this

this one doesn't change the output name. still outputs with .avi.mpg

panseluta 03-10-2011 06:00 AM

I used -sameq in my post because it's not relevant at this point.

I was able to solve it somehow:



find -name '*.avi' -exec ffmpeg -i {} -target dvd -aspect 4:3 -sameq {}.mpg \;

find . -name "*.avi.mpg" -exec rename 's/\.avi.mpg$/\.mpg/i' {} \;

So the first line takes all avi files in folder and subfolders and converts them to mpg but the resulting output is named with .avi.mpg.
The second line renames .avi.mpg to .mpg.

The script is not yet ready for my needs but the problem is SOLVED :)

Many thanks!!

grail 03-10-2011 08:28 AM

Or if you wanted you could have just put your first 2 scripts together (kinda):


while read -r file
    ffmpeg -i "$file" -target dvd -aspect 4:3 -sameq "${file%.avi}.mpg"
done< <(find . -name '*.avi')

schneidz 03-10-2011 09:20 AM

man sed:

[schneidz@hyper clips]$ echo filename.mpg | sed s/mpg/avi/

also i like the file command since it displays the actual file type (not just the file extension):

[schneidz@hyper clips]$ file  hobo-with-a-gun.mpg
hobo-with-a-gun.mpg: RIFF (little-endian) data, AVI, 320 x 240, ~30 fps, video: XviD, audio: MPEG-1 Layer 3 (mono, 22050 Hz)

as you can see the file command identifies it as an avi video eventhough someone labelled it as an mpeg.

All times are GMT -5. The time now is 01:04 AM.