"""Exercise the exported video, frame order, and forward audio with FFmpeg.""" from array import array from pathlib import Path import shutil import subprocess import sys import tempfile import unittest sys.path.insert(0, str(Path(__file__).resolve().parents[1])) from reverse_tools import reverse_loop @unittest.skipUnless(shutil.which('ffmpeg') and shutil.which('ffprobe'), 'requires FFmpeg') class ReverseLoopTests(unittest.TestCase): @classmethod def setUpClass(cls): cls.temp = tempfile.TemporaryDirectory() cls.root = Path(cls.temp.name) cls.source = cls.root / 'h3-six.mp4' subprocess.run(['ffmpeg', '-nostdin', '-loglevel', 'error', '-f', 'lavfi', '-i', 'testsrc2=size=320x180:rate=24', '-f', 'lavfi', '-i', 'sine=frequency=440:sample_rate=48000:duration=5', '-f', 'lavfi', '-i', 'sine=frequency=880:sample_rate=48000:duration=1', '-filter_complex', '[1:a][2:a]concat=n=2:v=0:a=1[a]', '-map', '0:v', '-map', '[a]', '-t', '6', '-c:v', 'libx264', '-crf', '0', '-pix_fmt', 'yuv420p', '-c:a', 'aac', str(cls.source)], check=True) cls.silent = cls.root / 'h3-silent.mp4' subprocess.run(['ffmpeg', '-nostdin', '-loglevel', 'error', '-i', str(cls.source), '-an', '-c:v', 'copy', str(cls.silent)], check=True) cls.original_bytes = cls.source.read_bytes() @classmethod def tearDownClass(cls): cls.temp.cleanup() def frame(self, path, index): return subprocess.check_output(['ffmpeg', '-nostdin', '-loglevel', 'error', '-i', str(path), '-vf', f'select=eq(n\\,{index})', '-frames:v', '1', '-f', 'rawvideo', '-pix_fmt', 'rgb24', '-']) def duration(self, path): return float(subprocess.check_output(['ffprobe', '-v', 'error', '-show_entries', 'format=duration', '-of', 'default=noprint_wrappers=1:nokey=1', str(path)])) def test_two_and_three_passes_have_correct_frames_and_forward_audio(self): for passes, include in [(2, False), (3, False), (3, True)]: with self.subTest(passes=passes, include=include): output = reverse_loop(self.root, self.source.name, 4, passes, include) self.assertAlmostEqual(self.duration(output), 2*passes + (6 if include else 0), delta=.06) offset = 144 if include else 0 if include: self.assertLess(sum(abs(a-b) for a,b in zip(self.frame(output, 0),self.frame(self.source, 0))) / len(self.frame(output, 0)), 5) for p in range(passes): for i in (0, 47): source_index = 143-i if p % 2 == 0 else 96+i actual, wanted = self.frame(output, offset + 48*p + i), self.frame(self.source, source_index) self.assertEqual(len(actual), len(wanted)) self.assertLess(sum(abs(a-b) for a,b in zip(actual,wanted))/len(actual), 5) pcm = array('f', subprocess.check_output(['ffmpeg', '-v', 'error', '-i', str(output), '-map', '0:a:0', '-ac', '1', '-ar', '48000', '-f', 'f32le', '-'])) for p in range(passes): for second, expected in ((.25, 440), (1.25, 880)): start = int(((6 if include else 0) + 2*p + second)*48000) samples = pcm[start:start+12000] self.assertEqual(len(samples), 12000) hz = sum((a < 0) != (b < 0) for a,b in zip(samples,samples[1:]))*2 self.assertAlmostEqual(hz, expected, delta=25) self.assertEqual(self.source.read_bytes(), self.original_bytes) def test_silent_and_invalid_inputs(self): output = reverse_loop(self.root, self.silent.name, 4, 3, True) self.assertAlmostEqual(self.duration(output), 12, delta=.06) for name, point, passes, include in [('../h3-six.mp4',4,2,False), ('h3-six.mp4',6,2,False), ('h3-six.mp4',4,0,False), ('h3-six.mp4',4,11,False), ('h3-six.mp4',float('nan'),2,False)]: with self.subTest(name=name, point=point, passes=passes): with self.assertRaises(ValueError): reverse_loop(self.root, name, point, passes, include) if __name__ == '__main__': unittest.main()