Skip to content
This repository has been archived by the owner on Jun 11, 2023. It is now read-only.

Commit

Permalink
Add auto dectection of resolution using mplayer2
Browse files Browse the repository at this point in the history
This commit is a proof-of-concept.
Two problems needed to be solved.

1. The 1st comment file may not have a matching video file
2. The video file may not have .mp4 extension
  • Loading branch information
Alex Vong committed Feb 17, 2015
1 parent 387985d commit 26fa477
Showing 1 changed file with 44 additions and 3 deletions.
47 changes: 44 additions & 3 deletions danmaku2ass.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import os
import random
import re
import subprocess
import sys
import time
import xml.dom.minidom
Expand Down Expand Up @@ -814,13 +815,46 @@ def GetCommentProcessor(input_file):
return CommentFormatMap[ProbeCommentFormat(input_file)]


# Use mplayer2 to get the resolution info of the video file
# having the same name as the comment file
def get_resolution(file_name):
try:
width = 0
height = 0
media_info = subprocess.check_output(["mplayer", "-really-quiet"
, "-ao", "null", "-vo", "null"
, "-identify", "-frames", "0"
, file_name]).decode(
"utf-8").split("\n")
for item in media_info:
if "ID_VIDEO_WIDTH=" in item:
width = int(item.split("=")[1])
elif "ID_VIDEO_HEIGHT=" in item:
height = int(item.split("=")[1])
if width > 0 and height > 0:
return [width, height]
else:
raise Exception()
except:
print()
print("Resolution is not specified by --size WIDTHxHEIGHT")
print("and fail to read resolution info using mplayer")
print()
print("Option 1: set the size manually using --size WIDTHxHEIGHT")
print()
print("Option 2: rename the video so that it has the same name")
print("as the comment file and install mplayer2 if")
print("it is not already pre-installed on your distro")
print()


def main():
logging.basicConfig(format='%(levelname)s: %(message)s')
if len(sys.argv) == 1:
sys.argv.append('--help')
parser = argparse.ArgumentParser()
parser.add_argument('-o', '--output', metavar=_('OUTPUT'), help=_('Output file'))
parser.add_argument('-s', '--size', metavar=_('WIDTHxHEIGHT'), required=True, help=_('Stage size in pixels'))
parser.add_argument('-s', '--size', metavar=_('WIDTHxHEIGHT'), help=_('Stage size in pixels'))
parser.add_argument('-fn', '--font', metavar=_('FONT'), help=_('Specify font face [default: %s]') % _('(FONT) sans-serif')[7:], default=_('(FONT) sans-serif')[7:])
parser.add_argument('-fs', '--fontsize', metavar=_('SIZE'), help=(_('Default font size [default: %s]') % 25), type=float, default=25.0)
parser.add_argument('-a', '--alpha', metavar=_('ALPHA'), help=_('Text opacity'), type=float, default=1.0)
Expand All @@ -834,8 +868,15 @@ def main():
width, height = str(args.size).split('x', 1)
width = int(width)
height = int(height)
except ValueError:
raise ValueError(_('Invalid stage size: %r') % args.size)
except:
# Get the res of the 1st file, assume the rest are the same
#
# Two problems need to be solved:
# 1. The 1st comment file may not have a matching video file
# 2. The video file may not have .mp4 extension
res = get_resolution(args.file[0].replace(".xml", ".mp4"))
width = res[0]
height = res[1]
Danmaku2ASS(args.file, args.output, width, height, args.protect, args.font, args.fontsize, args.alpha, args.duration_marquee, args.duration_still, args.reduce)


Expand Down

0 comments on commit 26fa477

Please sign in to comment.