You are not logged in.

#1 2007-02-09 15:10:20

Zoranthus
Member
From: muc
Registered: 2006-11-22
Posts: 166

Mass converter

I made a little program in python for something that certainly has a solutions somewhere already (most probably even in form of a shell script big_smile) but I just couldn't find anything, so here it goes:
It's a mass converting program that can do stuff like:
convert /opt/bla/*.tga /opt/bla/*.jpg
or
lame /opt/blah/*.wav /opt/blah/*.mp3

For converting all file in directories+subdirecotires to something else.

you select the program to use, the directory to work in, the in- and output file types and wether or not to recurse into subdirectories in a little text dialogue; that was it.

You can download the binary here
or view the source code below.

This can probably be infinitely extended for use with arguments behind the command, behind the single file names and behind the entire expression, as well as for renaming (enumerated), command line arguments, gui etc. Feel free to demand such things or just do it yourself, if enough people bother (= if there aren't 10000 programs that do the same, jsut better already) I'll probably move it to AUR.

#Author: Zoranthus
import os
import subprocess
import dircache


directory = raw_input("Target directory: ")

if directory[len(directory)-1]!="/":                         #Auto-correction of "/blah" to "/blah/
        directory=directory+"/"
ending1 = raw_input("Convert *.")
ending2 = raw_input("To *.")
program = raw_input("Using the program: ")
subdir = raw_input("Recurse into subdirectories (y/n)? ")

start=directory

def get_dirs(pathlist,pathtodo):
                content = dircache.listdir(pathtodo[0])
                for entity in content:
                        if os.path.exists(pathtodo[0]+entity+"/")==True and subdir=="y":
                                pathlist=pathlist+[pathtodo[0]+entity+"/"]
                                pathtodo=pathtodo+[pathtodo[0]+entity+"/"]
                        else:
                                pass
                if len(pathtodo)>1 and subdir=="y":
                        del pathtodo[0]
                        get_dirs(pathlist,pathtodo)
                else:
                        for dirs in pathlist:
                                content = dircache.listdir(dirs)
                                for filename in content:
                                        length = len(filename)
                                        position = len(filename)-len(ending1)-1
                                        filenamebase = filename[0:position]
                                        ii=0
                                        dotpos=0
                                        while ii<len(filename):
                                                dot=filename[ii]
                                                if dot ==".":
                                                        dotpos=ii
                                                        ii=ii+1
                                                else:
                                                        pass
                                                ii=ii+1
                                        if filename[dotpos:length] != "."+ending1:
#                                               print filename+"  skipped"    #output of skipped filenames
                                                pass
                                        else:
                                                print dirs+":  "+filename+" ---> "+filenamebase+"."+ending2
                                                file1 = filenamebase+"."+ending1
                                                file2 = filenamebase+"."+ending2
                                                subprocess.call([program, dirs+file1, dirs+file2])
get_dirs([start],[start])
print '\n'+"Done."

Last edited by Zoranthus (2007-02-09 15:10:36)

Offline

#2 2007-02-09 15:45:07

F
Member
Registered: 2006-10-09
Posts: 322

Re: Mass converter

I don't know python, but looking through that code it doesn't seem to me as if its really converting anything, rather just renaming files (remember that file extensions are almost useless in linux).

If you do instead want to convert, say, .wav files to .mp3 files, I wrote a zsh script a long time ago to help me rip CDs, maybe it can be of some use to you all. (Note, I used lame to convert .wav files to .mp3 files)

#!/bin/zsh
# pcp stands for post cdparanoia.
# By: F

for file in *.cdda.wav ; do lame ${file} ${file:r}.mp3 ; done

change_filename() {
    for file in *.cdda.mp3 ; do
        echo -n "Rename ${file} to: "
        read i
        mv ${file} ${i}.mp3
    ; done
}

echo -n "Would you like to rename your files? (y/n) "
read input
if [[ $input == y ]] ; then
    change_filename
else
    exit
fi

Last edited by F (2007-02-09 15:46:37)

Offline

#3 2007-02-10 20:18:10

noriko
Member
From: In My Mind
Registered: 2006-06-09
Posts: 535
Website

Re: Mass converter

@Zoranthus ;
it looks like a nice little script.
but i think it's be better / more usable  imho if you used sys args instead of input, as that can get annoying for me at least if i have to do it more than once.


The.Revolution.Is.Coming - - To fight, To hunger, To Resist!

Offline

Board footer

Powered by FluxBB