Sunday, June 9, 2013

Bash Download of M3U MP3 Playlist


I have a list of MP3’s I regularly listen to which are all pulled from a website via a “*.m3u” list.  My wife wanted the songs, but she wants to listen to them on her IPhone.  So I whipped up this Bash script to take a “*.m3u” file as an argument, and pull all the tracks in the playlist down to the current directory.

If you look at the m3u playlist in Itunes, you may see some streaming files that are of duration “continuous”.  Those will not download correctly using this method.

#!/bin/bash

# Takes a list of MP3's (generally an m3u file) and downloads all to current directory
# www.klugedeforce.com
#
if [ $# != 1 ] ; then
  echo "usage $0 mp3_list_file.txt"
  echo "file list can have any extension, but should contain list of files one per line:"
  echo ""
  echo "http://downloads.hpmgl.net/mp3song_file1.mp3"
  echo "http://downloads.hpmgl.net/song_file2.mp3"
  echo "http://downloads.hpmgl.net/song_file3.mp3"

elif [ -e $1 ] ; then
   IFS=$'\r\n'
   for line in $(cat $1); do
       trimmed_line="$(echo ${line} | sed 's/^[ \t\r\n]*//g' | sed 's/[ \t\r\n]*$//g')"
       # trimmed_line=$line | sed 's/^[ \t\r\n]*//g' | sed 's/[ \t\r\n]*$/b/g'
       outfile=${trimmed_line##http://*/}
       if [ -n "$outfile" ] ; then
       `curl "${trimmed_line}" > "${outfile}"`;
          echo $outfile;
       fi
   done
else
   echo "Filename $1 not found"
fi          


No comments:

Post a Comment