Another Windows playlists importer

Hi!
I’m missing M3u Import plugin from Volumio 3 and, not having seen this Importing M3U playlists into Volumio earlier, I thought I would try to do it myself in bash.
I’m almost a newbie in Linux and bash then my script is basical and written after trial and error and looking on the web for solutions to my needs. Moreover, it’s not a complete plug-in because it does not output metatags and artwork. Just after importing it, you will see meaningless artistn, titlen, albumn for every song. But one will only need to enqueue the imported playlist, open the queue, play it and save it with the same name or any desired name and all fields, including albumart, will be there. Should you use a different name, delete the imported one, afterwards.

Assumptions:

  1. the Windows folders structure of one’s music collection will be the same of the Volumio’s one. Example, the hard disk connected to your RPI will be a backup of the Windows’ one
  2. one will need only to customize the variables win_path and vol_path after checking the Windows playlist and the path of music folder in Volumio. Don’t delete slashes and backslashes here!
  3. Windows playlists must have absolute paths. For this reason, never save the Windows playlist where all the list’s music is. Just save them in the main music folder or in a ad hoc “Playlists” folder. This is because, example, Foobar saves relative paths if all songs are in the same folder of the playlist’s one
  4. Windows playlists must be m3u8, made with Foobar2000. These format supports UTF-8. Notice Foobar’s m3u8 playlists start with a first line containing only “#” and this script deletes the first line just for this reason. I don’t know whether other programs make similar starting playlists but, if they don’t, just comment the relevant line in the script to avoid deleting first playlist entry. One might use m3u too modifying the script but they should be tweaked to support UTF-8 (that’s what I do when I import my m3u8’s to another system running plain MPD)
  5. the to be converted playlists will need to be put in the Volumio directory where the script itself is run
  6. the playlists will be copied in /data/playlist with a command that does it for all extension-less files, thus check there are no other files like those in the Volumio directory where you are running the script. There must neither be sub-directories here. This command will output an error because it will try to copy them because they look like an extension-less file too: don’t consider it
#!/bin/bash
# Set your Windows PC music folder path
win_path='D:\\Data\\Music\\'

# Set your Volumio music folder path
vol_path='mnt\/USB\/disklabel\/Data\/Music\/'

# Remove BOM
find . -type f \( -name "*.m3u8" \) -exec sed -i '1s/^\xEF\xBB\xBF//' {} +

# Convert Windows CRLF To Unix LF line endings
sed -i 's/\r$//' *.m3u8

# Remove Foobar2000 first line (containing #)
sed -i '1d' *.m3u8

# Replace Windows music folder with Volumio music folder path
sed -i "s%$win_path%$vol_path%g" *.m3u8

# Replace \ with /
#sed -i -e 's/\\/\//g' *.m3u8
sed -i 's/\\/\//g' *.m3u8

# Prepend {"service":"mpd","uri":"
sed -i 's/^/{"service":"mpd","uri":"/' *.m3u8

# Append ","title":"titlen","artist":"artistn","album":"albumn","albumart":""} to each line
sed -i 's/$/","title":"titlen","artist":"artistn","album":"albumn","albumart":""}/' *.m3u8

# From multi-lines to one-liner (adds a "," between lines)
sed -i ':a; N; $!ba; s/\n/,/g' *.m3u8

# Prepend "["
sed -i 's/^/[/' *.m3u8

# Append "]"
sed -i 's/$/]/' *.m3u8

# Crop extension
for file in /data/INTERNAL/*.m3u8
do
    mv "$file" "${file%.m3u8}"
done

# Copy ready playlists to /data/playlist (you'll get an error message: forget it)
cp -- !(*.*) /data/playlists

Test it and feel free to improve and share with me too

Further to the interest in this script (lol) I improved it to be used with multiple playlists.
Along with that, now the title of the song will be displayed. But you know the filename can have " - " in it, then the title can be incomplete. Though it’s just a temporary title because you’ll see the proper one once the playlist is saved from within Volumio.

Again, one needs to customize his Windows music folder path and the Volumio one. In my case, I have example a certain drive label which is in that path. As the first version, this applies to playlists used in Windows, UTF-8 format, in a local hard disk. I only use m3u8 playlists in Foobar because m3u ones do not support non English characters, example Russian… Probably can be adapted to convert playlists used in Volumio in different network drives commenting the relevant lines.

Put your Windows playlists in the same folder where you are running the script from (example /data/INTERNAL). Be careful because it will overwrite already present Volumio playlists if they have the same name. Give the script a name.sh, make it executable with

chmod u+x scriptname.sh

and run it with ./scriptname.sh

#!/bin/bash

# Set your Windows PC music folder path
# Example, for "E:\Data\Music":
win_path='E:\\Data\\Music\\'

# Set your Volumio music folder path
# Example, for "USB/drive_label/Data/Music/":
vol_path='USB\/drive_label\/Data\/Music\/'

# Remove BOM
find . -type f \( -name "*.m3u8" \) -exec sed -i '1s/^\xEF\xBB\xBF//' {} +

# Convert Windows CRLF To Unix LF line endings
sed -i 's/\r$//' *.m3u8

# Remove (as for Foobar2000 playlists) first line (containing only #)
sed -i '1d' *.m3u8

# Replace Windows music folder with Volumio music folder path
sed -i "s%$win_path%$vol_path%g" *.m3u8

# Replace remaining \ with /
sed -i 's/\\/\//g' *.m3u8

# Create temporary file in RAM
TMPFILE=$(mktemp /dev/shm/win2vol.XXXXXX)

# Create another temporary file in RAM
TMPFILE2=$(mktemp /dev/shm/win2vol.2XXX)

# Loop files to append title to path/filename lines
for i in *.m3u8
do
	# Write file names in TMPFILE
	sed 's|.*/||' "$i" >> /dev/shm/win2vol.XXXXXX

	# Remove " - " from file names in TMPFILE
	sed -i 's/ - /\n/;s/.*\n//' /dev/shm/win2vol.XXXXXX

	# Append ","title":" to "$i" lines
	sed -i 's/$/","title":"/' "$i"

	# Append TMPFILE lines to "$i" lines saving result in TMPFILE2
	paste -d '' "$i" /dev/shm/win2vol.XXXXXX >> /dev/shm/win2vol.2XXX

	# Replace "$i" with win2vol.2XXX
	cp /dev/shm/win2vol.2XXX /data/INTERNAL/"$i"

	# Delete text in temporary files
	> /dev/shm/win2vol.XXXXXX
	> /dev/shm/win2vol.2XXX
done

# Remove temporary files
rm /dev/shm/win2vol.XXXXXX
rm /dev/shm/win2vol.2XXX

# Prepend {"service":"mpd","uri":"
sed -i 's/^/{"service":"mpd","uri":"/' *.m3u8

# Append ","artist":"artistn","album":"albumn","albumart":""} to each line
sed -i 's/$/","artist":"artistn","album":"albumn","albumart":""}/' *.m3u8

# From multi-lines to one-liner (adds a "," between lines)
sed -i ':a; N; $!ba; s/\n/,/g' *.m3u8

# Prepend "[" and append "]"
sed -i 's/^/[/; s/$/]/' *.m3u8

# Crop extension
for i in *.m3u8
do
    mv "$i" "${i%.m3u8}"
done

# Copy ready playlists to /data/playlist
cp -- !(*.* | . | ..) /data/playlist

Same as the first version, queue and play each playlist in Volumio, save the queue as playlist giving it the same name and all metadata and albumart will be there