1

I have .m3u files generated in Linux which can't be read by Windows programs due to different way to treat special characters in paths.

For example, how can I convert this Linux path:

Music/Timo%20Rautiainen%20&%20Trio%20Niskalaukaus/07%20H%C3%A4mmennys%20ja%20viha.mp3

into this path readable in Windows:

Music/Timo Rautiainen & Trio Niskalaukaus/07 Hämmennys ja viha.mp3

in a text file?

It would be easy if I could just replace all instances of %20 with spaces, but as you can see above there are special characters (äöñ etc.) in the paths and filenames which need to be converted, also. These are too varied to be done with regex, and I don't even have a list of all special characters in use in my files.

edit: as mentioned in comments, the above is a result of URL/URI encoding, rather than reasons I postulated.

Easy fix was to use an online URL encoder/decoder, which fixed the paths very quickly. This is the URL decoder I used. Accepted answer below with a native Linux Python method.

bramford
  • 83
  • 6
  • That's not a Linux path. That's a URI-encoded path. Look for URI decoders. – muru Feb 07 '15 at 15:08
  • I did, but googling came up with PERL scripts and other methods which are way too complicated for me. I found a workaround by importing the playlist to Exaile media player, and exporting it from Exaile to another m3u playlist - Exaile exports them without the encoding and are readable in Windows. – bramford Feb 07 '15 at 17:42
  • Turns out Exaile also wouldn't correctly export some paths, so I ended up using a URL-encoder (not URI-encoder, not sure if there's a difference). OP updated. Thank you, muruliini! – bramford Feb 08 '15 at 19:53
  • 1
    URLs are mostly a form of URIs (see http://stackoverflow.com/q/176264/2072269). Good to know you got a workaround. – muru Feb 08 '15 at 20:00

1 Answers1

3

If you don't like Perl, use Python! ;-] Seriously, this or an equivalent Perl one-liner is the easiest way, if you can't use a different application to generate M3U lists.

from sys import stdin
from urllib.parse import unquote

print(*map(unquote, stdin), sep='', end='')

Run with:

python3 [script file] < [quoted m3u file] > [unquoted m3u8 file]

Note that the .m3u8 extension gives the reading application a hint, that the file is UTF-8 encoded. On Linux that may not matter, because most of the time it's the default encoding anyway, but many Windows applications might assume windows-1250 encoding or whatever your locale is.

David Foerster
  • 35,754
  • 55
  • 92
  • 145
  • `from future`? Why not use Python3 instead? O.o – muru Feb 08 '15 at 20:57
  • OP is on Ubuntu. OP has Python 3. – muru Feb 08 '15 at 20:58
  • I'm on Linux Mint, but probably doesn't make a difference in this case. Thank you for the "proper" solution which doesn't require a website! – bramford Feb 10 '15 at 00:43
  • Not really. Afaik there's no modern desktop Linux distribution with a different character encoding than UTF-8 – at least those that don't target audiences with Chinese alphabets. – David Foerster Feb 10 '15 at 00:45