clips frames

This commit is contained in:
j 2007-07-10 20:01:41 +00:00
parent 5a9fd08511
commit b3558df3be
4 changed files with 42 additions and 27 deletions

View file

@ -31,9 +31,8 @@ def saveFile(f_name, data):
def loadStaticFile(fname): def loadStaticFile(fname):
return loadFile(join(dirname(abspath(__file__)), "static", fname)) return loadFile(join(dirname(abspath(__file__)), "static", fname))
def loadFrame(afile, position): def loadFrame(afile, position):
position = basename(position) position = basename(position).replace(':', '.')
frame = join(afile.frameFolder, '%s.%s' % (position, img_extension)) frame = join(afile.frameFolder, '%s.%s' % (position, img_extension))
if not exists(frame): if not exists(frame):
afile.extractFrame(position) afile.extractFrame(position)
@ -42,7 +41,7 @@ def loadFrame(afile, position):
return '' return ''
def loadClip(afile, position): def loadClip(afile, position):
position = basename(position) position = basename(position).replace(':', '.')
flash = join(afile.frameFolder, '%s.%s' % (position, 'flv')) flash = join(afile.frameFolder, '%s.%s' % (position, 'flv'))
if not exists(flash): if not exists(flash):
afile.extractClip(position) afile.extractClip(position)

View file

@ -31,16 +31,19 @@ class Root(controllers.RootController):
if action == 'metadata': if action == 'metadata':
return dict(metadata = f) return dict(metadata = f)
elif action in ('timeline', 'timeline.png'): elif action in ('timeline', 'timeline.png'):
cherrypy.response.headerMap['Content-Type'] = "image/jpeg" cherrypy.response.headerMap['Content-Type'] = "image/png"
cherrypy.response.headerMap["Expires"] = httpExpires(60*60*24*15) cherrypy.response.headerMap["Expires"] = httpExpires(60*60*24*15)
return f.timeline() return f.timeline()
elif position: #clip / frame elif position: #clip / frame
cherrypy.response.headerMap['Content-Type'] = "image/jpeg"
cherrypy.response.headerMap["Expires"] = httpExpires(60*60*24*15)
position = position.replace('.png', '').replace('.jpg', '') position = position.replace('.png', '').replace('.jpg', '')
position = position.replace('.flv', '')
position = position.replace('-', ':').replace('.',':') position = position.replace('-', ':').replace('.',':')
if action == 'clip': if action == 'clip':
cherrypy.response.headerMap['Content-Type'] = "video/x-flv"
cherrypy.response.headerMap["Expires"] = httpExpires(60*60*24*15)
return f.clip(position) return f.clip(position)
elif action == 'frame': elif action == 'frame':
cherrypy.response.headerMap['Content-Type'] = "image/jpeg"
cherrypy.response.headerMap["Expires"] = httpExpires(60*60*24*15)
return f.frame(position) return f.frame(position)
return dict() return dict()

View file

