From 64617c4d8ccf7a27d61491f384c480539b966ddd Mon Sep 17 00:00:00 2001 From: NicoHood Date: Tue, 5 Apr 2016 20:55:44 +0200 Subject: [PATCH 01/11] Added script description --- shuffle.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/shuffle.py b/shuffle.py index 274f0e0..d2f94f3 100755 --- a/shuffle.py +++ b/shuffle.py @@ -606,7 +606,9 @@ def handle_interrupt(signal, frame): if __name__ == '__main__': signal.signal(signal.SIGINT, handle_interrupt) - parser = argparse.ArgumentParser() + parser = argparse.ArgumentParser(description= + 'Python script for building the Track and Playlist database ' + 'for the newer gen IPod Shuffle.') parser.add_argument('--disable-voiceover', action='store_true', help='Disable voiceover feature') parser.add_argument('--rename-unicode', action='store_true', help='Rename files causing unicode errors, will do minimal required renaming') parser.add_argument('--track-gain', type=nonnegative_int, default=0, help='Specify volume gain (0-99) for all tracks; 0 (default) means no gain and is usually fine; e.g. 60 is very loud even on minimal player volume') From df97b876b83808ea8aa4ce2ea5bd78f6f556ca0e Mon Sep 17 00:00:00 2001 From: NicoHood Date: Tue, 5 Apr 2016 20:57:34 +0200 Subject: [PATCH 02/11] Made argument parser functions better readable in script --- shuffle.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/shuffle.py b/shuffle.py index d2f94f3..e02a879 100755 --- a/shuffle.py +++ b/shuffle.py @@ -609,9 +609,14 @@ if __name__ == '__main__': parser = argparse.ArgumentParser(description= 'Python script for building the Track and Playlist database ' 'for the newer gen IPod Shuffle.') - parser.add_argument('--disable-voiceover', action='store_true', help='Disable voiceover feature') - parser.add_argument('--rename-unicode', action='store_true', help='Rename files causing unicode errors, will do minimal required renaming') - parser.add_argument('--track-gain', type=nonnegative_int, default=0, help='Specify volume gain (0-99) for all tracks; 0 (default) means no gain and is usually fine; e.g. 60 is very loud even on minimal player volume') + parser.add_argument('--disable-voiceover', action='store_true', + help='Disable voiceover feature') + parser.add_argument('--rename-unicode', action='store_true', + help='Rename files causing unicode errors, will do minimal required renaming') + parser.add_argument('--track-gain', type=nonnegative_int, default='0', + help='Specify volume gain (0-99) for all tracks; ' + '0 (default) means no gain and is usually fine; ' + 'e.g. 60 is very loud even on minimal player volume') parser.add_argument('path', help='Path to the IPod\'s root directory') result = parser.parse_args() From bcc374df130f2830c0fc04138f7a0d4b17116675 Mon Sep 17 00:00:00 2001 From: NicoHood Date: Tue, 5 Apr 2016 20:58:35 +0200 Subject: [PATCH 03/11] Added Auto Playlists --- shuffle.py | 74 +++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 62 insertions(+), 12 deletions(-) diff --git a/shuffle.py b/shuffle.py index e02a879..6d5c57b 100755 --- a/shuffle.py +++ b/shuffle.py @@ -450,6 +450,33 @@ class Playlist(Record): listtracks = [ x for (_, x) in sorted(sorttracks) ] return listtracks + def populate_directory(self, playlistpath, recursive = True): + # Add all tracks inside the folder and its subfolders recursively. + # Folders containing no music and only a single Album + # would generate duplicated playlists. That is intended and "wont fix". + # Empty folders (inside the music path) will generate an error -> "wont fix". + listtracks = [] + for (dirpath, dirnames, filenames) in os.walk(playlistpath): + dirnames.sort() + + for filename in sorted(filenames, key = lambda x: x.lower()): + # Only add valid music files to playlist + if os.path.splitext(filename)[1].lower() in (".mp3", ".m4a", ".m4b", ".m4p", ".aa", ".wav"): + # Reformat fullPath so that the basepath is lower/upper and the rest lower. + # This is required to get the correct position (track index) inside Playlist.construct() + # /media/username/USER'S IPOD/IPod_Control/Music/Artist/Album/Track.mp3 + fullPath = os.path.abspath(os.path.join(dirpath, filename)) + # /media/username/USER'S IPOD/ + basepath = self.base + # ipod_control/music/artist/album/track.mp3 + ipodpath = self.path_to_ipod(fullPath)[1:].lower() + # /media/username/USER'S IPOD/ipod_control/music/artist/album/track.mp3 + fullPath = os.path.abspath(os.path.join(basepath, ipodpath)) + listtracks.append(fullPath) + if not recursive: + break + return listtracks + def remove_relatives(self, relative, filename): base = os.path.dirname(os.path.abspath(filename)) if not os.path.exists(relative): @@ -461,17 +488,26 @@ class Playlist(Record): return fullPath def populate(self, filename): - with open(filename, 'rb') as f: - data = f.readlines() + # Create a playlist of the folder and all subfolders + if os.path.isdir(filename): + self.listtracks = self.populate_directory(filename) - extension = os.path.splitext(filename)[1].lower() - if extension == '.pls': - self.listtracks = self.populate_pls(data) - elif extension == '.m3u': - self.listtracks = self.populate_m3u(data) - # Ensure all paths are not relative to the playlist file - for i in range(len(self.listtracks)): - self.listtracks[i] = self.remove_relatives(self.listtracks[i], filename) + # Read the playlist file + else: + with open(filename, 'rb') as f: + data = f.readlines() + + extension = os.path.splitext(filename)[1].lower() + if extension == '.pls': + self.listtracks = self.populate_pls(data) + elif extension == '.m3u': + self.listtracks = self.populate_m3u(data) + else: + raise + + # Ensure all paths are not relative to the playlist file + for i in range(len(self.listtracks)): + self.listtracks[i] = self.remove_relatives(self.listtracks[i], filename) # Handle the VoiceOverData text = os.path.splitext(os.path.basename(filename))[0] @@ -502,7 +538,7 @@ class Playlist(Record): return output + chunks class Shuffler(object): - def __init__(self, path, voiceover=True, rename=False, trackgain=0): + def __init__(self, path, voiceover=True, rename=False, trackgain=0, auto_playlists=None): self.path, self.base = self.determine_base(path) self.tracks = [] self.albums = [] @@ -512,6 +548,7 @@ class Shuffler(object): self.voiceover = voiceover self.rename = rename self.trackgain = trackgain + self.auto_playlists = auto_playlists def initialize(self): # remove existing voiceover files (they are either useless or will be overwritten anyway) @@ -548,6 +585,15 @@ class Shuffler(object): if os.path.splitext(filename)[1].lower() in (".pls", ".m3u"): self.lists.append(os.path.abspath(os.path.join(dirpath, filename))) + # Create automatic playlists in music directory. + # Ignore the (music) root and any hidden directories. + if self.auto_playlists and "ipod_control/music/" in dirpath.lower() and "/." not in dirpath.lower(): + # Only go to a specific depth. -1 is unlimted, 0 is ignored as there is already a master playlist. + depth = dirpath[len(self.path) + len(os.path.sep):].count(os.path.sep) - 1 + if self.auto_playlists < 0 or depth <= self.auto_playlists: + print "Adding folder", depth, " ", dirpath + self.lists.append(os.path.abspath(dirpath)) + def write_database(self): with open(os.path.join(self.base, "iPod_Control", "iTunes", "iTunesSD"), "wb") as f: f.write(self.tunessd.construct()) @@ -617,6 +663,10 @@ if __name__ == '__main__': help='Specify volume gain (0-99) for all tracks; ' '0 (default) means no gain and is usually fine; ' 'e.g. 60 is very loud even on minimal player volume') + parser.add_argument('--auto-playlists', type=int, default=None, const=-1, nargs='?', + help='Generate automatic playlists for each folder recursively inside ' + '"IPod_Control/Music/". You can optionally limit the depth: ' + '0=root, 1=artist, 2=album, n=subfoldername, default=-1 (No Limit).') parser.add_argument('path', help='Path to the IPod\'s root directory') result = parser.parse_args() @@ -629,7 +679,7 @@ if __name__ == '__main__': print "Error: Did not find any voiceover program. Voiceover disabled." result.disable_voiceover = True - shuffle = Shuffler(result.path, voiceover=not result.disable_voiceover, rename=result.rename_unicode, trackgain=result.track_gain) + shuffle = Shuffler(result.path, voiceover=not result.disable_voiceover, rename=result.rename_unicode, trackgain=result.track_gain, auto_playlists=result.auto_playlists) shuffle.initialize() shuffle.populate() shuffle.write_database() From 6e919eca3db7e4fee341425333bd3ff22747832b Mon Sep 17 00:00:00 2001 From: NicoHood Date: Tue, 5 Apr 2016 20:58:52 +0200 Subject: [PATCH 04/11] Minor typo --- shuffle.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shuffle.py b/shuffle.py index 6d5c57b..4e95d43 100755 --- a/shuffle.py +++ b/shuffle.py @@ -579,7 +579,7 @@ class Shuffler(object): for filename in sorted(filenames, key = lambda x: x.lower()): fullPath = os.path.abspath(os.path.join(dirpath, filename)) relPath = fullPath[fullPath.index(self.path)+len(self.path)+1:].lower() - fullPath = os.path.abspath(os.path.join(self.path, relPath)); + fullPath = os.path.abspath(os.path.join(self.path, relPath)) if os.path.splitext(filename)[1].lower() in (".mp3", ".m4a", ".m4b", ".m4p", ".aa", ".wav"): self.tracks.append(fullPath) if os.path.splitext(filename)[1].lower() in (".pls", ".m3u"): From 8dff7e8d5e15521e41a02b91d346f81e2b30441d Mon Sep 17 00:00:00 2001 From: NicoHood Date: Tue, 5 Apr 2016 22:03:10 +0200 Subject: [PATCH 05/11] Skip hidden directories for auto playlists --- shuffle.py | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/shuffle.py b/shuffle.py index 4e95d43..acb347c 100755 --- a/shuffle.py +++ b/shuffle.py @@ -459,20 +459,22 @@ class Playlist(Record): for (dirpath, dirnames, filenames) in os.walk(playlistpath): dirnames.sort() - for filename in sorted(filenames, key = lambda x: x.lower()): - # Only add valid music files to playlist - if os.path.splitext(filename)[1].lower() in (".mp3", ".m4a", ".m4b", ".m4p", ".aa", ".wav"): - # Reformat fullPath so that the basepath is lower/upper and the rest lower. - # This is required to get the correct position (track index) inside Playlist.construct() - # /media/username/USER'S IPOD/IPod_Control/Music/Artist/Album/Track.mp3 - fullPath = os.path.abspath(os.path.join(dirpath, filename)) - # /media/username/USER'S IPOD/ - basepath = self.base - # ipod_control/music/artist/album/track.mp3 - ipodpath = self.path_to_ipod(fullPath)[1:].lower() - # /media/username/USER'S IPOD/ipod_control/music/artist/album/track.mp3 - fullPath = os.path.abspath(os.path.join(basepath, ipodpath)) - listtracks.append(fullPath) + # Ignore any hidden directories + if "/." not in dirpath.lower(): + for filename in sorted(filenames, key = lambda x: x.lower()): + # Only add valid music files to playlist + if os.path.splitext(filename)[1].lower() in (".mp3", ".m4a", ".m4b", ".m4p", ".aa", ".wav"): + # Reformat fullPath so that the basepath is lower/upper and the rest lower. + # This is required to get the correct position (track index) inside Playlist.construct() + # /media/username/USER'S IPOD/IPod_Control/Music/Artist/Album/Track.mp3 + fullPath = os.path.abspath(os.path.join(dirpath, filename)) + # /media/username/USER'S IPOD/ + basepath = self.base + # ipod_control/music/artist/album/track.mp3 + ipodpath = self.path_to_ipod(fullPath)[1:].lower() + # /media/username/USER'S IPOD/ipod_control/music/artist/album/track.mp3 + fullPath = os.path.abspath(os.path.join(basepath, ipodpath)) + listtracks.append(fullPath) if not recursive: break return listtracks From d71be4f9fb46c3290fc4879ee4313b14e4cc02cf Mon Sep 17 00:00:00 2001 From: NicoHood Date: Tue, 5 Apr 2016 22:06:53 +0200 Subject: [PATCH 06/11] Removed debug output --- shuffle.py | 1 - 1 file changed, 1 deletion(-) diff --git a/shuffle.py b/shuffle.py index acb347c..c4cff6c 100755 --- a/shuffle.py +++ b/shuffle.py @@ -593,7 +593,6 @@ class Shuffler(object): # Only go to a specific depth. -1 is unlimted, 0 is ignored as there is already a master playlist. depth = dirpath[len(self.path) + len(os.path.sep):].count(os.path.sep) - 1 if self.auto_playlists < 0 or depth <= self.auto_playlists: - print "Adding folder", depth, " ", dirpath self.lists.append(os.path.abspath(dirpath)) def write_database(self): From 5b2a4a2a3637aed2ec0bc4ed58e10345ae968951 Mon Sep 17 00:00:00 2001 From: NicoHood Date: Tue, 5 Apr 2016 23:11:48 +0200 Subject: [PATCH 07/11] Fix hyphen in filename #4 --- shuffle.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/shuffle.py b/shuffle.py index c4cff6c..502cbc6 100755 --- a/shuffle.py +++ b/shuffle.py @@ -327,7 +327,11 @@ class Track(Record): self["filetype"] = 2 text = os.path.splitext(os.path.basename(filename))[0] - audio = mutagen.File(filename, easy = True) + audio = None + try: + audio = mutagen.File(filename, easy = True) + except: + print "Error calling mutagen. Possible invalid filename/ID3Tags (hyphen in filename?)" if audio: # Note: Rythmbox IPod plugin sets this value always 0. self["stop_at_pos_ms"] = int(audio.info.length * 1000) From 4134e93cd3c56680a893140bcd24898781bfb467 Mon Sep 17 00:00:00 2001 From: NicoHood Date: Wed, 6 Apr 2016 18:10:24 +0200 Subject: [PATCH 08/11] Removed lower case from script (fix issue #5) --- shuffle.py | 22 ++++------------------ 1 file changed, 4 insertions(+), 18 deletions(-) diff --git a/shuffle.py b/shuffle.py index 502cbc6..809a26d 100755 --- a/shuffle.py +++ b/shuffle.py @@ -464,20 +464,11 @@ class Playlist(Record): dirnames.sort() # Ignore any hidden directories - if "/." not in dirpath.lower(): + if "/." not in dirpath: for filename in sorted(filenames, key = lambda x: x.lower()): # Only add valid music files to playlist if os.path.splitext(filename)[1].lower() in (".mp3", ".m4a", ".m4b", ".m4p", ".aa", ".wav"): - # Reformat fullPath so that the basepath is lower/upper and the rest lower. - # This is required to get the correct position (track index) inside Playlist.construct() - # /media/username/USER'S IPOD/IPod_Control/Music/Artist/Album/Track.mp3 fullPath = os.path.abspath(os.path.join(dirpath, filename)) - # /media/username/USER'S IPOD/ - basepath = self.base - # ipod_control/music/artist/album/track.mp3 - ipodpath = self.path_to_ipod(fullPath)[1:].lower() - # /media/username/USER'S IPOD/ipod_control/music/artist/album/track.mp3 - fullPath = os.path.abspath(os.path.join(basepath, ipodpath)) listtracks.append(fullPath) if not recursive: break @@ -488,9 +479,6 @@ class Playlist(Record): if not os.path.exists(relative): relative = os.path.join(base, relative) fullPath = relative - ipodpath = self.parent.parent.parent.path - relPath = fullPath[fullPath.index(ipodpath)+len(ipodpath)+1:].lower() - fullPath = os.path.abspath(os.path.join(ipodpath, relPath)) return fullPath def populate(self, filename): @@ -581,19 +569,17 @@ class Shuffler(object): for (dirpath, dirnames, filenames) in os.walk(self.path): dirnames.sort() # Ignore the speakable directory and any hidden directories - if "ipod_control/speakable" not in dirpath.lower() and "/." not in dirpath.lower(): + if "iPod_Control/Speakable" not in dirpath and "/." not in dirpath: for filename in sorted(filenames, key = lambda x: x.lower()): fullPath = os.path.abspath(os.path.join(dirpath, filename)) - relPath = fullPath[fullPath.index(self.path)+len(self.path)+1:].lower() - fullPath = os.path.abspath(os.path.join(self.path, relPath)) if os.path.splitext(filename)[1].lower() in (".mp3", ".m4a", ".m4b", ".m4p", ".aa", ".wav"): self.tracks.append(fullPath) if os.path.splitext(filename)[1].lower() in (".pls", ".m3u"): - self.lists.append(os.path.abspath(os.path.join(dirpath, filename))) + self.lists.append(fullPath) # Create automatic playlists in music directory. # Ignore the (music) root and any hidden directories. - if self.auto_playlists and "ipod_control/music/" in dirpath.lower() and "/." not in dirpath.lower(): + if self.auto_playlists and "iPod_Control/Music/" in dirpath and "/." not in dirpath: # Only go to a specific depth. -1 is unlimted, 0 is ignored as there is already a master playlist. depth = dirpath[len(self.path) + len(os.path.sep):].count(os.path.sep) - 1 if self.auto_playlists < 0 or depth <= self.auto_playlists: From 96a0d35dc86e5eb57d632f04624296135e486fb6 Mon Sep 17 00:00:00 2001 From: NicoHood Date: Wed, 6 Apr 2016 22:02:54 +0200 Subject: [PATCH 09/11] Use switch to enable voiceover --- shuffle.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/shuffle.py b/shuffle.py index 809a26d..e5b4db5 100755 --- a/shuffle.py +++ b/shuffle.py @@ -646,8 +646,8 @@ if __name__ == '__main__': parser = argparse.ArgumentParser(description= 'Python script for building the Track and Playlist database ' 'for the newer gen IPod Shuffle.') - parser.add_argument('--disable-voiceover', action='store_true', - help='Disable voiceover feature') + parser.add_argument('--voiceover', action='store_true', + help='Enable voiceover feature') parser.add_argument('--rename-unicode', action='store_true', help='Rename files causing unicode errors, will do minimal required renaming') parser.add_argument('--track-gain', type=nonnegative_int, default='0', @@ -666,11 +666,11 @@ if __name__ == '__main__': if result.rename_unicode: check_unicode(result.path) - if not result.disable_voiceover and not Text2Speech.check_support(): + if result.voiceover and not Text2Speech.check_support(): print "Error: Did not find any voiceover program. Voiceover disabled." - result.disable_voiceover = True + result.voiceover = False - shuffle = Shuffler(result.path, voiceover=not result.disable_voiceover, rename=result.rename_unicode, trackgain=result.track_gain, auto_playlists=result.auto_playlists) + shuffle = Shuffler(result.path, voiceover=result.voiceover, rename=result.rename_unicode, trackgain=result.track_gain, auto_playlists=result.auto_playlists) shuffle.initialize() shuffle.populate() shuffle.write_database() From 7129c05e99a48bd0dfbd4cb6cc6a5b436097f13b Mon Sep 17 00:00:00 2001 From: NicoHood Date: Wed, 6 Apr 2016 22:03:18 +0200 Subject: [PATCH 10/11] Add version number to description --- shuffle.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shuffle.py b/shuffle.py index e5b4db5..9fd7f92 100755 --- a/shuffle.py +++ b/shuffle.py @@ -645,7 +645,7 @@ if __name__ == '__main__': signal.signal(signal.SIGINT, handle_interrupt) parser = argparse.ArgumentParser(description= 'Python script for building the Track and Playlist database ' - 'for the newer gen IPod Shuffle.') + 'for the newer gen IPod Shuffle. Version 1.3') parser.add_argument('--voiceover', action='store_true', help='Enable voiceover feature') parser.add_argument('--rename-unicode', action='store_true', From a1cebe9d0beaab17c025601b47c3f0c4d4d35711 Mon Sep 17 00:00:00 2001 From: NicoHood Date: Wed, 6 Apr 2016 22:08:28 +0200 Subject: [PATCH 11/11] Differentiate track and playlist voiceover --- shuffle.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/shuffle.py b/shuffle.py index 9fd7f92..4880802 100755 --- a/shuffle.py +++ b/shuffle.py @@ -157,6 +157,7 @@ class Record(object): self._struct = collections.OrderedDict([]) self._fields = {} self.voiceover = parent.voiceover + self.playlist_voiceover = parent.playlist_voiceover self.rename = parent.rename self.trackgain = parent.trackgain @@ -178,7 +179,7 @@ class Record(object): return output def text_to_speech(self, text, dbid, playlist = False): - if self.voiceover: + if self.voiceover and not playlist or self.playlist_voiceover and playlist: # Create the voiceover wav file fn = "".join(["{0:02X}".format(ord(x)) for x in reversed(dbid)]) path = os.path.join(self.base, "iPod_Control", "Speakable", "Tracks" if not playlist else "Playlists", fn + ".wav") @@ -423,7 +424,7 @@ class Playlist(Record): def set_master(self, tracks): # By default use "All Songs" builtin voiceover (dbid all zero) # Else generate alternative "All Songs" to fit the speaker voice of other playlists - if self.voiceover and (Text2Speech.valid_tts['pico2wave'] or Text2Speech.valid_tts['espeak']): + if self.playlist_voiceover and (Text2Speech.valid_tts['pico2wave'] or Text2Speech.valid_tts['espeak']): self["dbid"] = hashlib.md5("masterlist").digest()[:8] #pylint: disable-msg=E1101 self.text_to_speech("All songs", self["dbid"], True) self["listtype"] = 1 @@ -532,7 +533,7 @@ class Playlist(Record): return output + chunks class Shuffler(object): - def __init__(self, path, voiceover=True, rename=False, trackgain=0, auto_playlists=None): + def __init__(self, path, voiceover=False, playlist_voiceover=False, rename=False, trackgain=0, auto_playlists=None): self.path, self.base = self.determine_base(path) self.tracks = [] self.albums = [] @@ -540,6 +541,7 @@ class Shuffler(object): self.lists = [] self.tunessd = None self.voiceover = voiceover + self.playlist_voiceover = playlist_voiceover self.rename = rename self.trackgain = trackgain self.auto_playlists = auto_playlists @@ -647,7 +649,9 @@ if __name__ == '__main__': 'Python script for building the Track and Playlist database ' 'for the newer gen IPod Shuffle. Version 1.3') parser.add_argument('--voiceover', action='store_true', - help='Enable voiceover feature') + help='Enable track voiceover feature') + parser.add_argument('--playlist-voiceover', action='store_true', + help='Enable playlist voiceover feature') parser.add_argument('--rename-unicode', action='store_true', help='Rename files causing unicode errors, will do minimal required renaming') parser.add_argument('--track-gain', type=nonnegative_int, default='0', @@ -670,7 +674,7 @@ if __name__ == '__main__': print "Error: Did not find any voiceover program. Voiceover disabled." result.voiceover = False - shuffle = Shuffler(result.path, voiceover=result.voiceover, rename=result.rename_unicode, trackgain=result.track_gain, auto_playlists=result.auto_playlists) + shuffle = Shuffler(result.path, voiceover=result.voiceover, playlist_voiceover=result.playlist_voiceover, rename=result.rename_unicode, trackgain=result.track_gain, auto_playlists=result.auto_playlists) shuffle.initialize() shuffle.populate() shuffle.write_database()