def _assemble(self, audio, frame_pattern, output=None): if not output: output = os.path.join(self.tmp_dir, "output.avi") try: # Try to be compatible as much as possible with old ffmpeg releases (>= 0.7) # - Do not use new syntax options # - Do not use libx264, not available on old Ubuntu/Debian # - Do not use -threads auto, not available on 0.8.* # - Old releases are very picky regarding arguments position # # 0.5 (Debian Squeeze & Ubuntu 10.4) is not supported because of # scaling issues with image2. cmd = [ self.ffmpeg, "-v", "0", "-i", audio, "-f", "image2", "-r", "1", "-s", "hd720","-i", frame_pattern, "-map", "1:0", "-acodec", "libmp3lame", "-ab", "128k", "-map", "0:1", "-vcodec", "mpeg4", "-vb", "2M", output ] utils.check_output(cmd, stderr=subprocess.STDOUT) except subprocess.CalledProcessError as e: raise Exception("Failed to create final movie as %s.\n" "\tExit code: %s\n" "\tOutput:\n%s" % (output, e.returncode, e.output)) return output
def download_video_no_cache(self, output_path=None): """Downloads the video. Args: output_path: Where to save the video. A file in temporary directory is used if None. Returns: The path where the video has been saved. Raises: DownloadFailedException: If the video cannot be downloaded. """ video_url = self.presentation.metadata['video_url'] video_path = self.presentation.metadata['video_path'] if not output_path: output_path = self._video_path try: cmd = [self.rtmpdump, '-q', '-r', video_url, '-y', video_path, "-o", output_path] utils.check_output(cmd, stderr=subprocess.STDOUT) except subprocess.CalledProcessError as e: try: os.unlink(output_path) except OSError: pass raise client.DownloadError("Failed to download video at %s: rtmpdump exited with %s" % (video_url, e.returncode)) return output_path
def test_download(self): tmp_dir = tempfile.mkdtemp() output_path = os.path.join(tmp_dir, "output.avi") cmd = self.build_download_cmd([short_presentation_id, '-o', output_path]) output = utils.check_output(cmd, stderr=subprocess.STDOUT) self.assertTrue(os.path.exists(output_path)) shutil.rmtree(tmp_dir)
def test_pattern(self): infoq_client = client.InfoQ(cache_enabled=True) summary = presentation.get_summaries(infoq_client).next() cmd = self.build_list_cmd(['-p', summary['title']]) output = utils.check_output(cmd, stderr=subprocess.STDOUT) self.assertEqual(output.count("Id: "), 1)
def test_duplicates(self): # Try to spot bugs in the summary fetcher. # Sometimes the same summary is returned several times cmd = self.build_list_cmd(['-n', '30', '-s']) output = utils.check_output(cmd, stderr=subprocess.STDOUT).strip() ids = output.split('\n') id_set = set(ids) self.assertEqual(len(ids), len(id_set))
def assert_bad_command(self, args): cmd = self.build_download_cmd(args) try: output = utils.check_output(cmd, stderr=subprocess.STDOUT) self.fail("Exception expected") except subprocess.CalledProcessError as e: self.assertEqual(e.returncode, 2) self.assertTrue(e.output.startswith(usage_prefix))
def test_no_arg(self): cmd = self.build_cmd([]) try: output = utils.check_output(cmd, stderr=subprocess.STDOUT) self.fail("Exception expected") except subprocess.CalledProcessError as e: self.assertEqual(e.returncode, 2) print e.output self.assertTrue(e.output.startswith(usage_prefix))
def test_clear(self): # Ensure there is at least one file in the cache dir infoq_client = client.InfoQ(cache_enabled=True) infoq_client.cache.put_content("testfile", "content") # Backup the cache dir backup_dir = infoq_client.cache.dir tmp_dir = os.path.join(tempfile.mkdtemp(), os.path.basename(backup_dir)) shutil.copytree(backup_dir, tmp_dir) try: cmd = self.build_clear_cmd([]) utils.check_output(cmd, stderr=subprocess.STDOUT) self.assertFalse(os.path.exists(backup_dir)) # Now restore the cache dir shutil.copytree(tmp_dir, backup_dir) finally: shutil.rmtree(os.path.dirname(tmp_dir))
def test_custom_ffmpeg(self): if sys.platform.startswith("win32"): # TODO: Need to find a way to create an alias on win32 return ffmpeg_path = utils.check_output(["which", "ffmpeg"]).strip() tmp_dir = tempfile.mkdtemp() try: alias_path = os.path.join(tmp_dir, "ffmpeg") print ffmpeg_path os.symlink(ffmpeg_path, alias_path) output_path = os.path.join(tmp_dir, "output.avi") cmd = self.build_download_cmd([short_presentation_id, '-o', output_path]) output = utils.check_output(cmd, stderr=subprocess.STDOUT) self.assertTrue(os.path.exists(output_path)) finally: shutil.rmtree(tmp_dir)
def test_help(self): cmd = self.build_download_cmd(['--help']) output = utils.check_output(cmd, stderr=subprocess.STDOUT) self.assertTrue(output.startswith(usage_prefix))
def test_error(self): try: with open(os.devnull, "w") as fnull: utils.check_output(["python", "--foo"], stderr=subprocess.STDOUT) except subprocess.CalledProcessError as e: self.assertEquals(e.returncode, 2)
def test_max_hit(self): cmd = self.build_list_cmd(['-n', '1']) output = utils.check_output(cmd, stderr=subprocess.STDOUT) self.assertEqual(output.count("Id: "), 1)
def test_size(self): # TODO: Find a better test # We could use du -sh then compare its output to our. cmd = self.build_size_cmd([]) output = utils.check_output(cmd, stderr=subprocess.STDOUT).strip() self.assertIsNotNone(re.match('\d{1,4}\.\d{2} \w{2,5}', output))
def test_short_output(self): cmd = self.build_list_cmd(['-s']) output = utils.check_output(cmd, stderr=subprocess.STDOUT) self.assertEqual(len(output.strip().split("\n")), 10)
def test_max_pages(self): cmd = self.build_list_cmd(['-m', '1']) output = utils.check_output(cmd, stderr=subprocess.STDOUT) self.assertEqual(output.count("Id: "), presentation._RightBarPage.ENTRIES_PER_PAGES)
def test_ok(self): utils.check_output(["python", "-h"])
def test_no_arg(self): cmd = self.build_list_cmd([]) output = utils.check_output(cmd, stderr=subprocess.STDOUT) self.assertEqual(output.count("Id: "), 10)