public
Description: personal scripts
Homepage:
Clone URL: git://github.com/mackstann/bin.git
bin / TranscodeToMP3
100755 64 lines (50 sloc) 1.439 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/bin/zsh
 
# Written by Nick Welch <nick@incise.org>. Author disclaims copyright.
 
# TranscodeToMP3: recursively find all files in the current directory with the
# specified extension (the only argument) and convert them to mp3, using
# mplayer to decode them, changing the extension to .mp3 in the new files'
# filenames and leaving the original files intact.
 
extension="$1"
threads=2
 
if [ "$extension" -eq ".mp3" ]
then
    echo "don't do that, it'll overwrite your files"
    exit 1
fi
 
#i=0
#files=()
#find . -name "*$extension" | sort | while read file
#do
# files[$i]="$file"
# i=$(($i+1))
#done
 
start=`date +%s`
 
elapsed_time()
{
    now=`date +%s`
    elapsed=$(($now - $start))
    minutes=$(($elapsed / 60))
    seconds=$(($elapsed % 60))
    printf "%02d:%02d" $minutes $seconds
}
 
transcode_file()
{
    filename="$1"
    mkfifo "$filename".pipe
    mp3_filename=`basename "$filename" "$extension"`.mp3
    DoItEasy mplayer -really-quiet -noconsolecontrols -ao pcm:file="$filename".pipe "$filename" 2>/dev/null &
    DoItEasy lame --quiet --preset extreme "$filename".pipe "$mp3_filename"
    rm -f "$filename".pipe
    echo T+`elapsed_time`" finished: $filename"
}
 
# launch them all gradually
 
echo "launching $threads threads at a time..."
 
find . -name "*$extension" | sort | while read file
do
    while [ `jobs | wc -l` -ge $threads ]
    do
        sleep 0.2
    done
    transcode_file "$file" &
done
 
wait