xspf2m3u

simple XSPF to M3U conversion
git clone https://git.ce9e.org/xspf2m3u.git

commit
b5e22d8389061bdbb4365223ab6acac5aaaf7e25
parent
ef5493e4b00e83f89ac6a577d6e3b840f47ea00c
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2023-03-02 07:13
m3u2xspf: parse EXTM3U metadata

thanks to sosie-js

Diffstat

M m3u2xspf.py 31 ++++++++++++++++++++++++-------

1 files changed, 24 insertions, 7 deletions


diff --git a/m3u2xspf.py b/m3u2xspf.py

@@ -1,4 +1,5 @@
    1     1 import os
   -1     2 import re
    2     3 import sys
    3     4 import argparse
    4     5 from xml.sax.saxutils import escape
@@ -13,6 +14,7 @@ KEYS = {
   13    14     'album': 'album',
   14    15     'artist': 'creator',
   15    16     'title': 'title',
   -1    17     'duration': 'duration',
   16    18 }
   17    19 
   18    20 
@@ -30,19 +32,34 @@ def get_tags(path):
   30    32         return {'location': path}
   31    33 
   32    34 
   -1    35 def parse_extinf(line):
   -1    36     match = re.fullmatch(r'#EXTINF:([0-9]+),(.*)', line)
   -1    37     extinf = {}
   -1    38     if match:
   -1    39         extinf['title'] = match[2].strip()
   -1    40         if match[1] not in ['0', '-1']:
   -1    41             extinf['duration'] = str(int(match[1], 10) * 1000)
   -1    42     return extinf
   -1    43 
   -1    44 
   33    45 def iter_lines(path):
   34    46     root = os.path.dirname(path)
   -1    47     track = {}
   35    48     with open(sys.argv[1]) as fh:
   36    49         for line in fh:
   37    50             line = line.rstrip()
   38    51             if line.startswith('#'):
   39    -1                 pass
   40    -1             elif line.startswith('http'):
   41    -1                 yield {'location': line}
   42    -1             elif line:
   43    -1                 if not line.startswith('/'):
   44    -1                     line = os.path.join(root, line)
   45    -1                 yield get_tags(line)
   -1    52                 if line.startswith('#EXTINF:'):
   -1    53                     track = parse_extinf(line)
   -1    54             else:
   -1    55                 if line.startswith('http'):
   -1    56                     track['location'] = line
   -1    57                 elif line:
   -1    58                     if not line.startswith('/'):
   -1    59                         line = os.path.join(root, line)
   -1    60                     track.update(get_tags(line))
   -1    61                 yield track
   -1    62                 track = {}
   46    63 
   47    64 
   48    65 def parse_args():