Ejemplo n.º 1
0
def extract_frames(video_path: str, output_frames_dir: str, notification_path: Optional[str]=None, quality: int=0) -> None:
	"""Use ffmpeg to read a video file and extract the frames.
	Requires 'ffmpeg' to be on the command line.
	The resulting JPEG frames will be named in the format '%06d.jpg'.
	Uses notification_path to indicate whether the extraction completed;
	this will be named video_path'/.finished-extraction' if None.
	Will warn and write over the output dir if it exists but no file at notification_path exists.
	Quality must be between 0 and 31, where 0 is the highest.
	"""
	if notification_path is None: notification_path = pjoin(dirname(video_path), ".finished-extraction")

	if pexists(notification_path) and pexists(output_frames_dir):
		logger.info("Frames directory {} is already complete; skipping.".format(output_frames_dir))
	else:

		if pexists(output_frames_dir):
			logger.warn("Frames directory {} already exists but is incomplete. Extracting frames...".format(output_frames_dir))
		else:
			logger.info("Extracting frames into {}".format(output_frames_dir))

		if not pexists(video_path):
			raise ValueError('Cannot extract frames: video.avi does not exist')

		wrap_cmd_call([
				'ffmpeg',
				'-i', video_path,
				'-q:v', quality,
				pjoin(output_frames_dir, '%06d.jpg')
		])
		with open(notification_path, 'w'): print('')  # all done
Ejemplo n.º 2
0
def gen_video(video_path: str,
              input_frames_dir: str,
              video_frame_rate: str,
              input_image_extension: str = '.jpg',
              encoding: str = 'libx265',
              crf: int = 0) -> None:
    """Use ffmpeg to read generate a video from frames.
	Requires 'ffmpeg' to be on the command line.
	The resulting JPEG frames will be named in the format '%06d.jpg'.
	"""
    input_format_str = pjoin(input_frames_dir,
                             "%06d{}".format(input_image_extension))
    wrap_cmd_call([
        'ffmpeg',
        '-f',
        'concat',  # I don't remember why!
        '-r',
        video_frame_rate,  # we don't know this because we just have frames
        '-safe',
        '0',  # just allow any filename
        '-i',
        input_format_str,
        '-vf',
        'scale=trunc(iw/2)*2:trunc(ih/2)*2',  # compatibility with some players, including QuickTime
        '-c:v',
        encoding,
        '-crf',
        crf,
        '-pix_fmt',
        'yuv420p',
        '-y',
        video_path
    ])
Ejemplo n.º 3
0
 def __darwin_switch(self, i: int):
     if self.output_device is not None:
         logger.debug("Setting audio output device to %s" %
                      self.output_device[i])
         wrap_cmd_call(
             ['SwitchAudioSource', '-s',
              '%s' % self.output_device[i]])
     if self.input_device is not None:
         logger.debug("Setting audio input device to %s" %
                      self.input_device[i])
         wrap_cmd_call([
             'SwitchAudioSource', '-t input', '-s',
             '%s' % self.input_device[i]
         ])
     if self.output_gain is not None:
         logger.debug("Setting system volume to configured default %s" %
                      self.output_gain[i])
         wrap_cmd_call([
             'osascript', '-e',
             'set volume output volume %s' % self.output_gain[i]
         ])
     if self.input_gain is not None:
         logger.debug("Setting input gain to configured default %s" %
                      self.input_gain[i])
         wrap_cmd_call([
             'osascript', '-e',
             'set volume input volume %s' % self.input_gain[i]
         ])
     logger.debug("Done configuring audio")
Ejemplo n.º 4
0
def sevenz(dir_to_sevenz: str,
           sevenz_path: str,
           overwrite: OverwriteChoice = OverwriteChoice.FAIL,
           _7z_executable: str = '7z') -> None:
    """7-zips a directory and adds a .sha256 with the same base filename of the output archive.
	Leaves the original directory when it finishes.
	Requires '7z' to be on the command line.
	"""
    if not pexists(dir_to_sevenz):
        raise InvalidDirectoryException(
            "The path {} to 7-zip does not exist".format(dir_to_sevenz))
    if not pdir(dir_to_sevenz):
        raise InvalidDirectoryException(
            "The path {} to 7-zip is not a directory".format(dir_to_sevenz))

    file_hasher = FileHasher(algorithm=hashlib.sha256, extension='.sha256')
    logging.info("7-zipping files in {}".format(dir_to_sevenz))

    if pexists(sevenz_path) and not pfile(sevenz_path):
        raise InvalidFileException(
            "The 7-zip file cannot be written to {}: The path exists and is not a file"
            .format(sevenz_path))

    if pexists(sevenz_path):
        if overwrite is OverwriteChoice.FAIL:
            raise InvalidFileException(
                "Cannot proceed: The 7-zip file {} already exists.".format(
                    sevenz_path))
        elif overwrite is OverwriteChoice.WARN:
            warnings.warn(
                "The 7-zip file {} already exists. Won't overwrite.".format(
                    sevenz_path))
        elif overwrite is OverwriteChoice.OVERWRITE:
            os.remove(sevenz_path)
        elif overwrite is OverwriteChoice.IGNORE:
            pass

    wrap_cmd_call([_7z_executable, 'a', sevenz_path, dir_to_sevenz])
    file_hasher.add_hash(sevenz_path)  # will overwrite regardless
Ejemplo n.º 5
0
def find_matlab_version(matlab_path: str) -> Optional[str]:
	"""Calls MATLAB to get its version.
	Note: This takes ~30 seconds.
	"""
	out, err = wrap_cmd_call([
		matlab_path, '-nodesktop', '-nosplash', '-nodisplay', '-r', 'quit'
	])
	m = re.compile('R[0-9]{4}[A-Za-z]*[^\\s]*').search(out)
	if m is not None:
		return m.group(0)
	else:
		warnings.warn('MATLAB responded with output that does not appear to contain version information')
		return None
Ejemplo n.º 6
0
    def __windows_switch(self, i: int):
        def percent_to_real(percent: int) -> int:
            audio_max = 65535  # This is true of Windows in general, so not necessary to put in config
            # audio min is 0
            return round(audio_max * percent / 100)

        if self.output_device is not None:
            logger.debug("Setting audio output device to %s" %
                         self.output_device[i])
            wrap_cmd_call([
                'nircmd', 'setdefaultsounddevice',
                '%s' % self.output_device[i]
            ],
                          timeout_secs=self.timeout_secs)
        if self.input_device is not None:
            logger.debug("Setting audio input device to %s" %
                         self.input_device[i])
            wrap_cmd_call([
                'nircmd', 'setdefaultsounddevice',
                '%s' % self.input_device[i], '2'
            ],
                          timeout_secs=self.timeout_secs)
        if self.output_gain is not None:
            logger.debug("Setting system volume to configured default %s" %
                         self.output_gain[i])
            wrap_cmd_call([
                'nircmd', 'setsysvolume',
                '%s' % percent_to_real(self.output_gain[i]),
                self.output_device[i]
            ],
                          timeout_secs=self.timeout_secs)
        if self.input_gain is not None:
            logger.debug("Setting input gain to configured default %s" %
                         self.input_gain[i])
            wrap_cmd_call([
                'nircmd', 'setsysvolume',
                '%s' % percent_to_real(self.input_gain[i]),
                self.input_device[i]
            ],
                          timeout_secs=self.timeout_secs)