@ -8,14 +8,15 @@ import turbogears
import re import re
from urllib import quote, quote_plus from urllib import quote, quote_plus
import os import os
from os.path import abspath, join, dirname from os.path import abspath, join, dirname, exists
from datetime import datetime from datetime import datetime
import time import time
import math import math
from glob import glob from glob import glob
import shutil import shutil
from scrapeit.utils import read_url
import socket
import cache import cache
import oxdb_import import oxdb_import
from oxdb_utils import oxdb_title, oxdb_director, oxdb_id from oxdb_utils import oxdb_title, oxdb_director, oxdb_id
@ -40,9 +41,12 @@ class Archive(SQLObject):
def notifyFrontend(self, action, md5sum): def notifyFrontend(self, action, md5sum):
if self.baseUrlFrontend: if self.baseUrlFrontend:
dto = socket.getdefaulttimeout()
socket.setdefaulttimeout(100)
url = "%s/%s?md5sum=%s" % (self.baseUrlFrontend, action, md5sum) url = "%s/%s?md5sum=%s" % (self.baseUrlFrontend, action, md5sum)
result = read_url(url) result = read_url(url)
print "Frontend:", result print "Frontend:", result
socket.setdefaulttimeout(dto)
def _get_files(self): def _get_files(self):
q = ArchiveFile.select(ArchiveFile.q.archiveID == self.id) q = ArchiveFile.select(ArchiveFile.q.archiveID == self.id)
@ -263,7 +267,7 @@ class ArchiveFile(SQLObject):
def updateMeta(self): def updateMeta(self):
self.findSubtitleLink() self.findSubtitleLink()
if os.path.exists(self.absolutePath): if exists(self.absolutePath):
info = midentify.identify(self.absolutePath) info = midentify.identify(self.absolutePath)
self.length = info['length'] self.length = info['length']
self.width = info['width'] self.width = info['width']
@ -338,7 +342,7 @@ class ArchiveFile(SQLObject):
return join(cache.cache_root, 'mini', self.md5sum[:4], "%s.avi" % self.md5sum) return join(cache.cache_root, 'mini', self.md5sum[:4], "%s.avi" % self.md5sum)
def _get_frameFolder(self): def _get_frameFolder(self):
f = join(cache.cache_root, 'mini', self.md5sum[:4], self.md5sum) f = join(cache.cache_root, 'frame', self.md5sum[:4], self.md5sum)
if not exists(f): if not exists(f):
os.makedirs(f) os.makedirs(f)
return f return f
@ -347,7 +351,7 @@ class ArchiveFile(SQLObject):
return join(cache.cache_root, 'timeline', self.md5sum[:4], "%s.png" % self.md5sum) return join(cache.cache_root, 'timeline', self.md5sum[:4], "%s.png" % self.md5sum)
def removeMiniMovie(self): def removeMiniMovie(self):
if os.path.exists(self.mini_movie_file): if exists(self.mini_movie_file):
os.remove(self.mini_movie_file) os.remove(self.mini_movie_file)
def _findSubtitleByStart(self, start): def _findSubtitleByStart(self, start):
@ -365,13 +369,16 @@ class ArchiveFile(SQLObject):
self.extractTimeline() self.extractTimeline()
self.extracted = True self.extracted = True
def extractClip(self, inpoint, outpoint=-1, flash_folder=cache.frame_cache_root): def extractClip(self, inpoint, outpoint=-1, flash_folder=-1):
if flash_folder == -1:
flash_folder = self.frameFolder
movie_file = self.mini_movie_file movie_file = self.mini_movie_file
position = inpoint.replace(':', '.') position = inpoint.replace(':', '.')
flash_movie = join(self.frameFolder, '%s.%s' % (position, 'flv')) flash_movie = join(self.frameFolder, '%s.%s' % (position, 'flv'))
width = 128 width = 128
height = int(width / (self.width / self.height)) height = int(width / (self.width / self.height))
height = height - height % 2 height = height - height % 2
inpoint = inpoint.replace('.', ':')
if outpoint == -1: if outpoint == -1:
s = self._findSubtitleByStart(inpoint) s = self._findSubtitleByStart(inpoint)
if s: if s:
@ -381,21 +388,23 @@ class ArchiveFile(SQLObject):
extract_flash(movie_file, flash_movie, inpoint, outpoint, width, height, offset = 0) extract_flash(movie_file, flash_movie, inpoint, outpoint, width, height, offset = 0)
#extract_flash_ng(self.absolutePath, flash_movie, inpoint, outpoint, width, height, offset) #extract_flash_ng(self.absolutePath, flash_movie, inpoint, outpoint, width, height, offset)
def extractFrame(self, position, img_folder=cache.frame_cache_root): def extractFrame(self, position, img_folder=-1):
if img_folder == -1:
img_folder = self.frameFolder
if self.movieFile: if self.movieFile:
return self.movieFile.extractFrame(position, img_folder) return self.movieFile.extractFrame(position, img_folder)
movie_file = self.mini_movie_file movie_file = self.mini_movie_file
img_folder = join(img_folder, self.oxdb) if not exists(img_folder):
if not os.path.exists(img_folder):
os.makedirs(img_folder) os.makedirs(img_folder)
position = position.replace('.', ':')
extract_frame(movie_file, position, img_folder, offset = 0, redo = False) extract_frame(movie_file, position, img_folder, offset = 0, redo = False)
def extractFrames(self, img_folder=cache.frame_cache_root): def extractFrames(self, img_folder=cache.frame_cache_root):
if self.movieFile: if self.movieFile:
return self.movieFile.extractFrames(img_folder) return self.movieFile.extractFrames(img_folder)
movie_file = self.absolutePath movie_file = self.absolutePath
img_folder = join(img_folder, self.oxdb) if not exists(img_folder):
if not os.path.exists(img_folder):
os.makedirs(img_folder) os.makedirs(img_folder)
extract_subtitles(movie_file, self.srt.encode('utf-8'), img_folder, width=100, offset=self.offset) extract_subtitles(movie_file, self.srt.encode('utf-8'), img_folder, width=100, offset=self.offset)
@ -404,12 +413,12 @@ class ArchiveFile(SQLObject):
return return
mini_movie_file = self.mini_movie_file mini_movie_file = self.mini_movie_file
movie_file = self.absolutePath movie_file = self.absolutePath
if not movie_file or not os.path.exists(movie_file): if not movie_file or not exists(movie_file):
return return
if os.path.exists(mini_movie_file) and not force: if exists(mini_movie_file) and not force:
print "clip exists, skipping extraction", mini_movie_file print "clip exists, skipping extraction", mini_movie_file
return return
if not os.path.exists(dirname(mini_movie_file)): if not exists(dirname(mini_movie_file)):
os.makedirs(dirname(mini_movie_file)) os.makedirs(dirname(mini_movie_file))
options = '' options = ''
options += " -ovc lavc -lavcopts vcodec=mjpeg" options += " -ovc lavc -lavcopts vcodec=mjpeg"
@ -423,7 +432,7 @@ class ArchiveFile(SQLObject):
def removeTimeline(self): def removeTimeline(self):
if os.path.exists(self.timelineFile): if exists(self.timelineFile):
os.unlink(self.timelineFile) os.unlink(self.timelineFile)
def extractTimeline(self, force = False): def extractTimeline(self, force = False):
@ -437,14 +446,16 @@ class ArchiveFile(SQLObject):
return return
t = self.timelineFile t = self.timelineFile
if os.path.exists(self.mini_movie_file): if exists(self.mini_movie_file):
if not os.path.exists(os.path.dirname(t)): if not exists(os.path.dirname(t)):
os.makedirs(os.path.dirname(t)) os.makedirs(os.path.dirname(t))
#lets only extract the timeline if it does not exist yet #lets only extract the timeline if it does not exist yet
if os.path.exists(t): if exists(t):
print "skipping, ", self.path print "skipping, ", self.path
return return
extractTimelineScript = abspath(join(dirname(__file__), "tools/extract_timeline.py")) #this fails in tg-admin shell
#extractTimelineScript = abspath(join(dirname(__file__), "tools/extract_timeline.py"))
extractTimelineScript = "oxdbarchive/tools/extract_timeline.py"
cmd = "python %s %s %s" %(extractTimelineScript, t, self.mini_movie_file) cmd = "python %s %s %s" %(extractTimelineScript, t, self.mini_movie_file)
os.system(cmd) os.system(cmd)
@ -458,7 +469,7 @@ class ArchiveFile(SQLObject):
if movieFile: if movieFile:
movieFile.srt = '' movieFile.srt = ''
if subtitle and movieFile: if subtitle and movieFile:
if not subtitle.absolutePath or not os.path.exists(subtitle.absolutePath): if not subtitle.absolutePath or not exists(subtitle.absolutePath):
return return
if not subtitle.absolutePath.endswith('.srt'): if not subtitle.absolutePath.endswith('.srt'):
print "this is not a subtitle", subtitle.absolutePath print "this is not a subtitle", subtitle.absolutePath

View file

@ -6,6 +6,7 @@ import re
import os import os
from os.path import abspath, join, dirname from os.path import abspath, join, dirname
import shutil import shutil
import time
import chardet import chardet
@ -214,6 +215,7 @@ def extract_frame(movie_file, timestamp, img_folder, width=128, offset = 0, redo
for f in files[:-2]: for f in files[:-2]:
print "unlink", f print "unlink", f
os.unlink(f) os.unlink(f)
time.sleep(0.1)
else: else:
print "update the cache %s missing" % movie_file print "update the cache %s missing" % movie_file
shutil.rmtree(framedir) shutil.rmtree(framedir)