Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File conversion script

Status
Not open for further replies.

cyberchris

Programmer
Joined
Jul 22, 2007
Messages
1
Location
GB
Ok...I've been trying to do this with a shell script but I've been advised to use Perl as I have some requirements that are a little more involved....I'm hoping someone can help as I don't know Perl at all...although I'm intermediate in PHP.

this is what I have as a shell script...it works but doesn't do everything I need.

Code:
###################

#!/bin/bash

cd /var/for f in $( find . -name "*.mov" -o -name "*.mpeg" -o -name "*.mpg" -o -name "*.mp4" -o -name "*.wmv" -o -name "*.3gp" ) ; do ffmpeg -i $f -s 384x240 -b 700000 -r 25 -acodec mp3 -ar 44100 -ab 128 -f flv `echo $f | sed "s/\.[^.]*$//"`.flv ; done

for f in $( find . -name "*.wav" -o -name "*.aif" -o -name "*.sd2" -o -name "*.wma" ) ; do ffmpeg -i $f -acodec mp3 -ar 44100 -ab 128 `echo $f | sed "s/\.[^.]*$//"`.mp3 ; done

###################

This script goes through a chosen directory plus all subdirectories and converts all video format files to .flv and audio format files to .mp3

I'm executing a linux command using ffmpeg to do this. this part I do know.

This works fine but I need to make this a Perl script and add the following features:

1. I need to have it work with files and folders with spaces in the name.

2. I need it to check if the .flv or .mp3 already exists and if not then convert the file...if so then just move on the the next file.

3. I would like the script to save all actions in some sort of 'converted log' file so I can see what it did exactly.

4. and lastly id like to have it handle any problems in a better way eg. if for some reason it can't convert the file or the conversion fails...it should save the error and path/filename in an 'error log' being different file to the 'converted log'. and also not stop the script(like it does now) if there is a problem then it just logs it and moves on to the next file.

Note: it has to save the new .flv or .mp3 with the same filename

eg:
mymovie.mp4 ....becomes......mymovie.flv
mysong.wav....becomes......mysong.mp3

and the newly converted file has to have the same ownership as the original file is was converted from.

any advice on this would be greatly appreciated.

Cheers
C
 
Hi

There are many ways to do it, I would suggest to use File::Find module for getting all desired files in your directory.

Then save these files in one array and proceed iterating over it and renaming the files. Something like this

For renaming the files:
Code:
foreach $file (@NAMES) {
    my $newname = $file;
    # change $newname
    rename($file, $newname) or  
        warn "Couldn't rename $file to $newname: $!\n";
}

Other way:

Code:
opendir(DIR, yourdirectory");
foreach (readdir(DIR)) {
    next unless -f;
    print "do something with $_\n";
}
closedir(DIR);

opendir(DIR, ".");
@files = grep {-f} readdir(DIR);
closedir(DIR);
foreach (@files) {
    print "do something with $_\n";
}

use File::Find;
sub wanted {
    if (-f) {
        print "do something with $File::Find::name\n";
    }
}
find(\&wanted, ".");

Some other tips:

Code:
# returns all files in the current working directory
@files = glob('*');

# how about only .pl files?
@files = glob('*.pl');

# gets all .conf files in /usr/local/etc
# @files will contain full path names like
#      /usr/local/etc/httpd.conf
@files = glob('/usr/local/etc/*.conf');

# if you don't want the path information, 
# you can s/// off the path
@files = map s|^/usr/local/etc/|| && $_, @files;

# or change your working directory
chdir '/usr/local/etc';
@files = glob('*.conf');

# but in that case, you're likely better off with *dir()
opendir DIR, '/usr/local/etc' or die $!;
@files = grep -f && /\.conf$/, readdir DIR;
closedir DIR;

Hopefully it will give you some ideas..

Cheers


dmazzini
GSM System and Telecomm Consultant

 

for doing system calls (to your ffmpeg program).

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those Who Say It Cannot Be Done Are Usually Interrupted by Someone Else Doing It; Give the wrong symptoms, get the wrong solutions;
 
if you know PHP why not use PHP? It looks like something PHP can handle as easily as perl. All you are doing is recursing through a directory tree to find the files you want and then calling a program to convert them to another file type. The checking if the file already exists and writing a log of the activity should be fairly trivial. I would think these are all things PHP can handle.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top