• R/O
  • SSH

Commit

Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#objective-cqt誰得cocoawindowspythonrubyphpgameguibathyscaphec翻訳omegat計画中(planning stage)frameworktwittertestdombtronvb.netdirectxarduinopreviewerゲームエンジン

Commit MetaInfo

Revisão4d513a0808fae8d5da83d6f9cd80e307e800de77 (tree)
Hora2018-02-17 05:48:53
AutorJaime Marquínez Ferrándiz <jaime.marquinez.ferrandiz@fast...>
CommiterJaime Marquínez Ferrándiz

Mensagem de Log

Add support for converting to other formats

Mudança Sumário

Diff

diff -r 0c8bf157617a -r 4d513a0808fa beetsplug/playersync.py
--- a/beetsplug/playersync.py Sun Jan 14 23:05:47 2018 +0100
+++ b/beetsplug/playersync.py Fri Feb 16 21:48:53 2018 +0100
@@ -6,12 +6,14 @@
66 import sqlite3
77 import shutil
88 import datetime
9+import subprocess
10+import tempfile
911
1012 IS_PY3 = sys.version_info[0] >= 3
1113
1214 from beets.plugins import BeetsPlugin
1315 from beets.ui import Subcommand
14-from beets.util import prune_dirs, bytestring_path
16+from beets.util import prune_dirs, bytestring_path, command_output
1517 from beets.dbcore import OrQuery
1618 from beets.library import parse_query_parts, Item
1719
@@ -26,6 +28,18 @@
2628 PRIMARY KEY (idPlaylist, idSong, listIndex));
2729 '''
2830
31+def _ext_from_format(format):
32+ if format == 'aac':
33+ return 'm4a'
34+ else:
35+ return format
36+
37+def _format_from_ext(ext):
38+ if ext == 'm4a':
39+ return 'aac'
40+ else:
41+ return ext
42+
2943 class PlayerSync(BeetsPlugin):
3044 def info(self, *args, **kwargs):
3145 return self._log.info(*args, **kwargs)
@@ -67,7 +81,7 @@
6781 self.info('Processing query: "{0}"', query)
6882 items = lib.items(query)
6983 for item in items:
70- self.add_item(lib, db, item)
84+ self.add_item(lib, db, item, config)
7185 db.execute('INSERT INTO query (query) VALUES (?)', (query,))
7286 query_id = db.execute('SELECT idQuery FROM query WHERE query=?', (query,)).fetchone()[0]
7387 db.executemany('INSERT INTO querysong VALUES (?, ?)', [(query_id, item['id']) for item in items])
@@ -87,7 +101,7 @@
87101 song_path = os.path.join(lib_directory, song_path)
88102 query, q_sort = parse_query_parts(['path:' + song_path], Item)
89103 item = list(lib.items(OrQuery(query)))[0]
90- self.add_item(lib, db, item)
104+ self.add_item(lib, db, item, config)
91105 items.append(item)
92106 db.execute(
93107 'INSERT INTO playlist (playlist) VALUES (?)',
@@ -111,10 +125,27 @@
111125 self.info('"{0}" does not exist', item['path'])
112126 continue
113127 with db:
114- self.info('Copying "{0}"', item)
115128 if not os.path.exists(os.path.dirname(copy_path)):
116129 os.makedirs(os.path.dirname(copy_path))
117- shutil.copyfile(item['path'], copy_path)
130+ new_ext = os.path.splitext(copy_path)[1].lower()
131+ old_ext = os.path.splitext(item['path'])[1].lower()
132+ if IS_PY3 and isinstance(old_ext, bytes):
133+ old_ext = old_ext.decode()
134+ if new_ext == old_ext:
135+ self.info('Copying "{0}"', item)
136+ shutil.copyfile(item['path'], copy_path)
137+ else:
138+ fmt = _format_from_ext(new_ext.lstrip('.'))
139+ self.info('Converting to {0}: {1}', fmt, item)
140+ args = sys.argv[:sys.argv.index('sync')]
141+ temp_dir = tempfile.mkdtemp()
142+ try:
143+ args.extend(['convert', '--yes', '--format', fmt, '--dest', temp_dir, 'id:' + str(item_id)])
144+ out = subprocess.check_call(args)
145+ temp_path = os.path.join(temp_dir, song_path)
146+ shutil.copyfile(temp_path, copy_path)
147+ finally:
148+ shutil.rmtree(temp_dir)
118149 db.execute('UPDATE song SET synced=1,date=? WHERE idSong=?', (datetime.datetime.now(), item_id))
119150
120151 for item_id, song_path in db.execute('''
@@ -144,13 +175,16 @@
144175
145176 self.info('Finished syncing {0}', config['path'])
146177
147- def add_item(self, lib, db, item):
178+ def add_item(self, lib, db, item, config):
148179 item_id = item['id']
149180 fullpath = item['path']
150181 copy_path = os.path.relpath(fullpath, lib.directory)
151182 if IS_PY3 and isinstance(copy_path, bytes):
152183 copy_path = copy_path.decode()
153184 if not db.execute('SELECT * FROM song WHERE idSong=?', (item_id,)).fetchall():
185+ allowed_formats = [f.lower() for f in config['formats'].get()]
186+ if item['format'].lower() not in allowed_formats:
187+ copy_path = os.path.splitext(copy_path)[0] + '.' + _ext_from_format(allowed_formats[0])
154188 db.execute('INSERT INTO song (idSong, filename) VALUES (?,?)', (item_id, copy_path))
155189 else:
156190 # check if the file is outdated