-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add video track and attachment extraction #45
base: release/1.1
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
|
||
import json | ||
from os import devnull | ||
from os.path import expanduser, isfile | ||
from os.path import expanduser, isfile, join, basename, splitext | ||
import subprocess as sp | ||
|
||
import bitmath | ||
|
@@ -38,6 +38,7 @@ def __init__(self, file_path=None, title=None): | |
be used if it exists. | ||
""" | ||
self.mkvmerge_path = 'mkvmerge' | ||
self.mkvextract_path = 'mkvextract' | ||
self.title = title | ||
self._chapters_file = None | ||
self._chapter_language = None | ||
|
@@ -69,6 +70,19 @@ def __init__(self, file_path=None, title=None): | |
new_track.forced_track = track['properties']['forced_track'] | ||
self.add_track(new_track) | ||
|
||
# add attachment with info | ||
for attachment in info_json['attachments']: | ||
new_attachment = MKVAttachment(file_path, | ||
name=attachment['file_name'], | ||
description=attachment['description']) | ||
if 'id' in attachment: | ||
new_attachment.id = attachment['id'] | ||
if 'size' in attachment: | ||
new_attachment.size = attachment['size'] | ||
if 'properties' in attachment and 'uid' in attachment['properties']: | ||
new_attachment.uid = attachment['properties']['uid'] | ||
self.add_attachment(new_attachment) | ||
|
||
# split options | ||
self._split_options = [] | ||
|
||
|
@@ -663,3 +677,34 @@ def flatten(item): | |
return flat_list | ||
else: | ||
return [item] | ||
|
||
def extract_video_tracks(self, out_folder=''): | ||
"""Extract video tracks.""" | ||
names = [] | ||
name_args = [] | ||
for track in self.tracks: | ||
if track._track_type == 'video': | ||
bname = splitext(basename(track.file_path))[0] | ||
name = '{}.mp4'.format(join(out_folder, bname + "_" + track.track_name)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Three comments for this line:
|
||
name_args.append('{}:{}'.format(track._track_id, name)) | ||
names.append(name) | ||
file_path = track._file_path | ||
|
||
sp.run([self.mkvextract_path, 'tracks', file_path] + name_args, | ||
stdout=open(devnull, 'wb')) | ||
return names | ||
|
||
def extract_attachments(self, out_folder=''): | ||
"""Extract attachments.""" | ||
names = [] | ||
name_args = [] | ||
for attachment in self.attachments: | ||
bname = splitext(basename(attachment.file_path))[0] | ||
name = join(out_folder, bname + "_" + attachment.name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same concerns as part 1 of the comment on line 688. As for the naming of attachment files, I think it would be more appropriate to keep the attachment name specified and not prepend the basename. |
||
name_args.append('{}:{}'.format(attachment.id, name)) | ||
names.append(name) | ||
file_path = attachment._file_path | ||
|
||
sp.run([self.mkvextract_path, 'attachments', file_path] + name_args, | ||
stdout=open(devnull, 'wb')) | ||
return names |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at the mkvmerge output schema, it seems that "id", "size", and "properties" will always be present in an attachment's section of the info_json. Therefore, these should be added in the MKVAttachment constructor so they are documented and not just dynamically assigned.
Note that "uid" is not required within the "properties" section of the mkvmerge output schema. I recommend including "properties" as a parameter to the constructor, then within the constructor, check if "uid" exists and assign it to an attribute within the MKVAttachment object.
Perhaps an implementation that allows something like this: