raricode 0.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- raricode/__init__.py +36 -0
- raricode/__main__.py +4 -0
- raricode/click_custom/__init__.py +0 -0
- raricode/click_custom/mutually_exclusive.py +25 -0
- raricode/commands/__init__.py +0 -0
- raricode/commands/cropdetect.py +188 -0
- raricode/commands/ffmpeg_info.py +10 -0
- raricode/commands/targetsize.py +0 -0
- raricode/commands/transcode.py +134 -0
- raricode/exception.py +2 -0
- raricode/ffmpeg/__init__.py +0 -0
- raricode/ffmpeg/codec/__init__.py +0 -0
- raricode/ffmpeg/codec/audio.py +26 -0
- raricode/ffmpeg/codec/video.py +26 -0
- raricode/ffmpeg/cropdetect.py +3 -0
- raricode/ffmpeg/rate/__init__.py +0 -0
- raricode/ffmpeg/rate/audio.py +16 -0
- raricode/ffmpeg/rate/video.py +56 -0
- raricode/ffmpeg/runner.py +50 -0
- raricode/utils/__init__.py +0 -0
- raricode/utils/block_printer.py +128 -0
- raricode/utils/ctrlseq.py +58 -0
- raricode/utils/cursor.py +151 -0
- raricode/utils/logging.py +36 -0
- raricode/utils/rate.py +69 -0
- raricode-0.0.1.dist-info/METADATA +29 -0
- raricode-0.0.1.dist-info/RECORD +30 -0
- raricode-0.0.1.dist-info/WHEEL +4 -0
- raricode-0.0.1.dist-info/entry_points.txt +3 -0
- raricode-0.0.1.dist-info/licenses/LICENSE +201 -0
raricode/__init__.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
|
|
3
|
+
import click
|
|
4
|
+
from shelltastic import shell
|
|
5
|
+
|
|
6
|
+
from raricode.commands.cropdetect import cropdetect
|
|
7
|
+
from raricode.commands.ffmpeg_info import ffmpeginfo
|
|
8
|
+
from raricode.commands.transcode import transcode
|
|
9
|
+
from raricode.exception import RaricodeException
|
|
10
|
+
from raricode.utils.logging import setup_logging
|
|
11
|
+
|
|
12
|
+
LOGGER = logging.getLogger(__name__)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@click.group()
|
|
16
|
+
@click.version_option()
|
|
17
|
+
@click.option("--verbose", "-v", count=True, help="Turn on verbose logging")
|
|
18
|
+
def main_command(verbose: int):
|
|
19
|
+
# Setup logging
|
|
20
|
+
setup_logging(verbose)
|
|
21
|
+
|
|
22
|
+
# Ensure ffmpeg is available on the path
|
|
23
|
+
ffmpeg_bin = shell.which("ffmpeg")
|
|
24
|
+
if ffmpeg_bin:
|
|
25
|
+
LOGGER.debug("ffmpeg bin: %s", ffmpeg_bin)
|
|
26
|
+
else:
|
|
27
|
+
raise RaricodeException("ffmpeg is not on the path")
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
main_command.command(transcode)
|
|
31
|
+
main_command.command(cropdetect)
|
|
32
|
+
main_command.command(ffmpeginfo)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def main() -> None:
|
|
36
|
+
main_command()
|
raricode/__main__.py
ADDED
|
File without changes
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
from click import Option, UsageError
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class MutuallyExclusiveOption(Option):
|
|
5
|
+
def __init__(self, *args, **kwargs):
|
|
6
|
+
self.mutually_exclusive = set(kwargs.pop("mutually_exclusive", []))
|
|
7
|
+
help = kwargs.get("help", "")
|
|
8
|
+
if self.mutually_exclusive:
|
|
9
|
+
ex_str = ", ".join(self.mutually_exclusive)
|
|
10
|
+
kwargs["help"] = help + (
|
|
11
|
+
" NOTE: This argument is mutually exclusive with "
|
|
12
|
+
" arguments: [" + ex_str + "]."
|
|
13
|
+
)
|
|
14
|
+
super().__init__(*args, **kwargs)
|
|
15
|
+
|
|
16
|
+
def handle_parse_result(self, ctx, opts, args):
|
|
17
|
+
print(opts)
|
|
18
|
+
if self.mutually_exclusive.intersection(opts) and self.name in opts:
|
|
19
|
+
raise UsageError(
|
|
20
|
+
"Illegal usage: `{}` is mutually exclusive with arguments `{}`.".format(
|
|
21
|
+
self.name, ", ".join(self.mutually_exclusive)
|
|
22
|
+
)
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
return super().handle_parse_result(ctx, opts, args)
|
|
File without changes
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import functools
|
|
2
|
+
import logging
|
|
3
|
+
import sys
|
|
4
|
+
import time
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
import click
|
|
9
|
+
from shelltastic import CaptureMode, shell
|
|
10
|
+
|
|
11
|
+
from raricode.utils.block_printer import BlockPrinter
|
|
12
|
+
from raricode.utils.rate import RateRingBuffer
|
|
13
|
+
|
|
14
|
+
LOGGER = logging.getLogger(__name__)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class CropDetectDetails:
|
|
18
|
+
def __init__(self, crop_detect_line: str) -> None:
|
|
19
|
+
if not crop_detect_line.startswith("[Parsed_cropdetect"):
|
|
20
|
+
self.is_valid = False
|
|
21
|
+
split_line = None
|
|
22
|
+
else:
|
|
23
|
+
self.is_valid = True
|
|
24
|
+
split_line = crop_detect_line.split()
|
|
25
|
+
|
|
26
|
+
self.x1: int = int(split_line[3].split(":")[1]) if split_line else 0
|
|
27
|
+
self.x2: int = int(split_line[4].split(":")[1]) if split_line else 0
|
|
28
|
+
self.y1: int = int(split_line[5].split(":")[1]) if split_line else 0
|
|
29
|
+
self.y2: int = int(split_line[6].split(":")[1]) if split_line else 0
|
|
30
|
+
self.w: int = int(split_line[7].split(":")[1]) if split_line else 0
|
|
31
|
+
self.h: int = int(split_line[8].split(":")[1]) if split_line else 0
|
|
32
|
+
self.x: int = int(split_line[9].split(":")[1]) if split_line else 0
|
|
33
|
+
self.y: int = int(split_line[10].split(":")[1]) if split_line else 0
|
|
34
|
+
self.pts: int = int(split_line[11].split(":")[1]) if split_line else 0
|
|
35
|
+
self.t: float = float(split_line[12].split(":")[1]) if split_line else 0
|
|
36
|
+
self.limit: float = float(split_line[13].split(":")[1]) if split_line else 0
|
|
37
|
+
self.crop: str = split_line[14].split("=")[1] if split_line else ""
|
|
38
|
+
|
|
39
|
+
def __str__(self) -> str:
|
|
40
|
+
return str(self.__dict__)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
@functools.total_ordering
|
|
44
|
+
@dataclass(slots=True, kw_only=True)
|
|
45
|
+
class CropStat:
|
|
46
|
+
crop: str
|
|
47
|
+
count: int
|
|
48
|
+
percentage: float
|
|
49
|
+
|
|
50
|
+
def __eq__(self, other: object) -> bool:
|
|
51
|
+
if not isinstance(other, CropStat):
|
|
52
|
+
return NotImplemented
|
|
53
|
+
return self.count == other.count
|
|
54
|
+
|
|
55
|
+
def __lt__(self, other):
|
|
56
|
+
if not isinstance(other, CropStat):
|
|
57
|
+
return NotImplemented
|
|
58
|
+
return self.count < other.count
|
|
59
|
+
|
|
60
|
+
def __str__(self) -> str:
|
|
61
|
+
return f"crop={self.crop} ({(self.percentage * 100):.1f}%): {self.count}"
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
class CropStats:
|
|
65
|
+
def __init__(self) -> None:
|
|
66
|
+
self.__stats: dict[str, int] = {}
|
|
67
|
+
self.total = 0
|
|
68
|
+
self._rate = RateRingBuffer(rate_period=10, bucket_window=1)
|
|
69
|
+
|
|
70
|
+
def add(self, crop: CropDetectDetails):
|
|
71
|
+
if crop.is_valid:
|
|
72
|
+
self.__stats[crop.crop] = (
|
|
73
|
+
self.__stats[crop.crop] + 1 if self.__stats.get(crop.crop) else 1
|
|
74
|
+
)
|
|
75
|
+
self.total += 1
|
|
76
|
+
self._rate.accumulate(1)
|
|
77
|
+
|
|
78
|
+
def stats(self, threshold_percentage: float | None = None):
|
|
79
|
+
if threshold_percentage is None:
|
|
80
|
+
threshold_percentage = 0
|
|
81
|
+
return [
|
|
82
|
+
CropStat(crop=k, count=v, percentage=v / self.total)
|
|
83
|
+
for k, v in self.__stats.items()
|
|
84
|
+
if v / self.total >= threshold_percentage
|
|
85
|
+
]
|
|
86
|
+
|
|
87
|
+
def rate(self) -> float:
|
|
88
|
+
return self._rate.rate()
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
@click.argument("video_file")
|
|
92
|
+
@click.option(
|
|
93
|
+
"--display-threshold",
|
|
94
|
+
default=10,
|
|
95
|
+
type=float,
|
|
96
|
+
help="Percentage threshold in order to display crop in printout.",
|
|
97
|
+
)
|
|
98
|
+
@click.option(
|
|
99
|
+
"--minframes",
|
|
100
|
+
default=2000,
|
|
101
|
+
type=int,
|
|
102
|
+
help="Minimum number of frames to check before accepting a crop setting",
|
|
103
|
+
)
|
|
104
|
+
@click.option(
|
|
105
|
+
"--threshold",
|
|
106
|
+
default=80,
|
|
107
|
+
type=float,
|
|
108
|
+
help="Percentage of frames that must match before accepting a crop setting",
|
|
109
|
+
)
|
|
110
|
+
def cropdetect(
|
|
111
|
+
video_file: str, threshold: float, minframes: int, display_threshold: float
|
|
112
|
+
):
|
|
113
|
+
"""
|
|
114
|
+
Detect the crop settings for a video.
|
|
115
|
+
|
|
116
|
+
By default, a crop setting is accepted as the correct setting if its
|
|
117
|
+
at least 80% of the frames checked after checking 2000 frames.
|
|
118
|
+
|
|
119
|
+
--threshold and --minframes can be used to configure this acceptance critera.
|
|
120
|
+
"""
|
|
121
|
+
video_file_path = Path(video_file)
|
|
122
|
+
display_threshold = (
|
|
123
|
+
display_threshold / 100
|
|
124
|
+
) # Convert from percentage to float percentage
|
|
125
|
+
threshold = threshold / 100 # Convert from percentage to float percentage
|
|
126
|
+
if video_file_path.exists():
|
|
127
|
+
print(f"{video_file_path}")
|
|
128
|
+
else:
|
|
129
|
+
LOGGER.error(f"{video_file_path} does not exists")
|
|
130
|
+
sys.exit(1)
|
|
131
|
+
|
|
132
|
+
with shell.run_async(
|
|
133
|
+
[
|
|
134
|
+
"ffmpeg",
|
|
135
|
+
"-i",
|
|
136
|
+
str(video_file_path),
|
|
137
|
+
"-vf",
|
|
138
|
+
"cropdetect=round=2",
|
|
139
|
+
"-an",
|
|
140
|
+
"-f",
|
|
141
|
+
"null",
|
|
142
|
+
"-",
|
|
143
|
+
],
|
|
144
|
+
stderr=CaptureMode.PIPE,
|
|
145
|
+
) as async_result:
|
|
146
|
+
assert async_result.stderr is not None
|
|
147
|
+
|
|
148
|
+
stats = CropStats()
|
|
149
|
+
last_print = time.perf_counter()
|
|
150
|
+
block_printer = BlockPrinter()
|
|
151
|
+
next_check_frames = minframes
|
|
152
|
+
|
|
153
|
+
print("----------------------------------------")
|
|
154
|
+
|
|
155
|
+
for line in async_result.stderr:
|
|
156
|
+
# Decode line
|
|
157
|
+
line_decoded = line.decode()
|
|
158
|
+
LOGGER.debug("%s", line_decoded)
|
|
159
|
+
crop_details = CropDetectDetails(line_decoded)
|
|
160
|
+
LOGGER.debug("%s", crop_details)
|
|
161
|
+
stats.add(crop_details)
|
|
162
|
+
|
|
163
|
+
if stats.total >= next_check_frames:
|
|
164
|
+
if stats.stats(threshold):
|
|
165
|
+
async_result.terminate()
|
|
166
|
+
break
|
|
167
|
+
next_check_frames += 50
|
|
168
|
+
|
|
169
|
+
if time.perf_counter() - last_print >= 0.1:
|
|
170
|
+
with block_printer.replace_block() as p:
|
|
171
|
+
p.print(f"Frames Checked: {stats.total} ({stats.rate():.1f}f/s)")
|
|
172
|
+
p.print("----------------------------------------")
|
|
173
|
+
for stat in sorted(stats.stats(display_threshold), reverse=True):
|
|
174
|
+
p.print(stat)
|
|
175
|
+
p.print("----------------------------------------")
|
|
176
|
+
last_print = time.perf_counter()
|
|
177
|
+
|
|
178
|
+
# Final Print
|
|
179
|
+
with block_printer.replace_block() as p:
|
|
180
|
+
p.print(f"Frames Checked: {stats.total} ({stats.rate():.1f}f/s)")
|
|
181
|
+
p.print("----------------------------------------")
|
|
182
|
+
for stat in sorted(stats.stats(display_threshold), reverse=True):
|
|
183
|
+
p.print(stat)
|
|
184
|
+
p.print("----------------------------------------")
|
|
185
|
+
|
|
186
|
+
# Print Result
|
|
187
|
+
detected_crop = max(stats.stats())
|
|
188
|
+
print(f"Detected Crop: {detected_crop.crop}")
|
|
File without changes
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
|
|
3
|
+
import click
|
|
4
|
+
|
|
5
|
+
from raricode.click_custom.mutually_exclusive import MutuallyExclusiveOption
|
|
6
|
+
from raricode.ffmpeg.codec.audio import AudioCodec
|
|
7
|
+
from raricode.ffmpeg.codec.video import VideoCodec
|
|
8
|
+
from raricode.ffmpeg.rate.audio import AudioBitrate
|
|
9
|
+
from raricode.ffmpeg.rate.video import VideoBitrate, VideoCrf
|
|
10
|
+
from raricode.ffmpeg.runner import FfmpegRunner
|
|
11
|
+
|
|
12
|
+
LOGGER = logging.getLogger(__name__)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@click.argument("output")
|
|
16
|
+
@click.option(
|
|
17
|
+
"-i",
|
|
18
|
+
"--input",
|
|
19
|
+
type=str,
|
|
20
|
+
required=True,
|
|
21
|
+
help="Input video file",
|
|
22
|
+
)
|
|
23
|
+
@click.option(
|
|
24
|
+
"--video-codec",
|
|
25
|
+
"-c:v",
|
|
26
|
+
type=str,
|
|
27
|
+
required=True,
|
|
28
|
+
help="Video codec",
|
|
29
|
+
)
|
|
30
|
+
@click.option(
|
|
31
|
+
"--video-flag",
|
|
32
|
+
"-f:v",
|
|
33
|
+
type=(str, str),
|
|
34
|
+
multiple=True,
|
|
35
|
+
help="Video codec flag",
|
|
36
|
+
)
|
|
37
|
+
@click.option(
|
|
38
|
+
"--audio-codec",
|
|
39
|
+
"-c:a",
|
|
40
|
+
type=str,
|
|
41
|
+
required=True,
|
|
42
|
+
help="Audio codec",
|
|
43
|
+
)
|
|
44
|
+
@click.option(
|
|
45
|
+
"--audio-flag",
|
|
46
|
+
"-f:a",
|
|
47
|
+
type=(str, str),
|
|
48
|
+
multiple=True,
|
|
49
|
+
help="Audio codec flag",
|
|
50
|
+
)
|
|
51
|
+
@click.option(
|
|
52
|
+
"--crf",
|
|
53
|
+
"video_crf",
|
|
54
|
+
cls=MutuallyExclusiveOption,
|
|
55
|
+
mutually_exclusive=["video_bitrate"],
|
|
56
|
+
type=int,
|
|
57
|
+
)
|
|
58
|
+
@click.option(
|
|
59
|
+
"-b:v",
|
|
60
|
+
"video_bitrate",
|
|
61
|
+
cls=MutuallyExclusiveOption,
|
|
62
|
+
mutually_exclusive=["video_crf"],
|
|
63
|
+
type=str,
|
|
64
|
+
)
|
|
65
|
+
@click.option(
|
|
66
|
+
"-b:a",
|
|
67
|
+
"audio_bitrate",
|
|
68
|
+
type=str,
|
|
69
|
+
)
|
|
70
|
+
@click.option("--dryrun", is_flag=True)
|
|
71
|
+
def transcode(
|
|
72
|
+
input: str,
|
|
73
|
+
output: str,
|
|
74
|
+
video_codec: str,
|
|
75
|
+
video_flag: tuple[tuple[str, str], ...],
|
|
76
|
+
video_crf: int | None,
|
|
77
|
+
video_bitrate: str | None,
|
|
78
|
+
audio_codec: str,
|
|
79
|
+
audio_flag: tuple[tuple[str, str], ...],
|
|
80
|
+
audio_bitrate: str | None,
|
|
81
|
+
dryrun: bool,
|
|
82
|
+
):
|
|
83
|
+
"""
|
|
84
|
+
Transcode a video using ffmpeg
|
|
85
|
+
|
|
86
|
+
Not meant to be a complete replacement for ffmpeg.
|
|
87
|
+
"""
|
|
88
|
+
print(f"input: {input}")
|
|
89
|
+
print(f"output: {output}")
|
|
90
|
+
print(f"video_codec: {video_codec}")
|
|
91
|
+
print(f"video_flag: {video_flag}")
|
|
92
|
+
print(f"video_crf: {video_crf}")
|
|
93
|
+
print(f"video_bitrate: {video_bitrate}")
|
|
94
|
+
print(f"audio_codec: {audio_codec}")
|
|
95
|
+
print(f"audio_flag: {audio_flag}")
|
|
96
|
+
print(f"audio_bitrate: {audio_bitrate}")
|
|
97
|
+
|
|
98
|
+
# Video configs
|
|
99
|
+
vcodec = VideoCodec(video_codec)
|
|
100
|
+
|
|
101
|
+
for flag, param in video_flag:
|
|
102
|
+
vcodec.set_flag(flag, param)
|
|
103
|
+
|
|
104
|
+
video_rate = None
|
|
105
|
+
|
|
106
|
+
if video_crf:
|
|
107
|
+
video_rate = VideoCrf(video_crf)
|
|
108
|
+
|
|
109
|
+
if video_bitrate:
|
|
110
|
+
video_rate = VideoBitrate(video_bitrate)
|
|
111
|
+
|
|
112
|
+
# Audio Configs
|
|
113
|
+
acodec = AudioCodec(audio_codec)
|
|
114
|
+
|
|
115
|
+
for flag, param in audio_flag:
|
|
116
|
+
acodec.set_flag(flag, param)
|
|
117
|
+
|
|
118
|
+
audio_rate = None
|
|
119
|
+
|
|
120
|
+
if audio_bitrate:
|
|
121
|
+
audio_rate = AudioBitrate(audio_bitrate)
|
|
122
|
+
|
|
123
|
+
# Run ffmpeg
|
|
124
|
+
runner = FfmpegRunner(
|
|
125
|
+
video_codec=vcodec,
|
|
126
|
+
audio_codec=acodec,
|
|
127
|
+
video_rate=video_rate,
|
|
128
|
+
audio_rate=audio_rate,
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
if dryrun:
|
|
132
|
+
runner.dry_run(input, output)
|
|
133
|
+
else:
|
|
134
|
+
runner.run(input, output)
|
raricode/exception.py
ADDED
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
class AudioCodec:
|
|
2
|
+
def __init__(self, codec: str) -> None:
|
|
3
|
+
self.codec = codec
|
|
4
|
+
self.flags: dict[str, str | None] = {}
|
|
5
|
+
|
|
6
|
+
def set_flag(self, flag: str, setting: str | None = None):
|
|
7
|
+
self.flags[flag] = setting
|
|
8
|
+
|
|
9
|
+
def remove_flag(self, flag: str):
|
|
10
|
+
del self.flags[flag]
|
|
11
|
+
|
|
12
|
+
def ffmpeg_codec_settings(self) -> list[str]:
|
|
13
|
+
settings = ["-c:a", self.codec]
|
|
14
|
+
|
|
15
|
+
for flag, setting in self.flags.items():
|
|
16
|
+
settings.append(flag)
|
|
17
|
+
if setting:
|
|
18
|
+
settings.append(setting)
|
|
19
|
+
|
|
20
|
+
return settings
|
|
21
|
+
|
|
22
|
+
def __str__(self) -> str:
|
|
23
|
+
return " ".join(self.ffmpeg_codec_settings())
|
|
24
|
+
|
|
25
|
+
def __repr__(self) -> str:
|
|
26
|
+
return f"VideoCodec({self.codec}, {self.flags})"
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
class VideoCodec:
|
|
2
|
+
def __init__(self, codec: str) -> None:
|
|
3
|
+
self.codec = codec
|
|
4
|
+
self.flags: dict[str, str | None] = {}
|
|
5
|
+
|
|
6
|
+
def set_flag(self, flag: str, setting: str | None = None):
|
|
7
|
+
self.flags[flag] = setting
|
|
8
|
+
|
|
9
|
+
def remove_flag(self, flag: str):
|
|
10
|
+
del self.flags[flag]
|
|
11
|
+
|
|
12
|
+
def ffmpeg_codec_settings(self) -> list[str]:
|
|
13
|
+
settings = ["-c:v", self.codec]
|
|
14
|
+
|
|
15
|
+
for flag, setting in self.flags.items():
|
|
16
|
+
settings.append(flag)
|
|
17
|
+
if setting:
|
|
18
|
+
settings.append(setting)
|
|
19
|
+
|
|
20
|
+
return settings
|
|
21
|
+
|
|
22
|
+
def __str__(self) -> str:
|
|
23
|
+
return " ".join(self.ffmpeg_codec_settings())
|
|
24
|
+
|
|
25
|
+
def __repr__(self) -> str:
|
|
26
|
+
return f"VideoCodec({self.codec}, {self.flags})"
|
|
File without changes
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
from abc import ABC, abstractmethod
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class AudioRate(ABC):
|
|
5
|
+
@abstractmethod
|
|
6
|
+
def rate_config(self) -> list[str]:
|
|
7
|
+
raise NotImplementedError
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class AudioBitrate(AudioRate):
|
|
11
|
+
def __init__(self, bitrate: str) -> None:
|
|
12
|
+
super().__init__()
|
|
13
|
+
self.bitrate = bitrate
|
|
14
|
+
|
|
15
|
+
def rate_config(self) -> list[str]:
|
|
16
|
+
return ["-b:a", self.bitrate]
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
from abc import ABC, abstractmethod
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class VideoRate(ABC):
|
|
5
|
+
@abstractmethod
|
|
6
|
+
def rate_config(self) -> list[str]:
|
|
7
|
+
raise NotImplementedError
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class VideoCrf(VideoRate):
|
|
11
|
+
def __init__(
|
|
12
|
+
self,
|
|
13
|
+
crf: int,
|
|
14
|
+
min_bitrate: str | None = None,
|
|
15
|
+
max_bitrate: str | None = None,
|
|
16
|
+
) -> None:
|
|
17
|
+
super().__init__()
|
|
18
|
+
self.crf = crf
|
|
19
|
+
self.min_bitrate = min_bitrate
|
|
20
|
+
self.max_bitrate = max_bitrate
|
|
21
|
+
|
|
22
|
+
def rate_config(self) -> list[str]:
|
|
23
|
+
rate_conf = ["-crf", str(self.crf)]
|
|
24
|
+
|
|
25
|
+
if self.min_bitrate:
|
|
26
|
+
rate_conf.extend(["-minrate", self.min_bitrate])
|
|
27
|
+
|
|
28
|
+
if self.max_bitrate:
|
|
29
|
+
rate_conf.extend(["-maxrate", self.max_bitrate])
|
|
30
|
+
|
|
31
|
+
return rate_conf
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class VideoBitrate(VideoRate):
|
|
35
|
+
def __init__(
|
|
36
|
+
self,
|
|
37
|
+
bitrate: str,
|
|
38
|
+
*,
|
|
39
|
+
min_bitrate: str | None = None,
|
|
40
|
+
max_bitrate: str | None = None,
|
|
41
|
+
) -> None:
|
|
42
|
+
super().__init__()
|
|
43
|
+
self.bitrate = bitrate
|
|
44
|
+
self.min_bitrate = min_bitrate
|
|
45
|
+
self.max_bitrate = max_bitrate
|
|
46
|
+
|
|
47
|
+
def rate_config(self) -> list[str]:
|
|
48
|
+
rate_conf = ["-b:v", self.bitrate]
|
|
49
|
+
|
|
50
|
+
if self.min_bitrate:
|
|
51
|
+
rate_conf.extend(["-minrate", self.min_bitrate])
|
|
52
|
+
|
|
53
|
+
if self.max_bitrate:
|
|
54
|
+
rate_conf.extend(["-maxrate", self.max_bitrate])
|
|
55
|
+
|
|
56
|
+
return rate_conf
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import shlex
|
|
2
|
+
|
|
3
|
+
from shelltastic import shell
|
|
4
|
+
|
|
5
|
+
from raricode.ffmpeg.codec.audio import AudioCodec
|
|
6
|
+
from raricode.ffmpeg.codec.video import VideoCodec
|
|
7
|
+
from raricode.ffmpeg.rate.audio import AudioRate
|
|
8
|
+
from raricode.ffmpeg.rate.video import VideoRate
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class FfmpegRunner:
|
|
12
|
+
def __init__(
|
|
13
|
+
self,
|
|
14
|
+
*,
|
|
15
|
+
video_codec: VideoCodec | None = None,
|
|
16
|
+
audio_codec: AudioCodec | None = None,
|
|
17
|
+
subtitle_codec=None,
|
|
18
|
+
video_rate: VideoRate | None = None,
|
|
19
|
+
audio_rate: AudioRate | None = None,
|
|
20
|
+
) -> None:
|
|
21
|
+
self.video_codec = video_codec
|
|
22
|
+
self.audio_codec = audio_codec
|
|
23
|
+
self.subtitle_codec = subtitle_codec
|
|
24
|
+
self.video_rate = video_rate
|
|
25
|
+
self.audio_rate = audio_rate
|
|
26
|
+
|
|
27
|
+
def ffmpeg_command(self, input_file: str, output_file: str) -> list[str]:
|
|
28
|
+
ffmpeg_cmd = ["ffmpeg", "-i", input_file]
|
|
29
|
+
|
|
30
|
+
if self.video_codec:
|
|
31
|
+
ffmpeg_cmd.extend(self.video_codec.ffmpeg_codec_settings())
|
|
32
|
+
|
|
33
|
+
if self.video_rate:
|
|
34
|
+
ffmpeg_cmd.extend(self.video_rate.rate_config())
|
|
35
|
+
|
|
36
|
+
if self.audio_codec:
|
|
37
|
+
ffmpeg_cmd.extend(self.audio_codec.ffmpeg_codec_settings())
|
|
38
|
+
|
|
39
|
+
if self.audio_rate:
|
|
40
|
+
ffmpeg_cmd.extend(self.audio_rate.rate_config())
|
|
41
|
+
|
|
42
|
+
ffmpeg_cmd.append(output_file)
|
|
43
|
+
|
|
44
|
+
return ffmpeg_cmd
|
|
45
|
+
|
|
46
|
+
def run(self, input_file: str, output_file: str) -> None:
|
|
47
|
+
shell.run(self.ffmpeg_command(input_file, output_file))
|
|
48
|
+
|
|
49
|
+
def dry_run(self, input_file: str, output_file: str) -> None:
|
|
50
|
+
print(shlex.join(self.ffmpeg_command(input_file, output_file)))
|
|
File without changes
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import io
|
|
2
|
+
from abc import ABC, abstractmethod
|
|
3
|
+
from contextlib import AbstractContextManager
|
|
4
|
+
from types import TracebackType
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
from raricode.utils import cursor
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class _AbstractBlockPrinter(ABC):
|
|
11
|
+
@abstractmethod
|
|
12
|
+
def print(self, *values, sep=" ", end="\n"):
|
|
13
|
+
"""Print text into the block. Use this instead of normal print."""
|
|
14
|
+
raise NotImplementedError
|
|
15
|
+
|
|
16
|
+
@abstractmethod
|
|
17
|
+
def replace(self, *values, sep=" ", end="\n"):
|
|
18
|
+
"""Replace the current block text with the given text"""
|
|
19
|
+
raise NotImplementedError
|
|
20
|
+
|
|
21
|
+
def flush(self):
|
|
22
|
+
pass
|
|
23
|
+
|
|
24
|
+
@abstractmethod
|
|
25
|
+
def clear(self):
|
|
26
|
+
"""Clear all text from the block"""
|
|
27
|
+
raise NotImplementedError
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class _BufferedBlockPrinter(_AbstractBlockPrinter, AbstractContextManager):
|
|
31
|
+
def __init__(self, parent_printer: _AbstractBlockPrinter) -> None:
|
|
32
|
+
self._parent_printer = parent_printer
|
|
33
|
+
self._closed = False
|
|
34
|
+
self._buffer: io.StringIO = io.StringIO()
|
|
35
|
+
|
|
36
|
+
def __enter__(self) -> Any:
|
|
37
|
+
return super().__enter__()
|
|
38
|
+
|
|
39
|
+
def __exit__(
|
|
40
|
+
self,
|
|
41
|
+
exc_type: type[BaseException] | None,
|
|
42
|
+
exc_value: BaseException | None,
|
|
43
|
+
traceback: TracebackType | None,
|
|
44
|
+
) -> bool | None:
|
|
45
|
+
self.close()
|
|
46
|
+
|
|
47
|
+
def _clear_buffer(self):
|
|
48
|
+
old_buffer = self._buffer
|
|
49
|
+
self._buffer = io.StringIO()
|
|
50
|
+
old_buffer.close()
|
|
51
|
+
|
|
52
|
+
def close(self):
|
|
53
|
+
if not self._closed:
|
|
54
|
+
self.flush()
|
|
55
|
+
self._buffer.close()
|
|
56
|
+
self._closed = True
|
|
57
|
+
|
|
58
|
+
def print(self, *values, sep=" ", end="\n"):
|
|
59
|
+
assert not self._closed, f"{self.__class__.__name__} is closed"
|
|
60
|
+
print(*values, sep=sep, end=end, file=self._buffer)
|
|
61
|
+
|
|
62
|
+
def replace(self, *values, sep=" ", end="\n"):
|
|
63
|
+
# We don't need to worry about flickering at all like in BlockPrinter,
|
|
64
|
+
# so we can just clear and then call print.
|
|
65
|
+
self.clear()
|
|
66
|
+
self.print(*values, sep=sep, end=end)
|
|
67
|
+
|
|
68
|
+
def flush(self):
|
|
69
|
+
assert not self._closed, f"{self.__class__.__name__} is closed"
|
|
70
|
+
# We replace the block but do not clear the buffer as the user
|
|
71
|
+
# can still append additional text to the buffer
|
|
72
|
+
self._parent_printer.replace(self._buffer.getvalue(), end="")
|
|
73
|
+
|
|
74
|
+
def clear(self):
|
|
75
|
+
assert not self._closed, f"{self.__class__.__name__} is closed"
|
|
76
|
+
# Since we buffer and only write on flush, all we have to do is clear the buffer.
|
|
77
|
+
self._clear_buffer()
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
class BlockPrinter(_AbstractBlockPrinter):
|
|
81
|
+
def __init__(self) -> None:
|
|
82
|
+
self._line_count = 0
|
|
83
|
+
|
|
84
|
+
def print(self, *values, sep=" ", end="\n"):
|
|
85
|
+
with io.StringIO() as str_io:
|
|
86
|
+
print(*values, sep=sep, end=end, file=str_io)
|
|
87
|
+
str_io.seek(0)
|
|
88
|
+
for line in str_io.readlines():
|
|
89
|
+
print(line, end="")
|
|
90
|
+
self._line_count += 1
|
|
91
|
+
|
|
92
|
+
def replace(self, *values, sep=" ", end="\n"):
|
|
93
|
+
# Return cursor to top of block and reset line_count
|
|
94
|
+
cursor.prev_line(self._line_count)
|
|
95
|
+
self._line_count = 0
|
|
96
|
+
|
|
97
|
+
with io.StringIO() as str_io:
|
|
98
|
+
# Print given text into buffer
|
|
99
|
+
print(*values, sep=sep, end=end, file=str_io)
|
|
100
|
+
|
|
101
|
+
# Loop through lines clear then replace
|
|
102
|
+
str_io.seek(0)
|
|
103
|
+
for line in str_io.readlines():
|
|
104
|
+
cursor.erase_to_end_of_line()
|
|
105
|
+
print(line, end="")
|
|
106
|
+
self._line_count += 1
|
|
107
|
+
# Clear rest of block
|
|
108
|
+
cursor.clear_screen_to_end()
|
|
109
|
+
|
|
110
|
+
def clear(self):
|
|
111
|
+
# Return cursor to top of block and reset line_count
|
|
112
|
+
cursor.prev_line(self._line_count)
|
|
113
|
+
self._line_count = 0
|
|
114
|
+
# Clear block
|
|
115
|
+
cursor.clear_screen_to_end()
|
|
116
|
+
|
|
117
|
+
def replace_block(self) -> _BufferedBlockPrinter:
|
|
118
|
+
"""
|
|
119
|
+
Returns a buffer that can be used to replace the block.
|
|
120
|
+
The replacement is printed all at once on flush or close.
|
|
121
|
+
|
|
122
|
+
This can reduce text flickering when replacing using multiple prints.
|
|
123
|
+
|
|
124
|
+
Returned buffer must be closed.
|
|
125
|
+
|
|
126
|
+
Using BlockPrinter while a buffer is open is undefined behavior.
|
|
127
|
+
"""
|
|
128
|
+
return _BufferedBlockPrinter(self)
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class CtrlSeq:
|
|
5
|
+
def __init__(self, seq: str, param: None | str | int = None) -> None:
|
|
6
|
+
"""Represents a terminal control sequence."""
|
|
7
|
+
self._seq = seq
|
|
8
|
+
self._param = "" if param is None else str(param)
|
|
9
|
+
|
|
10
|
+
def __call__(self, param: None | str | int) -> CtrlSeq:
|
|
11
|
+
"""Set control sequence parameter"""
|
|
12
|
+
return CtrlSeq(self._seq, param)
|
|
13
|
+
|
|
14
|
+
def __str__(self) -> str:
|
|
15
|
+
return f"\033[{self._param}{self._seq}"
|
|
16
|
+
|
|
17
|
+
def __repr__(self) -> str:
|
|
18
|
+
return f"\\033[{self._param}{self._seq}"
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
CUU = CtrlSeq("A")
|
|
22
|
+
"""Cursor Up"""
|
|
23
|
+
|
|
24
|
+
CUD = CtrlSeq("B")
|
|
25
|
+
"""Cursor Down"""
|
|
26
|
+
|
|
27
|
+
CUF = CtrlSeq("C")
|
|
28
|
+
"""Cursor Forward"""
|
|
29
|
+
|
|
30
|
+
CUB = CtrlSeq("D")
|
|
31
|
+
"""Cursor Back"""
|
|
32
|
+
|
|
33
|
+
CNL = CtrlSeq("E")
|
|
34
|
+
"""Cursor Next Line"""
|
|
35
|
+
|
|
36
|
+
CPL = CtrlSeq("F")
|
|
37
|
+
"""Cursor Previous Line"""
|
|
38
|
+
|
|
39
|
+
CHA = CtrlSeq("G")
|
|
40
|
+
"""Cursor Horizontal Absolute"""
|
|
41
|
+
|
|
42
|
+
CUP = CtrlSeq("H")
|
|
43
|
+
"""Cursor Position"""
|
|
44
|
+
|
|
45
|
+
ED = CtrlSeq("J")
|
|
46
|
+
"""Erase in Display"""
|
|
47
|
+
|
|
48
|
+
EL = CtrlSeq("K")
|
|
49
|
+
"""Erase in Line"""
|
|
50
|
+
|
|
51
|
+
SU = CtrlSeq("S")
|
|
52
|
+
"""Scroll Up"""
|
|
53
|
+
|
|
54
|
+
SD = CtrlSeq("T")
|
|
55
|
+
"""Scroll Down"""
|
|
56
|
+
|
|
57
|
+
HVP = CtrlSeq("f")
|
|
58
|
+
"""Horizontal Vertical Position"""
|
raricode/utils/cursor.py
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
from raricode.utils import ctrlseq
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def _clean_print(*values: object, sep=" ", file=None, flush=False):
|
|
5
|
+
print(*values, sep=sep, end="", file=file, flush=flush)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def forward(n: int):
|
|
9
|
+
"""
|
|
10
|
+
Move cursor forwards the given number of columns
|
|
11
|
+
|
|
12
|
+
`n` must be positive
|
|
13
|
+
"""
|
|
14
|
+
assert n >= 0, "n must be positive"
|
|
15
|
+
if n > 0:
|
|
16
|
+
_clean_print(ctrlseq.CUF(n))
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def right(n: int):
|
|
20
|
+
"""Alias for forward"""
|
|
21
|
+
forward(n)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def back(n: int):
|
|
25
|
+
"""
|
|
26
|
+
Move cursor back the given number of columns
|
|
27
|
+
|
|
28
|
+
`n` must be positive
|
|
29
|
+
"""
|
|
30
|
+
assert n >= 0, "n must be positive"
|
|
31
|
+
if n > 0:
|
|
32
|
+
_clean_print(ctrlseq.CUB(n))
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def left(n: int):
|
|
36
|
+
"""Alias for back"""
|
|
37
|
+
back(n)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def up(n: int):
|
|
41
|
+
"""
|
|
42
|
+
Move cursor up the given number of lines
|
|
43
|
+
|
|
44
|
+
`n` must be positive
|
|
45
|
+
"""
|
|
46
|
+
assert n >= 0, "n must be positive"
|
|
47
|
+
if n > 0:
|
|
48
|
+
_clean_print(ctrlseq.CUU(n))
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def down(n: int):
|
|
52
|
+
"""
|
|
53
|
+
Move cursor down the given number of lines
|
|
54
|
+
|
|
55
|
+
`n` must be positive
|
|
56
|
+
"""
|
|
57
|
+
assert n >= 0, "n must be positive"
|
|
58
|
+
if n > 0:
|
|
59
|
+
_clean_print(ctrlseq.CUD(n))
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def next_line(n: int):
|
|
63
|
+
"""Move cursor to beginning of line n lines down"""
|
|
64
|
+
assert n >= 0, "n must be positive"
|
|
65
|
+
if n > 0:
|
|
66
|
+
_clean_print(ctrlseq.CNL(n))
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def prev_line(n: int):
|
|
70
|
+
"""Move cursor to beginning of line n lines up"""
|
|
71
|
+
assert n >= 0, "n must be positive"
|
|
72
|
+
if n > 0:
|
|
73
|
+
_clean_print(ctrlseq.CPL(n))
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def horizontal_absolute(column: int):
|
|
77
|
+
"""1 based column position in the current row"""
|
|
78
|
+
_clean_print(ctrlseq.CHA(column))
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def position(row: int, column: int):
|
|
82
|
+
"""Move cursor to the specified row and column (1 based)"""
|
|
83
|
+
_clean_print(ctrlseq.CUP(f"{row};{column}"))
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def erase_display(n: int | None = None):
|
|
87
|
+
"""
|
|
88
|
+
Clears screen.
|
|
89
|
+
|
|
90
|
+
Accepted values are:
|
|
91
|
+
|
|
92
|
+
0 - Clear from cursor to end of screen (Default)</br>
|
|
93
|
+
1 - Clear from cursor to start of screen</br>
|
|
94
|
+
2 - Clear entire screen</br>
|
|
95
|
+
3 - Clear entire screen and scrollback buffer
|
|
96
|
+
"""
|
|
97
|
+
_clean_print(ctrlseq.ED(n))
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def clear_screen_to_end():
|
|
101
|
+
erase_display(0)
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def clear_screen_to_start():
|
|
105
|
+
erase_display(1)
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def clear_screen():
|
|
109
|
+
erase_display(2)
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def clear_screen_and_scrollback():
|
|
113
|
+
erase_display(3)
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def erase_in_line(n: int | None = None):
|
|
117
|
+
"""
|
|
118
|
+
Erases text in line. Cursor position does not change.
|
|
119
|
+
|
|
120
|
+
Accepted values are:
|
|
121
|
+
|
|
122
|
+
0 - Clear from cursor to end of line (Default)</br>
|
|
123
|
+
1 - Clear from cursor to beginning of line</br>
|
|
124
|
+
"""
|
|
125
|
+
_clean_print(ctrlseq.EL(n))
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def erase_to_end_of_line():
|
|
129
|
+
erase_in_line(0)
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def erase_to_start_of_line():
|
|
133
|
+
erase_in_line(1)
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def scroll_up(n: int):
|
|
137
|
+
"""Scroll whole page up by n lines. New lines are added at the bottom."""
|
|
138
|
+
assert n >= 0, "n must be positive"
|
|
139
|
+
if n > 0:
|
|
140
|
+
_clean_print(ctrlseq.SU(n))
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def scroll_down(n: int):
|
|
144
|
+
"""Scroll whole page down by n lines. New lines are added at the top."""
|
|
145
|
+
assert n >= 0, "n must be positive"
|
|
146
|
+
if n > 0:
|
|
147
|
+
_clean_print(ctrlseq.SD(n))
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def horizontal_vertical_position(row: int, column: int):
|
|
151
|
+
_clean_print(ctrlseq.HVP(f"{row};{column}"))
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
|
|
3
|
+
_VERBOSITY: int = 0
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def verbosity() -> int:
|
|
7
|
+
return _VERBOSITY
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def setup_logging(verbosity: int, *, prefix=""):
|
|
11
|
+
global _VERBOSITY
|
|
12
|
+
_VERBOSITY = verbosity
|
|
13
|
+
|
|
14
|
+
# Setup logger
|
|
15
|
+
if verbosity <= 2:
|
|
16
|
+
logging.basicConfig(format=f"{prefix}%(message)s")
|
|
17
|
+
else:
|
|
18
|
+
logging.basicConfig(
|
|
19
|
+
format=f"{prefix}%(asctime)s [%(levelname)s] %(message)s {{ %(filename)s:%(lineno)d }}"
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
# Set log level
|
|
23
|
+
if verbosity <= -2:
|
|
24
|
+
logging.getLogger().setLevel(logging.CRITICAL)
|
|
25
|
+
|
|
26
|
+
if verbosity == -1:
|
|
27
|
+
logging.getLogger().setLevel(logging.ERROR)
|
|
28
|
+
|
|
29
|
+
if verbosity == 0:
|
|
30
|
+
logging.getLogger().setLevel(logging.WARNING)
|
|
31
|
+
|
|
32
|
+
if verbosity == 1:
|
|
33
|
+
logging.getLogger().setLevel(logging.INFO)
|
|
34
|
+
|
|
35
|
+
if verbosity >= 2:
|
|
36
|
+
logging.getLogger().setLevel(logging.DEBUG)
|
raricode/utils/rate.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import math
|
|
2
|
+
import time
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class RateRingBuffer:
|
|
6
|
+
def __init__(self, rate_period: float = 60, bucket_window: int = 5) -> None:
|
|
7
|
+
assert rate_period >= bucket_window
|
|
8
|
+
self.rate_period: float = rate_period
|
|
9
|
+
self.ring_buffer: list[float | None] = [None] * math.floor(
|
|
10
|
+
rate_period / bucket_window
|
|
11
|
+
)
|
|
12
|
+
self.accumulator = 0
|
|
13
|
+
self.last_bucket_update = time.time()
|
|
14
|
+
|
|
15
|
+
@property
|
|
16
|
+
def num_of_buckets(self) -> int:
|
|
17
|
+
return len(self.ring_buffer)
|
|
18
|
+
|
|
19
|
+
@property
|
|
20
|
+
def bucket_window(self) -> float:
|
|
21
|
+
return self.rate_period / self.num_of_buckets
|
|
22
|
+
|
|
23
|
+
def update_buckets(self):
|
|
24
|
+
# Get how long it's been since bucket update
|
|
25
|
+
time_since_update = time.time() - self.last_bucket_update
|
|
26
|
+
|
|
27
|
+
# If we've breached our window time then roll over the bucket
|
|
28
|
+
if time_since_update >= self.bucket_window:
|
|
29
|
+
if time_since_update >= self.rate_period:
|
|
30
|
+
# If we breached total rate window, just evenly add it to all the buckets
|
|
31
|
+
rate = self.accumulator / time_since_update # accum/s
|
|
32
|
+
|
|
33
|
+
# Spread rate evenly
|
|
34
|
+
self.ring_buffer = [rate * self.bucket_window] * self.num_of_buckets
|
|
35
|
+
else:
|
|
36
|
+
# Otherwise advance a certain number of buckets
|
|
37
|
+
|
|
38
|
+
# How many buckets to advance
|
|
39
|
+
bucket_advance = max(
|
|
40
|
+
math.floor(time_since_update / self.bucket_window), 1
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
# Calculate amount per bucket
|
|
44
|
+
accum_spread = self.accumulator / bucket_advance
|
|
45
|
+
|
|
46
|
+
# Advance buckets
|
|
47
|
+
self.ring_buffer = self.ring_buffer[bucket_advance:] + (
|
|
48
|
+
[accum_spread] * bucket_advance
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
# Advance time
|
|
52
|
+
# We always advance window increments
|
|
53
|
+
self.last_bucket_update = self.last_bucket_update + (
|
|
54
|
+
max(math.floor(time_since_update / self.bucket_window), 1)
|
|
55
|
+
* self.bucket_window
|
|
56
|
+
)
|
|
57
|
+
# Reset accumulator
|
|
58
|
+
self.accumulator = 0
|
|
59
|
+
|
|
60
|
+
def accumulate(self, amount: int):
|
|
61
|
+
self.accumulator += amount
|
|
62
|
+
self.update_buckets()
|
|
63
|
+
|
|
64
|
+
def rate(self) -> float:
|
|
65
|
+
self.update_buckets()
|
|
66
|
+
valid_buckets = [i for i in self.ring_buffer if i is not None]
|
|
67
|
+
if not valid_buckets:
|
|
68
|
+
return 0
|
|
69
|
+
return sum(valid_buckets) / (len(valid_buckets) * self.bucket_window)
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: raricode
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: An ffmpeg trancoding helper
|
|
5
|
+
Author: Bearmine
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Requires-Dist: click>=8.4.1
|
|
9
|
+
Requires-Dist: shelltastic>=0.9.0
|
|
10
|
+
Requires-Python: >=3.12
|
|
11
|
+
Description-Content-Type: text/markdown
|
|
12
|
+
|
|
13
|
+
# Raricode
|
|
14
|
+
|
|
15
|
+
A set of trancoding helpers.
|
|
16
|
+
|
|
17
|
+
## Requirements
|
|
18
|
+
|
|
19
|
+
[FFmpeg](https://www.ffmpeg.org/) must be installed and available on your path.
|
|
20
|
+
|
|
21
|
+
## Installation
|
|
22
|
+
|
|
23
|
+
### Development Installation
|
|
24
|
+
|
|
25
|
+
Run in repo root:
|
|
26
|
+
|
|
27
|
+
```sh
|
|
28
|
+
uv tool install --editable .
|
|
29
|
+
```
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
raricode/__init__.py,sha256=sUR5zDr_Hg1_sJfsl7r3vowq4eL9JEJK7ztb-FcjTck,915
|
|
2
|
+
raricode/__main__.py,sha256=nF00YRK3q8QPU4O65ZNuEeinYgzUp_kpUWbg2mB_NdQ,65
|
|
3
|
+
raricode/click_custom/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
4
|
+
raricode/click_custom/mutually_exclusive.py,sha256=hA-pteSVngWlCGR9VF46x-gEGx2JKjRoZaX3XrSjCUY,962
|
|
5
|
+
raricode/commands/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
6
|
+
raricode/commands/cropdetect.py,sha256=F_J00XyXDZUBDu7OoNAvR1E4fzAUt50-EcqGfXyhgTY,6354
|
|
7
|
+
raricode/commands/ffmpeg_info.py,sha256=o_aeGpLIZarFswWcngXRhVN1UGcn4d8gOtXLeXXNKSI,187
|
|
8
|
+
raricode/commands/targetsize.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
9
|
+
raricode/commands/transcode.py,sha256=RZLWMG-boEmP1c0RrlEMG6jaVVZVSw1VX_MDscAWVIY,2891
|
|
10
|
+
raricode/exception.py,sha256=PWP1akU6sfw1YSUy4w7qh9qqaB6uO0Siz5O-t-h2TI8,94
|
|
11
|
+
raricode/ffmpeg/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
12
|
+
raricode/ffmpeg/codec/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
13
|
+
raricode/ffmpeg/codec/audio.py,sha256=HoZ1M-3MAhwl8aZGd-wtDKVpwyRM9MlScXDN6jNWHfQ,742
|
|
14
|
+
raricode/ffmpeg/codec/video.py,sha256=QrVUduqNltOz57vMKm_U-ayOdyDL1Z6gYuEnbKmGIUU,742
|
|
15
|
+
raricode/ffmpeg/cropdetect.py,sha256=pYou2tRaCZQUc6z57HJAT2onBfFvJC9soQaNkCpt62I,63
|
|
16
|
+
raricode/ffmpeg/rate/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
17
|
+
raricode/ffmpeg/rate/audio.py,sha256=2XOIIyhNeGUeBh-OXBZ3N6-ig5tS2fkh9Hs_uQNOPQg,370
|
|
18
|
+
raricode/ffmpeg/rate/video.py,sha256=mLRczGrPVS2N7cuLHIMr3tN_C_c4gNlLOv6dQusVdVU,1370
|
|
19
|
+
raricode/ffmpeg/runner.py,sha256=u1D9XdBvDwOwCF1OC30vGixVABInW0ZS5ZoAkbxRGfw,1581
|
|
20
|
+
raricode/utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
21
|
+
raricode/utils/block_printer.py,sha256=m_lOFbQQENPwpcciuj1HOVpX29MI-PvLq9fj9GKM9Tc,4167
|
|
22
|
+
raricode/utils/ctrlseq.py,sha256=1CSKnhoTAQbCRY54F9RjzQ7LIoYeieFlLz-CprtjYK0,1116
|
|
23
|
+
raricode/utils/cursor.py,sha256=u5_Un4CxZqGSuTsbBFldkxxfhgmY8qOQoqW5MvJHYtE,3132
|
|
24
|
+
raricode/utils/logging.py,sha256=R-QUroHtXmj1NdRK0rId60qp0EJu5Ncmj8U8fmj73j8,841
|
|
25
|
+
raricode/utils/rate.py,sha256=L3cfvcCJ-VP_lklbUhQ04eBvVB_rmEWCt9tJlQVqByY,2468
|
|
26
|
+
raricode-0.0.1.dist-info/licenses/LICENSE,sha256=FYUuFbYDDqQMEF19Bvm_fSlQQb8Df4FVsNbL2nOnbf0,11338
|
|
27
|
+
raricode-0.0.1.dist-info/WHEEL,sha256=4OL6Foqnnp3xRY5wMkjgc25_i5YJC6dKsC6LPcjqEoU,80
|
|
28
|
+
raricode-0.0.1.dist-info/entry_points.txt,sha256=Swb5JohYGhLGzeQLkbX8vc5AeIaC95THaccIrRThra4,44
|
|
29
|
+
raricode-0.0.1.dist-info/METADATA,sha256=l5BYmmKCADw49NR8G-znU5kdIq6Z0CUW7Oehuq8naaI,531
|
|
30
|
+
raricode-0.0.1.dist-info/RECORD,,
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Bearmine
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|