166 lines
5.9 KiB
Python
166 lines
5.9 KiB
Python
# -*- coding: utf-8 -*-
|
|
# -*- Mode: Python; -*-
|
|
# vi:si:et:sw=2:sts=2:ts=2
|
|
|
|
import re
|
|
import os
|
|
from os.path import abspath, join, dirname
|
|
import shutil
|
|
import time
|
|
|
|
from subtitles import time2ms
|
|
|
|
|
|
def extract_flash_ng(movie_file, flash_file, inpoint, outpoint, width=128, height=96, offset = 0):
|
|
ext = movie_file.split('.')[-1]
|
|
if ext in ('sub', 'srt'):
|
|
print "this is not a movie file, will not try to extract frames"
|
|
return
|
|
if offset:
|
|
print "Inpoint ", inpoint,
|
|
inpoint = shift_time(-offset, inpoint)
|
|
outpoint = shift_time(-offset, outpoint)
|
|
print " becomes ", inpoint
|
|
|
|
print "extracting %s -> %s" % (inpoint, outpoint)
|
|
duration = time2ms(outpoint) - time2ms(inpoint)
|
|
inpoint = time2ms(inpoint)
|
|
extractClipScript = abspath(join(dirname(__file__), "tools/extract_clip.py"))
|
|
|
|
cmd = '''%s "%s" %s %s %s''' % (extractClipScript, movie_file, flash_file, inpoint, duration)
|
|
os.system(cmd.encode('utf-8'))
|
|
|
|
def extract_flash(movie_file, flash_file, inpoint, outpoint, width=128, height=96, offset = 0):
|
|
import warnings
|
|
warnings.filterwarnings("ignore", "tempnam")
|
|
ext = movie_file.split('.')[-1]
|
|
if ext in ('sub', 'srt', 'mkv'):
|
|
print "this is not a movie file, will not try to extract frames"
|
|
return
|
|
framedir = os.tempnam()
|
|
os.mkdir(framedir)
|
|
os.chdir(framedir)
|
|
if offset:
|
|
print "Inpoint ", inpoint,
|
|
inpoint = shift_time(-offset, inpoint)
|
|
outpoint = shift_time(-offset, outpoint)
|
|
print " becomes ", inpoint
|
|
print "extracting %s -> %s" % (inpoint, outpoint)
|
|
outpoint = float(time2ms(outpoint) - time2ms(inpoint)) / 1000 + 1
|
|
|
|
audiorate = "44100"
|
|
if os.path.exists(movie_file):
|
|
mencoder_options = ''
|
|
mencoder_options += " '%s'" % movie_file
|
|
mencoder_options += " -ss '%s' -endpos %0.2f" % (inpoint, outpoint)
|
|
mencoder_options += ' -ovc copy -oac copy -o tempfile.avi '
|
|
mencoder = "mencoder %s >/dev/null 2>&1" % mencoder_options
|
|
#print mencoder.encode('utf-8')
|
|
os.system(mencoder.encode('utf-8'))
|
|
|
|
#create flash file
|
|
ffmpeg_options = ''
|
|
#ffmpeg_options += " -ss '%s' -t %0.2f" % (inpoint, outpoint)
|
|
ffmpeg_options += " -y -i 'tempfile.avi'"
|
|
ffmpeg_options += " -ar %s -b 128000 '%s'" % (audiorate, flash_file)
|
|
ffmpeg = "ffmpeg %s >/dev/null 2>&1" % ffmpeg_options
|
|
#print ffmpeg.encode('utf-8')
|
|
os.system(ffmpeg.encode('utf-8'))
|
|
else:
|
|
print "update the cache %s missing" % movie_file.encode('utf-8')
|
|
shutil.rmtree(framedir)
|
|
|
|
|
|
def extract_ogg(movie_file, clip_file, inpoint, outpoint, width=128, height=96, offset = 0):
|
|
import warnings
|
|
warnings.filterwarnings("ignore", "tempnam")
|
|
ext = movie_file.split('.')[-1]
|
|
if ext in ('sub', 'srt', 'mkv'):
|
|
print "this is not a movie file, will not try to extract frames"
|
|
return
|
|
framedir = os.tempnam()
|
|
os.mkdir(framedir)
|
|
os.chdir(framedir)
|
|
if offset:
|
|
print "Inpoint ", inpoint,
|
|
inpoint = shift_time(-offset, inpoint)
|
|
outpoint = shift_time(-offset, outpoint)
|
|
print " becomes ", inpoint
|
|
print "extracting %s -> %s" % (inpoint, outpoint)
|
|
outpoint = float(time2ms(outpoint) - time2ms(inpoint)) / 1000 + 1
|
|
|
|
audiorate = "44100"
|
|
if os.path.exists(movie_file):
|
|
mencoder_options = ''
|
|
mencoder_options += " '%s'" % movie_file
|
|
mencoder_options += " -ss '%s' -endpos %0.2f" % (inpoint, outpoint)
|
|
mencoder_options += ' -ovc copy -oac copy -o tempfile.avi '
|
|
mencoder = "mencoder %s >/dev/null 2>&1" % mencoder_options
|
|
#print mencoder.encode('utf-8')
|
|
os.system(mencoder.encode('utf-8'))
|
|
|
|
#create ogg file
|
|
ffmpeg2theora_options = ''
|
|
ffmpeg2theora_options += " 'tempfile.avi' -H %s -o '%s'" % (audiorate, clip_file)
|
|
ffmpeg2theora = "ffmpeg2theora %s >/dev/null 2>&1" % ffmpeg2theora_options
|
|
#print ffmpeg2theora.encode('utf-8')
|
|
os.system(ffmpeg2theora.encode('utf-8'))
|
|
else:
|
|
print "update the cache %s missing" % movie_file.encode('utf-8')
|
|
shutil.rmtree(framedir)
|
|
|
|
def extract_frame(movie_file, timestamp, img_folder, width=128, offset = 0, redo = False):
|
|
import warnings
|
|
warnings.filterwarnings("ignore", "tempnam")
|
|
ext = movie_file.split('.')[-1]
|
|
if ext in ('sub', 'srt'):
|
|
print "this is not a movie file, will not try to extract frames"
|
|
return
|
|
framedir = os.tempnam()
|
|
|
|
os.mkdir(framedir)
|
|
os.chdir(framedir)
|
|
if offset:
|
|
timestamp_in_file = shift_time(-offset, timestamp)
|
|
else:
|
|
timestamp_in_file = timestamp
|
|
if os.path.exists(movie_file):
|
|
mplayer_options = ''
|
|
mplayer_options += " '%s'" % movie_file
|
|
mplayer_options += " -ss '%s' -frames 2" % (timestamp_in_file)
|
|
mplayer_options += " -vo jpeg:quality=90 -vf scale -zoom -xy %d " % width
|
|
mplayer_options += " -ao null"
|
|
mplayer = "mplayer %s >/dev/null 2>&1" % mplayer_options
|
|
frame = os.path.join(img_folder, "%s.%s" % (timestamp.replace(':', '.'), img_extension))
|
|
if redo or not os.path.exists(frame):
|
|
print mplayer.encode('utf-8')
|
|
os.system (mplayer.encode('utf-8'))
|
|
files = os.listdir(framedir)
|
|
if files:
|
|
print "creating frame ", frame
|
|
shutil.move(os.path.join(framedir,files[-1]), frame)
|
|
if len(files)>1:
|
|
for f in files[:-2]:
|
|
print "unlink", f
|
|
os.unlink(f)
|
|
time.sleep(0.1)
|
|
else:
|
|
print "update the cache %s missing" % movie_file
|
|
shutil.rmtree(framedir)
|
|
|
|
def extract_poster_still(movie_file, png_file, inpoint):
|
|
ext = movie_file.split('.')[-1]
|
|
if ext in ('sub', 'srt'):
|
|
print "this is not a movie file, will not try to extract frames"
|
|
return
|
|
inpoint = time2ms(inpoint)
|
|
extractClipScript = abspath(join(dirname(__file__), "tools/extract_frame.py"))
|
|
|
|
cmd = '''%s "%s" "%s" %s 0 128''' % (extractClipScript, movie_file, png_file, inpoint)
|
|
os.system(cmd.encode('utf-8'))
|
|
|
|
def extract_subtitles(movie_file, srt, img_folder, width=128, offset = 0, redo = False):
|
|
subtitles = srt2dict(srt)
|
|
for k in sorted([int(k) for k in subtitles]):
|
|
timestamp = subtitles["%s" % k]['start']
|
|
extract_frame(movie_file, timestamp, img_folder, width, offset, redo)
|