def init(self): if not isinstance(self.config.file, str): raise BadConfig('This should be a string.', self, 'file') self.file = expand(self.config.file) if not os.path.exists(self.file): msg = 'File %r does not exist.' % self.file raise BadConfig(msg, self, 'file') timestamps_file = self.file + MPlayer.TIMESTAMPS_SUFFIX if os.path.exists(timestamps_file): self.info('Reading timestamps from %r.' % timestamps_file) self.timestamps = open(timestamps_file) else: self.timestamps = None self.info('Will use fps for timestamps.') self.mencoder_started = False self.state.timestamp = None self.state.timestamp = self.get_next_timestamp() self.state.next_frame = None self.state.finished = False self.num_frames_read = 0
def init(self): self.programs = check_programs_existence(programs=['mencoder']) for p in self.programs: self.info('Using %13s = %s' % (p, self.programs[p])) if not isinstance(self.config.file, str): raise BadConfig('This should be a string.', self, 'file') self.file = expand(self.config.file) if not os.path.exists(self.file): msg = 'File %r does not exist.' % self.file raise BadConfig(msg, self, 'file') timestamps_file = self.file + MPlayer.TIMESTAMPS_SUFFIX if os.path.exists(timestamps_file): self.info('Reading timestamps from %r.' % timestamps_file) self.timestamps = open(timestamps_file) else: self.timestamps = None #self.info('Will use fps for timestamps.') self.mencoder_started = False self.state.timestamp = None self.state.timestamp = self.get_next_timestamp() self.state.next_frame = None self.state.finished = False self.num_frames_read = 0
def init(self): dirname = self.config.dir dirname = expand(dirname) if not os.path.exists(dirname): raise BadConfig('Not existent directory %r.' % dirname, self, 'dir') if not os.path.isdir(dirname): raise BadConfig('The file %r is not a directory.' % dirname, self, 'dir') regexp = self.config.regexp # TODO: use proper logging self.info("Reading %r from %r." % (regexp, dirname)) all_files = os.listdir(dirname) selected = [os.path.join(dirname, f) for f in all_files if re.match(regexp, f)] def natural_key(string): """See http://www.codinghorror.com/blog/archives/001018.html""" return [int(s) if s.isdigit() else s for s in re.split(r'(\d+)', string)] selected.sort(key=lambda x: natural_key(os.path.basename(x))) self.info("Selected %d/%d files." % (len(selected), len(all_files))) fps = float(self.config.fps) if fps <= 0: raise BadConfig(self, "Invalid fps value %s." % fps, 'fps') # tuples (timestamp, filename) frames = [(i / fps, f) for i, f in enumerate(selected)] if not frames: raise Exception('No frames found in dir %r.' % dirname) self.state.frames = frames self.state.next_frame = 0
def init(self): dirname = self.config.dir dirname = expand(dirname) if not os.path.exists(dirname): raise Exception('Non existent directory "%s".' % dirname) if not os.path.isdir(dirname): raise Exception('The file "%s" is not a directory.' % dirname) # TODO: use proper logging self.info("Reading directory listings from %s" % dirname) all_files = os.listdir(dirname) regexp = '(\w+)_(\d+)\.(\d+)\.png' # tuples (timestamp, filename) frames = [] for filename in all_files: m = re.match(regexp, filename) if m: # signal_name = m.group(1) seconds = m.group(2) fraction = m.group(3) timestamp = float(seconds) + float('0.%s' % fraction) global_filename = os.path.join(dirname, filename) frames.append((timestamp, global_filename)) if not frames: raise Exception('No frames found in dir "%s".' % dirname) self.info("Read %s frames -- sorting." % len(frames)) frames.sort(key=lambda x: x[0]) self.state.frames = frames self.state.next_frame = 0 self.info("Camera log ready for %s" % dirname)
def try_initialization(self): # If we don't have at least two frames, continue if len(self.buffer) < 2: return # Get height and width from first image first_image = self.buffer[0][1] self.shape = first_image.shape self.height = self.shape[0] self.width = self.shape[1] if self.height > 8192 or self.width > 8192: msg = 'Mencoder cannot support movies this big (%sx%s)' msg = msg % (self.height, self.width) raise Exception(msg) self.ndim = len(self.shape) # Format for mencoder's rawvideo "format" option if self.ndim == 2: format = 'y8' # @ReservedAssignment else: if self.shape[2] == 3: format = 'rgb24' # @ReservedAssignment elif self.shape[2] == 4: # Note: did not try this yet format = 'rgba' # @ReservedAssignment msg = 'I detected that you are trying to write a transparent' msg += 'video. This does not work well yet (and besides,' msg += 'it is not supported in many applications, like ' msg += 'Keynote). Anyway, the plan is to use mencoder ' msg += 'to write a .AVI with codec "png". This will fail ' msg += 'for now, but perhaps in the future it will be ' msg += '.better' self.error(msg) # guess the fps if we are not given the config if self.config.fps is None: delta = self.buffer[-1][0] - self.buffer[0][0] if delta == 0: timestamps = [x[0] for x in self.buffer] self.debug('Got 0 delta: timestamps: %s' % timestamps) fps = 0 else: fps = (len(self.buffer) - 1) / delta # Check for very wrong results if not (3 < fps < 60): self.error('Detected fps is %.2f; this seems strange to me,' ' so I will use the safe choice fps = %.2f.' % (fps, self.config.fps_safe)) fps = self.config.fps_safe else: fps = self.config.fps # adapt the bitrate to the size of the image vbitrate0 = self.config.firstpass_bitrate shape0 = (640, 480) n1 = self.width * self.height n0 = shape0[0] * shape0[1] vbitrate = vbitrate0 * n1 / n0 self.info('Estimated bitrate %r' % vbitrate) max_bitrate = 90 * 1000 * 1000 if vbitrate > max_bitrate: vbitrate = max_bitrate self.info('Estimated bitrate too high, capping at %s' % vbitrate) self.filename = expand(self.config.file) if os.path.exists(self.filename): self.info('Removing previous version of %s.' % friendly_path(self.filename)) os.unlink(self.filename) self.tmp_filename = '%s-active.avi' % self.filename make_sure_dir_exists(self.filename) self.info('Writing %dx%d %s video stream at %.3f fps to %r.' % (self.width, self.height, format, fps, friendly_path(self.filename))) if format == 'rgba': ovc = ['-ovc', 'lavc', '-lavcopts', 'vcodec=png'] else: ovc = [ '-ovc', 'lavc', '-lavcopts', 'vcodec=%s:vbitrate=%d' % ('mpeg4', vbitrate) ] out = ['-o', self.tmp_filename] args = [ self.programs['mencoder'], '/dev/stdin', '-demuxer', 'rawvideo', '-rawvideo', 'w=%d:h=%d:fps=%f:format=%s' % (self.width, self.height, fps, format) ] + ovc + out # '-v', "0", # verbosity level (1 prints stats \r) # self.debug('$ %s' % " ".join(args)) # Note: mp4 encoding is currently broken in mencoder :-( # so we have to use ffmpeg as a second step. # These would be the options to add: # '-of', 'lavf', '-lavfopts', 'format=mp4' self.tmp_stdout = tempfile.TemporaryFile() self.tmp_stderr = tempfile.TemporaryFile() quiet = self.config.quiet if quiet: # XXX /dev/null not portable # self.debug('stderr: %s' % self.tmp_stderr) # self.debug('stdout: %s' % self.tmp_stdout) # TODO: write error self.process = subprocess.Popen(args, preexec_fn=ignore_sigint, stdin=subprocess.PIPE, stdout=self.tmp_stdout.fileno(), stderr=self.tmp_stderr.fileno()) else: self.process = subprocess.Popen(args=args, stdin=subprocess.PIPE) if self.config.timestamps: self.timestamps_filename = self.filename + '.timestamps' self.timestamps_file = open(self.timestamps_filename, 'w')
def try_initialization(self): # If we don't have at least two frames, continue if len(self.buffer) < 2: return # Get height and width from first image first_image = self.buffer[0][1] self.shape = first_image.shape self.height = self.shape[0] self.width = self.shape[1] if self.height > 8192 or self.width > 8192: msg = 'Mencoder cannot support movies this big (%sx%s)' msg = msg % (self.height, self.width) raise Exception(msg) self.ndim = len(self.shape) # Format for mencoder's rawvideo "format" option if self.ndim == 2: format = 'y8' # @ReservedAssignment else: if self.shape[2] == 3: format = 'rgb24' # @ReservedAssignment elif self.shape[2] == 4: # Note: did not try this yet format = 'rgba' # @ReservedAssignment msg = 'I detected that you are trying to write a transparent' msg += 'video. This does not work well yet (and besides,' msg += 'it is not supported in many applications, like ' msg += 'Keynote). Anyway, the plan is to use mencoder ' msg += 'to write a .AVI with codec "png". This will fail ' msg += 'for now, but perhaps in the future it will be ' msg += '.better' self.error(msg) # guess the fps if we are not given the config if self.config.fps is None: delta = self.buffer[-1][0] - self.buffer[0][0] if delta == 0: timestamps = [x[0] for x in self.buffer] self.debug('Got 0 delta: timestamps: %s' % timestamps) fps = 0 else: fps = (len(self.buffer) - 1) / delta # Check for very wrong results if not (3 < fps < 60): self.error('Detected fps is %.2f; this seems strange to me,' ' so I will use the safe choice fps = %.2f.' % (fps, self.config.fps_safe)) fps = self.config.fps_safe else: fps = self.config.fps # adapt the bitrate to the size of the image vbitrate0 = self.config.firstpass_bitrate shape0 = (640, 480) n1 = self.width * self.height n0 = shape0[0] * shape0[1] vbitrate = vbitrate0 * n1 / n0 self.info('Estimated bitrate %r' % vbitrate) max_bitrate = 90*1000*1000 if vbitrate > max_bitrate: vbitrate = max_bitrate self.info('Estimated bitrate too high, capping at %s' % vbitrate) self.filename = expand(self.config.file) if os.path.exists(self.filename): self.info('Removing previous version of %s.' % friendly_path(self.filename)) os.unlink(self.filename) self.tmp_filename = '%s-active.avi' % self.filename make_sure_dir_exists(self.filename) self.info('Writing %dx%d %s video stream at %.3f fps to %r.' % (self.width, self.height, format, fps, friendly_path(self.filename))) if format == 'rgba': ovc = ['-ovc', 'lavc', '-lavcopts', 'vcodec=png'] else: ovc = ['-ovc', 'lavc', '-lavcopts', 'vcodec=%s:vbitrate=%d' % ('mpeg4', vbitrate)] out = ['-o', self.tmp_filename] args = [ self.programs['mencoder'], '/dev/stdin', '-demuxer', 'rawvideo', '-rawvideo', 'w=%d:h=%d:fps=%f:format=%s' % (self.width, self.height, fps, format) ] + ovc + out # '-v', "0", # verbosity level (1 prints stats \r) # self.debug('$ %s' % " ".join(args)) # Note: mp4 encoding is currently broken in mencoder :-( # so we have to use ffmpeg as a second step. # These would be the options to add: # '-of', 'lavf', '-lavfopts', 'format=mp4' self.tmp_stdout = tempfile.TemporaryFile() self.tmp_stderr = tempfile.TemporaryFile() quiet = self.config.quiet if quiet: # XXX /dev/null not portable # self.debug('stderr: %s' % self.tmp_stderr) # self.debug('stdout: %s' % self.tmp_stdout) # TODO: write error self.process = subprocess.Popen(args, preexec_fn=ignore_sigint, stdin=subprocess.PIPE, stdout=self.tmp_stdout.fileno(), stderr=self.tmp_stderr.fileno()) else: self.process = subprocess.Popen(args=args, stdin=subprocess.PIPE) if self.config.timestamps: self.timestamps_filename = self.filename + '.timestamps' self.timestamps_file = open(self.timestamps_filename, 'w')