Visit the LQ Articles and Editorials section
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 03-10-2011, 01:57 AM   #1
LQ Newbie
Registered: Dec 2008
Posts: 20

Rep: Reputation: 1
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!!
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 03-10-2011, 02:29 AM   #2
Registered: May 2003
Location: Tengiz
Distribution: Slackware64 14.1
Posts: 672

Rep: Reputation: 94

find -name '*.avi' -exec ffmpeg -i {} -target dvd -aspect 4:3 -sameq `basename {} .avi`.mpg \;
How about this
Old 03-10-2011, 02:49 AM   #3
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
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.
1 members found this post helpful.
Old 03-10-2011, 03:35 AM   #4
LQ Newbie
Registered: Dec 2008
Posts: 20

Original Poster
Rep: Reputation: 1
Originally Posted by ozanbaba View Post

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
Old 03-10-2011, 06:00 AM   #5
LQ Newbie
Registered: Dec 2008
Posts: 20

Original Poster
Rep: Reputation: 1
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!!

Last edited by panseluta; 03-10-2011 at 06:22 AM.
Old 03-10-2011, 08:28 AM   #6
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 8,786

Rep: Reputation: 2470Reputation: 2470Reputation: 2470Reputation: 2470Reputation: 2470Reputation: 2470Reputation: 2470Reputation: 2470Reputation: 2470Reputation: 2470Reputation: 2470
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')
2 members found this post helpful.
Old 03-10-2011, 09:20 AM   #7
Senior Member
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 4,903

Rep: Reputation: 817Reputation: 817Reputation: 817Reputation: 817Reputation: 817Reputation: 817Reputation: 817
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.

Last edited by schneidz; 03-10-2011 at 09:28 AM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Why is ffmpeg stopping after the first encode when using my bash script? slinkoff Programming 6 07-06-2011 04:27 AM
using ffmpeg to convert video files acamus Linux - Newbie 2 01-04-2011 08:41 PM
Bash script to convert all mp3 files to aac eeepc4gsurf Linux - Desktop 7 11-13-2009 07:25 PM
Bash script to convert .mpg files to .avi files? Mike_Snyder Linux - Newbie 5 09-19-2009 01:33 PM
Script to automatically convert video files using ffmpeg jroyce Linux - Software 3 01-04-2008 11:17 PM

All times are GMT -5. The time now is 09:03 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration