LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   stuck at bash script to convert files with ffmpeg (http://www.linuxquestions.org/questions/linux-newbie-8/stuck-at-bash-script-to-convert-files-with-ffmpeg-867636/)

panseluta 03-10-2011 02:57 AM

stuck at bash script to convert files with ffmpeg
 
Hello,

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:

Code:

#!/bin/sh

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

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

Code:

#!/bin/sh

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 03:29 AM

Code:

#!/bin/sh

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

How about this

jschiwal 03-10-2011 03:49 AM

You can use filename substitution;
file=a_movie.avi
outfile=${file%.avi}.mpg

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:
Code:

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 04:35 AM

Quote:

Originally Posted by ozanbaba (Post 4285170)
Code:

#!/bin/sh

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 07:00 AM

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

I was able to solve it somehow:

Code:

#!/bin/sh

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 09:28 AM

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

#!/bin/bash

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


schneidz 03-10-2011 10:20 AM

man sed:
Code:

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

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

[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 06:56 PM.