class ThumbExtractionException(Exception):
pass
+class AVInfoException(Exception):
+ pass
+
class FileTransferException(Exception):
pass
import subprocess
import re
import os
+import math
class FFmpegTranscoder(base.BaseTranscoder):
"""
prog_bin = "ffprobe"
@staticmethod
- def get_video_duration(input_file):
+ def get_video_duration(input_file, formated=False):
args = FFmpegAVInfo.prog_bin + ' -show_format "' \
+ input_file + '"'
# Search for the line which contains duration information.
m = re.match(r"duration=([\d\.]+)", line)
if m is not None:
- return float(m.group(1))
+ seconds = float(m.group(1))
+ if not formated:
+ return seconds
+ else:
+ seconds = math.floor(seconds)
+ minutes = math.floor(seconds / 60)
+ seconds = seconds % 60
+ if minutes >= 60:
+ hours = math.floor(minutes / 60)
+ minutes = minutes % 60
+
+ return "%02d:%02d:%02d" % (hours, minutes, seconds)
+ else:
+ return "%02d:%02d" % (minutes, seconds)
exit_code = p.wait()
if exit_code > 0:
- raise api_exceptions.ThumbExtractionException( \
- 'FFmpeg exited with code ' + str(exit_code) + '.')
+ raise api_exceptions.AVInfoException( \
+ 'ffprobe exited with code ' + str(exit_code) + '.')
class BaseAVInfo:
@staticmethod
- def get_video_duration(input_file):
+ def get_video_duration(input_file, formated=False):
"""
- Returns the number of seconds of a video (int/float).
+ Returns the number of seconds of a video (int/float) if formated is
+ False and a string for duration formated as [HH:]:mm:ss otherwise.
"""
pass
import sys
import config
+import os
-#
-# !! Imports required for create_torrent
-#
from BaseLib.Core.API import *
-#
-#
-#
-def create_torrent(input_):
+def create_torrent(source):
+ """ Creates a torrent file for the video source file. """
+
+ if isinstance(source, unicode):
+ usource = source
+ else:
+ usource = source.decode(sys.getfilesystemencoding())
+
+ duration = config.AVINFO_CLASS.get_video_duration(source, True)
+
+ print config.BT_TRACKER, duration, source
+
tdef = TorrentDef()
- tdef.add_content(input_, config.AVINFO_CLASS.get_video_duration(input_))
+ tdef.add_content(usource, playtime=duration)
tdef.set_tracker(config.BT_TRACKER)
tdef.set_piece_length(32768)
tdef.finalize()
- tdef.save(input_ + ".tstream")
+ tdef.save(source + '.tstream')
- print 'READY!', config.BT_TRACKER, config.AVINFO_CLASS.get_video_duration(input_)
if __name__ == '__main__':
pass