from sqlalchemy import MetaData, Column, Unicode
+import json
+
MIGRATIONS = {}
values(name='webm_video'))
db.commit()
+
+
+@RegisterMigration(3, MIGRATIONS)
+def change_metadata_format(db):
+ """Change orig_metadata format for multi-stream a-v"""
+ db_metadata = MetaData(bind=db.bind)
+
+ vid_data = inspect_table(db_metadata, "video__mediadata")
+
+ for row in db.execute(vid_data.select()):
+ if not row.orig_metadata:
+ continue
+
+ metadata = json.loads(row.orig_metadata)
+
+ # before this migration there was info about only one video or audio
+ # stream. So, we store existing info as the first item in the list
+ new_metadata = {'audio': [], 'video': [], 'common': {}}
+ video_key_map = { # old: new
+ 'videoheight': 'height',
+ 'videowidth': 'width',
+ 'videorate': 'rate',
+ }
+ audio_key_map = { # old: new
+ 'audiochannels': 'channels',
+ }
+ common_key_map = {
+ 'videolength': 'length',
+ }
+
+ new_metadata['video'] = [dict((v, metadata.get(k))
+ for k, v in video_key_map.items() if metadata.get(k))]
+ new_metadata['audio'] = [dict((v, metadata.get(k))
+ for k, v in audio_key_map.items() if metadata.get(k))]
+ new_metadata['common'] = dict((v, metadata.get(k))
+ for k, v in common_key_map.items() if metadata.get(k))
+
+ # 'mimetype' should be in tags
+ new_metadata['common']['tags'] = {'mimetype': metadata.get('mimetype')}
+ if 'tags' in metadata:
+ new_metadata['video'][0]['tags'] = {}
+ new_metadata['audio'][0]['tags'] = {}
+
+ tags = metadata['tags']
+
+ video_keys = ['encoder', 'encoder-version', 'video-codec']
+ audio_keys = ['audio-codec']
+
+ for t, v in tags.items():
+ if t in video_keys:
+ new_metadata['video'][0]['tags'][t] = tags[t]
+ elif t in audio_keys:
+ new_metadata['audio'][0]['tags'][t] = tags[t]
+ else:
+ new_metadata['common']['tags'][t] = tags[t]
+ db.execute(vid_data.update()
+ .where(vid_data.c.media_entry==row.media_entry)
+ .values(orig_metadata=json.dumps(new_metadata)))
+ db.commit()