diff --git a/tests/test_container.py b/tests/test_container.py deleted file mode 100644 index 5aef83d116d7dbcc0e35a590197d9e7400422711..0000000000000000000000000000000000000000 --- a/tests/test_container.py +++ /dev/null @@ -1,29 +0,0 @@ -import sys -import unittest - -import av - -from .common import TestCase, fate_suite, is_windows, skip_tests - - -# On Windows, Python 3.0 - 3.5 have issues handling unicode filenames. -# Starting with Python 3.6 the situation is saner thanks to PEP 529: -# -# https://www.python.org/dev/peps/pep-0529/ - -broken_unicode = is_windows and sys.version_info < (3, 6) - - -class TestContainers(TestCase): - def test_context_manager(self): - with av.open(fate_suite("h264/interlaced_crop.mp4")) as container: - self.assertEqual(container.format.long_name, "QuickTime / MOV") - self.assertEqual(len(container.streams), 1) - - @unittest.skipIf( - broken_unicode or "unicode_filename" in skip_tests, - "Unicode filename handling is broken", - ) - def test_unicode_filename(self): - - av.open(self.sandboxed("¢∞§¶•ªº.mov"), "w") diff --git a/tests/test_encode.py b/tests/test_encode.py index ab963a72f57636aa80c8e9bd78e89d4e3b6430c1..e18c34330d88113e75197fc902eac7bbea3e8068 100644 --- a/tests/test_encode.py +++ b/tests/test_encode.py @@ -62,9 +62,6 @@ def write_rgb_rotate(output): for packet in stream.encode(None): output.mux(packet) - # Done! - output.close() - def assert_rgb_rotate(self, input_): @@ -87,157 +84,161 @@ def assert_rgb_rotate(self, input_): class TestBasicVideoEncoding(TestCase): - def test_rgb_rotate(self): - + def test_default_options(self): + with av.open(self.sandboxed("output.mov"), "w") as output: + stream = output.add_stream("mpeg4") + self.assertEqual(stream.bit_rate, 1024000) + self.assertEqual(stream.format.height, 480) + self.assertEqual(stream.format.name, "yuv420p") + self.assertEqual(stream.format.width, 640) + self.assertEqual(stream.height, 480) + self.assertEqual(stream.pix_fmt, "yuv420p") + self.assertEqual(stream.rate, Fraction(24, 1)) + self.assertEqual(stream.ticks_per_frame, 1) + self.assertEqual(stream.time_base, None) + self.assertEqual(stream.width, 640) + + def test_encoding(self): path = self.sandboxed("rgb_rotate.mov") - output = av.open(path, "w") - write_rgb_rotate(output) - assert_rgb_rotate(self, av.open(path)) + with av.open(path, "w") as output: + write_rgb_rotate(output) + with av.open(path) as input: + assert_rgb_rotate(self, input) def test_encoding_with_pts(self): - path = self.sandboxed("video_with_pts.mov") - output = av.open(path, "w") - stream = output.add_stream("libx264", 24) - stream.width = WIDTH - stream.height = HEIGHT - stream.pix_fmt = "yuv420p" + with av.open(path, "w") as output: + stream = output.add_stream("libx264", 24) + stream.width = WIDTH + stream.height = HEIGHT + stream.pix_fmt = "yuv420p" + + for i in range(DURATION): + frame = VideoFrame(WIDTH, HEIGHT, "rgb24") + frame.pts = i * 2000 + frame.time_base = Fraction(1, 48000) - for i in range(DURATION): - frame = VideoFrame(WIDTH, HEIGHT, "rgb24") - frame.pts = i * 2000 - frame.time_base = Fraction(1, 48000) + for packet in stream.encode(frame): + self.assertEqual(packet.time_base, Fraction(1, 24)) + output.mux(packet) - for packet in stream.encode(frame): + for packet in stream.encode(None): self.assertEqual(packet.time_base, Fraction(1, 24)) output.mux(packet) - for packet in stream.encode(None): - self.assertEqual(packet.time_base, Fraction(1, 24)) - output.mux(packet) + def test_encoding_with_unicode_filename(self): + path = self.sandboxed("¢∞§¶•ªº.mov") - output.close() + with av.open(path, "w") as output: + write_rgb_rotate(output) + with av.open(path) as input: + assert_rgb_rotate(self, input) class TestBasicAudioEncoding(TestCase): - def test_audio_transcode(self): - + def test_default_options(self): + with av.open(self.sandboxed("output.mov"), "w") as output: + stream = output.add_stream("mp2") + self.assertEqual(stream.bit_rate, 128000) + self.assertEqual(stream.format.name, "s16") + self.assertEqual(stream.rate, 48000) + self.assertEqual(stream.ticks_per_frame, 1) + self.assertEqual(stream.time_base, None) + + def test_transcode(self): path = self.sandboxed("audio_transcode.mov") - output = av.open(path, "w") - output.metadata["title"] = "container" - output.metadata["key"] = "value" - - sample_rate = 48000 - channel_layout = "stereo" - channels = 2 - sample_fmt = "s16" - - stream = output.add_stream("mp2", sample_rate) - - ctx = stream.codec_context - ctx.time_base = sample_rate - ctx.sample_rate = sample_rate - ctx.format = sample_fmt - ctx.layout = channel_layout - ctx.channels = channels - - src = av.open(fate_suite("audio-reference/chorusnoise_2ch_44kHz_s16.wav")) - for frame in src.decode(audio=0): - for packet in stream.encode(frame): - output.mux(packet) - for packet in stream.encode(None): - output.mux(packet) + with av.open(path, "w") as output: + output.metadata["title"] = "container" + output.metadata["key"] = "value" - output.close() + sample_rate = 48000 + channel_layout = "stereo" + channels = 2 + sample_fmt = "s16" - container = av.open(path) - self.assertEqual(len(container.streams), 1) - self.assertEqual( - container.metadata.get("title"), "container", container.metadata - ) - self.assertEqual(container.metadata.get("key"), None) + stream = output.add_stream("mp2", sample_rate) - stream = container.streams[0] - self.assertIsInstance(stream, AudioStream) - self.assertEqual(stream.codec_context.sample_rate, sample_rate) - self.assertEqual(stream.codec_context.format.name, "s16p") - self.assertEqual(stream.codec_context.channels, channels) + ctx = stream.codec_context + ctx.time_base = sample_rate + ctx.sample_rate = sample_rate + ctx.format = sample_fmt + ctx.layout = channel_layout + ctx.channels = channels + with av.open( + fate_suite("audio-reference/chorusnoise_2ch_44kHz_s16.wav") + ) as src: + for frame in src.decode(audio=0): + for packet in stream.encode(frame): + output.mux(packet) -class TestEncodeStreamSemantics(TestCase): - def test_audio_default_options(self): - output = av.open(self.sandboxed("output.mov"), "w") - - stream = output.add_stream("mp2") - self.assertEqual(stream.bit_rate, 128000) - self.assertEqual(stream.format.name, "s16") - self.assertEqual(stream.rate, 48000) - self.assertEqual(stream.ticks_per_frame, 1) - self.assertEqual(stream.time_base, None) - - def test_video_default_options(self): - output = av.open(self.sandboxed("output.mov"), "w") - - stream = output.add_stream("mpeg4") - self.assertEqual(stream.bit_rate, 1024000) - self.assertEqual(stream.format.height, 480) - self.assertEqual(stream.format.name, "yuv420p") - self.assertEqual(stream.format.width, 640) - self.assertEqual(stream.height, 480) - self.assertEqual(stream.pix_fmt, "yuv420p") - self.assertEqual(stream.rate, Fraction(24, 1)) - self.assertEqual(stream.ticks_per_frame, 1) - self.assertEqual(stream.time_base, None) - self.assertEqual(stream.width, 640) + for packet in stream.encode(None): + output.mux(packet) + + with av.open(path) as container: + self.assertEqual(len(container.streams), 1) + self.assertEqual( + container.metadata.get("title"), "container", container.metadata + ) + self.assertEqual(container.metadata.get("key"), None) + stream = container.streams[0] + self.assertIsInstance(stream, AudioStream) + self.assertEqual(stream.codec_context.sample_rate, sample_rate) + self.assertEqual(stream.codec_context.format.name, "s16p") + self.assertEqual(stream.codec_context.channels, channels) + + +class TestEncodeStreamSemantics(TestCase): def test_stream_index(self): - output = av.open(self.sandboxed("output.mov"), "w") - - vstream = output.add_stream("mpeg4", 24) - vstream.pix_fmt = "yuv420p" - vstream.width = 320 - vstream.height = 240 - - astream = output.add_stream("mp2", 48000) - astream.channels = 2 - astream.format = "s16" - - self.assertEqual(vstream.index, 0) - self.assertEqual(astream.index, 1) - - vframe = VideoFrame(320, 240, "yuv420p") - vpacket = vstream.encode(vframe)[0] - - self.assertIs(vpacket.stream, vstream) - self.assertEqual(vpacket.stream_index, 0) - - for i in range(10): - if astream.frame_size != 0: - frame_size = astream.frame_size - else: - # decoder didn't indicate constant frame size - frame_size = 1000 - aframe = AudioFrame("s16", "stereo", samples=frame_size) - aframe.rate = 48000 - apackets = astream.encode(aframe) - if apackets: - apacket = apackets[0] - break - - self.assertIs(apacket.stream, astream) - self.assertEqual(apacket.stream_index, 1) - - def test_audio_set_time_base_and_id(self): - output = av.open(self.sandboxed("output.mov"), "w") - - stream = output.add_stream("mp2") - self.assertEqual(stream.rate, 48000) - self.assertEqual(stream.time_base, None) - stream.time_base = Fraction(1, 48000) - self.assertEqual(stream.time_base, Fraction(1, 48000)) - self.assertEqual(stream.id, 0) - stream.id = 1 - self.assertEqual(stream.id, 1) + with av.open(self.sandboxed("output.mov"), "w") as output: + vstream = output.add_stream("mpeg4", 24) + vstream.pix_fmt = "yuv420p" + vstream.width = 320 + vstream.height = 240 + + astream = output.add_stream("mp2", 48000) + astream.channels = 2 + astream.format = "s16" + + self.assertEqual(vstream.index, 0) + self.assertEqual(astream.index, 1) + + vframe = VideoFrame(320, 240, "yuv420p") + vpacket = vstream.encode(vframe)[0] + + self.assertIs(vpacket.stream, vstream) + self.assertEqual(vpacket.stream_index, 0) + + for i in range(10): + if astream.frame_size != 0: + frame_size = astream.frame_size + else: + # decoder didn't indicate constant frame size + frame_size = 1000 + aframe = AudioFrame("s16", "stereo", samples=frame_size) + aframe.rate = 48000 + apackets = astream.encode(aframe) + if apackets: + apacket = apackets[0] + break + + self.assertIs(apacket.stream, astream) + self.assertEqual(apacket.stream_index, 1) + + def test_set_id_and_time_base(self): + with av.open(self.sandboxed("output.mov"), "w") as output: + stream = output.add_stream("mp2") + + # set id + self.assertEqual(stream.id, 0) + stream.id = 1 + self.assertEqual(stream.id, 1) + + # set time_base + self.assertEqual(stream.time_base, None) + stream.time_base = Fraction(1, 48000) + self.assertEqual(stream.time_base, Fraction(1, 48000)) diff --git a/tests/test_python_io.py b/tests/test_python_io.py index 0716429c8b8cccefd40df5ecafc1ff0a34232122..13e49510395e49f3ea153fbc8f8885974a1dc130 100644 --- a/tests/test_python_io.py +++ b/tests/test_python_io.py @@ -87,14 +87,15 @@ class TestPythonIO(TestCase): ) def test_writing_to_buffer(self): - fh = BytesIO() + buf = BytesIO() - self.write(fh) + self.write(buf) # Check contents. - self.assertTrue(fh.tell()) - fh.seek(0) - assert_rgb_rotate(self, av.open(fh)) + self.assertTrue(buf.tell()) + buf.seek(0) + with av.open(buf) as container: + assert_rgb_rotate(self, container) def test_writing_to_file(self): path = self.sandboxed("writing.mp4")