+def remux(video, xml=None):
+ if 'genre' in video:
+ if not os.path.exists(video['genre']):
+ os.mkdir(video['genre'])
+ video['path'] = Path(video['genre'] / video['filename']).with_suffix('.mkv')
+ else:
+ video['path'] = video['filename'].with_suffix('.mkv')
+ command = ["mkvmerge", "-o", str(video['path']), '--title', video['title']]
+
+ if xml:
+ with video['filename'].with_suffix('.xml').open('w') as f:
+ f.write(xml)
+ command.extend(['--global-tags', str(video['filename'].with_suffix('.xml'))])
+ if 'thumb' in video:
+ with open('thumbnail.jpg', 'wb') as f: # FIXME use title instead for many downloaders
+ f.write(video['thumb'].read())
+ command.extend(['--attachment-description', "Thumbnail",
+ '--attachment-mime-type', 'image/jpeg',
+ '--attach-file', 'thumbnail.jpg'])
+ # if 'subs' in video:
+ # for sub in video['subs']:
+ # if 'download' in sub:
+ # with open("{}.vtt".format(sub['lang']),'wb') as f:
+ # f.write(bytes("".join(sub['download']),'utf-8')) #FIXME
+ # command.extend(['--language 0:{} {}.vtt'.format(sub['lang'],sub['lang'])])
+
+ command.append(str(video['filename']))
+ print(Popen(command, stdout=PIPE).communicate()[0])
+ for fname in (video['filename'], video['filename'].with_suffix('.xml'), Path('thumbnail.jpg')):
+ try:
+ fname.unlink()
+ except:
+ pass
+ if 'timestamp' in video:
+ try:
+ os.utime(str(video['path']), times=(video['timestamp'].timestamp(), video['timestamp'].timestamp()))
+ except FileNotFoundError as e:
+ print(e)
+
+
+def mkv_metadata(video):
+ root = BeautifulSoup(features='xml')
+ root.append(Doctype('Tags SYSTEM "matroskatags.dtd"'))
+ tags = root.new_tag("Tags")
+ tag = root.new_tag("Tag")
+ tags.append(tag)
+ root.append(tags)
+ keep = ('title', 'description', 'url', 'genre')
+ targets = root.new_tag("Targets")
+ ttv = root.new_tag("TargetTypeValue")
+ ttv.string = str(50)
+ targets.append(ttv)
+ tag.append(targets)
+ for key in video:
+ if not key in keep:
+ continue
+ simple = root.new_tag('Simple')
+ name = root.new_tag('Name')
+ name.string = key.upper()
+ simple.append(name)
+ sstring = root.new_tag('String')
+ sstring.string = video[key]
+ simple.append(sstring)
+ tag.append(simple)
+ return str(root)
+
+