This repository has been archived on 2024-11-18. You can view files and clone it, but cannot push or open issues or pull requests.
poc/compression/main.py

62 lines
1.2 KiB
Python
Raw Normal View History

2024-06-28 11:01:49 +00:00
#!/usr/bin/env python3
2024-07-01 14:05:36 +00:00
import subprocess
2024-06-28 11:01:49 +00:00
2024-07-01 14:05:36 +00:00
class FFmpeg:
input: str
output: str
format: str
2024-07-01 16:45:15 +00:00
bitrate: int | None
fpass: tuple[int, int] | None
2024-07-01 14:05:36 +00:00
def __init__(
self,
input="pipe:0",
output="pipe:1",
2024-07-01 16:45:15 +00:00
format="amr",
2024-07-01 14:05:36 +00:00
bitrate=None,
2024-07-01 16:45:15 +00:00
fpass=None,
2024-07-01 14:05:36 +00:00
):
self.input = input
self.output = output
self.format = format
self.bitrate = bitrate
2024-07-01 16:45:15 +00:00
self.fpass = fpass
2024-07-01 14:05:36 +00:00
def __get_cmd(self) -> list[str]:
2024-07-01 16:45:15 +00:00
cmd = [
"ffmpeg",
"-i",
self.input,
"-f",
self.format,
"-ac",
"1",
"-ar",
"8000",
]
2024-07-01 14:05:36 +00:00
if self.bitrate is not None:
2024-07-01 16:45:15 +00:00
cmd += ["-b:a", str(self.bitrate)]
if self.fpass:
cmd += [
"-filter:a",
f"highpass=f={self.fpass[0]},lowpass=f={self.fpass[1]}",
]
return cmd + [self.output]
2024-07-01 14:05:36 +00:00
def run(self):
cmd = self.__get_cmd()
subprocess.Popen(cmd)
2024-06-28 11:01:49 +00:00
def main():
2024-07-01 16:45:15 +00:00
ff = FFmpeg(bitrate=12200, fpass=[200, 3400])
2024-07-01 14:05:36 +00:00
ff.run()
2024-06-28 11:01:49 +00:00
if __name__ == "__main__":
main()