File [scripts/flac_downto_128k.sh]

1  #!/bin/bash
2  #
3  # Convert all flac files to mp3 @ 128k using lame
4  #
5  # Thanks to http://blog.tordeu.com/?p=184 for snippets
6  #
7  if [ $1 ]; then
8    do_artist=$1
9  else
10    do_artist=""
11  fi
12  base_dir="${HOME}/Music"
13  high_dir="flac"
14  tmpy=/tmp/flac_downto_128k
15  bits_out=128
16  out_dir="mp3-${bits_out}kbit-resampled"
17  # Some stat counters
18  let n_artists=0
19  let n_albums=0
20  let n_tracks=0
21  let n_artists_r=0
22  let n_albums_r=0
23  let n_tracks_r=0
24  let n_artists_c=0
25  let n_albums_c=0
26  let n_tracks_c=0
27  last_artist=""
28  last_album=""
29  # Create a tmp directory for dumps
30  if [ ! -d "${tmpy}" ]; then
31    mkdir ${tmpy}
32  fi
33  # Scan through all of the high quality directories
34  for this_high_dir in ${high_dir}
35  do
36    # Scan for artists
37    echo "Scanning ${this_high_dir} directory ..."
38    ls -1 "${base_dir}/${this_high_dir}" > ${tmpy}/${this_high_dir}
39    # Scan for albums
40    while read artist
41    do
42      if [ "${do_artist}" = "" ] || [ "${do_artist}" = "${artist}" ]; then
43        let n_artists=$n_artists+1
44        # Scan for tracks
45        echo " Found ${artist} - scanning for albums ..."
46        artist_clean=`echo ${artist//[ \[\]()&;\'\"\,\’:*?]/_} | tr -s _ _ `
47        if [ "${artist}" != "${artist_clean}" ]; then
48          echo " Renaming ${artist} to ${artist_clean}"
49          mv "${base_dir}/${this_high_dir}/${artist}" "${base_dir}/${this_high_dir}/${artist_clean}"
50          artist=${artist_clean}
51          let n_artists_r=$n_artists_r+1
52        fi
53        ls -1 "${base_dir}/${this_high_dir}/${artist}" > ${tmpy}/${this_high_dir}-${artist}
54        while read album
55        do
56          let n_albums=$n_albums+1
57          echo " Found ${album} - scanning for tracks ..."
58          album_clean=`echo ${album//[ \[\]()&;\'\"\,\’:*?]/_} | tr -s _ _ `
59          if [ "${album}" != "${album_clean}" ]; then
60            echo " Renaming ${album} to ${album_clean}"
61            mv "${base_dir}/${this_high_dir}/${artist}/${album}" "${base_dir}/${this_high_dir}/${artist}/${album_clean}"
62            album=${album_clean}
63            let n_albums_r=$n_albums_r+1
64          fi
65          ls -1 "${base_dir}/${this_high_dir}/${artist}/${album}" > ${tmpy}/${this_high_dir}-${artist}-${album}
66          # Now the fun bit ... let's start re-encoding the track we've just found
67          while read track
68          do
69            track_clean=`echo ${track//[ \[\]()&;\'\"\,\’:*?]/_} | tr -s _ _ `
70            if [ "${track}" != "${track_clean}" ]; then
71              echo " Renaming ${track} to ${track_clean}"
72              mv "${base_dir}/${this_high_dir}/${artist}/${album}/${track}" "${base_dir}/${this_high_dir}/${artist}/${album}/${track_clean}"
73              track=${track_clean}
74              let n_tracks_r=$n_tracks_r+1
75            fi
76  this_source="${base_dir}/${this_high_dir}/${artist}/${album}/${track}"
77  this_dest=`echo ${base_dir}/${out_dir}/${artist}/${album}/${track} | sed 's/.flac/.mp3/g'`
78    # Check that it's a FLAC file
79  if [ `file "${this_source}" | grep -c "FLAC audio bitstream data"` = "1" ]; then
80              let n_tracks=$n_tracks+1
81              # Check the datestamps
82  source_date=`stat -c %Y "${this_source}"`
83  if [ -f "${this_dest}" ]; then
84  dest_date=`stat -c %Y "${this_dest}"`
85  else
86  dest_date=0
87  fi
88     # Only encode or copy if dest is older than source
89  if [ ${dest_date} -lt ${source_date} ]; then
90  ##############################################
91  # Time to do what we were sent here to do!!! #
92  ##############################################
93                # Get tag data from source.
94  # Artist
95  s_artist=`metaflac --show-tag=ARTIST "${this_source}" | awk -F "=" '{print $2}'`
96  # Album
97  s_album=`metaflac --show-tag=ALBUM "${this_source}" | awk -F "=" '{print $2}'`
98  # Track title
99  s_track=`metaflac --show-tag=TITLE "${this_source}" | awk -F "=" '{print $2}'`
100  # Genre
101  s_genre=`metaflac --show-tag=GENRE "${this_source}" | awk -F "=" '{print $2}'`
102  # Year
103  s_year=`metaflac --show-tag=DATE "${this_source}" | awk -F "=" '{print $2}' | awk -F "-" '{print $1}'`
104  # Track number
105  s_track_no=`metaflac --show-tag=TRACKNUMBER "${this_source}" | awk -F "=" '{print $2}'`
106  if [ `expr length "$s_track_no"` -eq 1 ]; then
107  s_track_no="0${s_track_no}"
108  fi
109  d_args="--ta \"${s_artist}\" --tg \"${s_genre}\" --tl \"${s_album}\""
110  d_args="${d_args} --tn \"${s_track_no}\" --tt \"${s_track}\""
111                if [ ! ${s_year} = "" ]; then
112                  d_args="${d_args} --ty \"${s_year}\""
113                fi
114                # Create the directory if it doesn't exist
115  if [ ! -d "${base_dir}/${out_dir}/${artist}/${album}" ]; then
116  mkdir -p "${base_dir}/${out_dir}/${artist}/${album}"
117  fi
118  # Now re-encode
119    echo " Re-encoding ${track} (to mp3-${bits_out}kbit)"
120  #echo "flac -c -d \"${this_source}\" 2>/dev/null | lame -B 128 -V 4 ${d_args} - \"${this_dest}\" 2>/dev/null"
121  eval "flac -c -d \"${this_source}\" 2>/dev/null | lame -B 128 -V 4 ${d_args} - \"${this_dest}\" 2>/dev/null"
122                let n_tracks_c=$n_tracks_c+1
123                if [ "${artist}" != "${last_artist}" ]; then
124                  let n_artists_c=$n_artists_c+1
125                fi
126                if [ "${album}" != "${last_album}" ]; then
127                  let n_albums_c=$n_albums_c+1
128                fi
129                last_artist=${artist}
130  last_album=${album}
131  ##########################
132  # Wasn't that simple?!?! #
133  ##########################
134  else
135  echo " Skipping ${track} (file up-to-date)"
136              fi
137  else
138  # Not a flac, so just copy (if not already up-to-date)
139              # Check the datestamps
140  source_date=`stat -c %Y "${this_source}"`
141  if [ -f "${this_dest}" ]; then
142                dest_date=`stat -c %Y "${this_dest}"`
143  else
144    dest_date=0
145  fi
146  # Copy or skip?
147  if [ ${dest_date} -lt ${source_date} ]; then
148                # Create the directory if it doesn't exist
149  if [ ! -d "${base_dir}/${out_dir}/${artist}/${album}" ]; then
150  mkdir -p "${base_dir}/${out_dir}/${artist}/${album}"
151  fi
152  echo " Copying ${track} as not a flac"
153  cp "${this_source}" "${this_dest}"
154  else
155  echo " Skipping ${track} (file up-to-date)"
156              fi
157  fi
158          done <${tmpy}/${this_high_dir}-${artist}-${album}
159        done <${tmpy}/${this_high_dir}-${artist}
160      fi
161    done <${tmpy}/${this_high_dir}
162  done
163  echo ""
164  echo "Found ${n_tracks} tracks in ${n_albums} albums from ${n_artists} artists"
165  echo "Renamed ${n_tracks_r} tracks, ${n_albums_r} albums and ${n_artists_r} artists"
166  echo "Converted ${n_tracks_c} tracks in ${n_albums_c} albums from ${n_artists_c} artists"
167  echo ""
168  # Now clean up the tmp area
169  ""rm -f -R ${tmpy}



Del.ico.us Digg Facebook Google LinkedIn LiveJournal NewsVine reddit StumbleUpon Twitter
Valid XHTML 1.0 Transitional Valid CSS! [Valid Atom 1.0] [Valid RSS 2.0]
[ Page last updated Fri 5th Apr 2024 | viewed 23415 times ]