Skip to content

Codecs

Info

Most of the parameters set for the codecs are those established by default in ffmpeg official docs. However, some parameters have been modified in order to achieve an improvement in performance.

# HLS default constants
# https://developer.apple.com/documentation/http-live-streaming/hls-authoring-specification-for-apple-devices
HLS_TIME = 10
HLS_LIST_SIZE = 0
HLS_TAG_VIDEO_FORMAT = 'hvc1'
HLS_PLAYLIST_TYPE = 'vod'


DEFAULT_AUDIO_CODEC = 'aac'
# The range of the CRF scale is 0–51, where 0 is lossless (higher quality)
DEFAULT_CRF = 0
# The preset determines compression efficiency and therefore affects encoding speed
# This option itemizes a range of choices from veryfast (best speed) to
# veryslow (best quality).
DEFAULT_PRESET = 'medium'
# keyframes minimum every 100 frames
DEFAULT_KEY_MIN = 100
# maximum amount of GOP size, maximum every 100 frames there will be a
# keyframe, together with -keyint_min this gives a keyframe every 100
# frames
DEFAULT_GOP = 100
# ffmpeg has scene detection. 0 (stands for false)
DEFAULT_SC_THRESHOLD = 0

Copy

Used to copy the codec from the source to the output. Special value copy (output only) to indicate that the stream is not to be re-encoded.

Usage:

copy_audio_stream = Copy("a")
copy_video_stream = Copy("v")
Source code in nucleus/sdk/processing/video/codecs.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Copy:
    """Used to `copy` the codec from the source to the output.
    Special value copy (output only) to indicate that the stream is not to be re-encoded.

    Usage:

        copy_audio_stream = Copy("a")
        copy_video_stream = Copy("v")
    """

    _stream_specifier: str

    def __init__(self, stream: Literal['v', 'a'] = 'v'):  # noqa: F821
        self._stream_specifier = stream

    def __contains__(self, codec: str) -> bool:
        ...

    def __iter__(self):
        yield f'c:{self._stream_specifier}', 'copy'

H264

Represents a H264 codec.

Usage:

h264 = H264()
Source code in nucleus/sdk/processing/video/codecs.py
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class H264:
    """Represents a H264 codec.

    Usage:

        h264 = H264()

    """

    def __contains__(self, codec: str) -> bool:
        videos = ['libx264', 'h264', 'h264_afm', 'h264_nvenc']
        audios = ['aac', 'libvo_aacenc', 'libfaac', 'libmp3lame', 'libfdk_aac']
        allowed_codecs = videos + audios
        return codec in allowed_codecs

    def __iter__(self):
        yield 'bf', 1
        yield 'g', DEFAULT_GOP
        yield 'crf', DEFAULT_CRF
        yield 'keyint_min', DEFAULT_KEY_MIN
        yield 'sc_threshold', DEFAULT_SC_THRESHOLD
        yield 'c:a', DEFAULT_AUDIO_CODEC
        yield 'c:v', 'libx264'

HEVC

Represents a HEVC codec.

Usage:

hevc = HEVC()
Source code in nucleus/sdk/processing/video/codecs.py
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
class HEVC:
    """Represents a HEVC codec.

    Usage:

        hevc = HEVC()

    """

    def __contains__(self, codec: str) -> bool:
        videos = ['libx265', 'h265']
        audios = ['aac', 'libvo_aacenc', 'libfaac', 'libmp3lame', 'libfdk_aac']
        allowed_codecs = videos + audios
        return codec in allowed_codecs

    def __iter__(self):
        yield 'g', DEFAULT_GOP
        yield 'crf', DEFAULT_CRF
        yield 'keyint_min', DEFAULT_KEY_MIN
        yield 'sc_threshold', DEFAULT_SC_THRESHOLD
        yield 'c:a', DEFAULT_AUDIO_CODEC
        yield 'c:v', 'libx265'
        yield 'x265-params', 'lossless=1'

VP9

Represents a VP9 codec.

Usage:

vp9 = VP9()
Source code in nucleus/sdk/processing/video/codecs.py
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
class VP9:
    """Represents a VP9 codec.

    Usage:

        vp9 = VP9()

    """

    def __contains__(self, codec: str) -> bool:
        videos = ['libvpx', 'libvpx-vp9']
        audios = ['aac', 'libvo_aacenc', 'libfaac', 'libmp3lame', 'libfdk_aac']
        allowed_codecs = videos + audios
        return codec in allowed_codecs

    def __iter__(self):
        yield 'c:a', DEFAULT_AUDIO_CODEC
        yield 'c:v', 'libvpx-vp9'