From ee339e632e729a67b89caca440e548a316169441 Mon Sep 17 00:00:00 2001 From: WyattBlue Date: Fri, 12 Jul 2024 02:41:22 -0400 Subject: [PATCH] Add default codec properties --- av/container/output.pyi | 6 ++++++ av/container/output.pyx | 23 +++++++++++++++++++++++ av/format.pyi | 4 ++++ tests/test_containerformat.py | 26 +++++++++++++++++++++----- 4 files changed, 54 insertions(+), 5 deletions(-) diff --git a/av/container/output.pyi b/av/container/output.pyi index 6df32c897..882592667 100644 --- a/av/container/output.pyi +++ b/av/container/output.pyi @@ -19,3 +19,9 @@ class OutputContainer(Container): def close(self) -> None: ... def mux(self, packets: Packet | Sequence[Packet]) -> None: ... def mux_one(self, packet: Packet) -> None: ... + @property + def default_video_codec(self) -> str: ... + @property + def default_audio_codec(self) -> str: ... + @property + def default_subtitle_codec(self) -> str: ... diff --git a/av/container/output.pyx b/av/container/output.pyx index 55e8b5006..184a30b2d 100644 --- a/av/container/output.pyx +++ b/av/container/output.pyx @@ -1,6 +1,8 @@ import logging import os +cimport libav as lib + from av.codec.codec cimport Codec from av.codec.context cimport CodecContext, wrap_codec_context from av.container.streams cimport StreamContainer @@ -192,6 +194,27 @@ cdef class OutputContainer(Container): self._started = True + @property + def default_video_codec(self): + """ + Returns the default video codec this container recommends. + """ + return lib.avcodec_get_name(self.format.optr.video_codec) + + @property + def default_audio_codec(self): + """ + Returns the default audio codec this container recommends. + """ + return lib.avcodec_get_name(self.format.optr.audio_codec) + + @property + def default_subtitle_codec(self): + """ + Returns the default subtitle codec this container recommends. + """ + return lib.avcodec_get_name(self.format.optr.subtitle_codec) + def close(self): for stream in self.streams: if stream.codec_context: diff --git a/av/format.pyi b/av/format.pyi index d2aef4764..c3506ed51 100644 --- a/av/format.pyi +++ b/av/format.pyi @@ -1,5 +1,7 @@ __all__ = ("ContainerFormat", "formats_available") +from typing import Literal + from .enum import EnumFlag class Flags(EnumFlag): @@ -22,10 +24,12 @@ class Flags(EnumFlag): SEEK_TO_PTS: int class ContainerFormat: + def __init__(self, name: str, mode: Literal["r", "w"] | None = None) -> None: ... name: str long_name: str is_input: bool is_output: bool + extensions: set[str] # flags no_file: int diff --git a/tests/test_containerformat.py b/tests/test_containerformat.py index dea3d29dc..f0460b131 100644 --- a/tests/test_containerformat.py +++ b/tests/test_containerformat.py @@ -1,10 +1,15 @@ -from av import ContainerFormat, formats_available +from av import ContainerFormat, formats_available, open from .common import TestCase class TestContainerFormats(TestCase): - def test_matroska(self): + def test_matroska(self) -> None: + with open("test.mkv", "w") as container: + self.assertNotEqual(container.default_video_codec, "none") + self.assertNotEqual(container.default_audio_codec, "none") + self.assertEqual(container.default_subtitle_codec, "ass") + fmt = ContainerFormat("matroska") self.assertTrue(fmt.is_input) self.assertTrue(fmt.is_output) @@ -13,7 +18,12 @@ def test_matroska(self): self.assertIn("mkv", fmt.extensions) self.assertFalse(fmt.no_file) - def test_mov(self): + def test_mov(self) -> None: + with open("test.mov", "w") as container: + self.assertNotEqual(container.default_video_codec, "none") + self.assertNotEqual(container.default_audio_codec, "none") + self.assertEqual(container.default_subtitle_codec, "none") + fmt = ContainerFormat("mov") self.assertTrue(fmt.is_input) self.assertTrue(fmt.is_output) @@ -22,7 +32,13 @@ def test_mov(self): self.assertIn("mov", fmt.extensions) self.assertFalse(fmt.no_file) - def test_stream_segment(self): + def test_gif(self) -> None: + with open("test.gif", "w") as container: + self.assertEqual(container.default_video_codec, "gif") + self.assertEqual(container.default_audio_codec, "none") + self.assertEqual(container.default_subtitle_codec, "none") + + def test_stream_segment(self) -> None: # This format goes by two names, check both. fmt = ContainerFormat("stream_segment") self.assertFalse(fmt.is_input) @@ -40,5 +56,5 @@ def test_stream_segment(self): self.assertEqual(fmt.extensions, set()) self.assertTrue(fmt.no_file) - def test_formats_available(self): + def test_formats_available(self) -> None: self.assertTrue(formats_available)