From b1d89075d3df240b36ce0ad1677af942e5cba859 Mon Sep 17 00:00:00 2001 From: ange Date: Mon, 1 Jul 2024 16:05:36 +0200 Subject: [PATCH 1/3] remove ffmpy deps from compression --- compression/main.py | 45 ++++++++++++++++++++++++++++++------ compression/requirements.txt | 1 - 2 files changed, 38 insertions(+), 8 deletions(-) delete mode 100644 compression/requirements.txt diff --git a/compression/main.py b/compression/main.py index 67e1401..66c95c5 100755 --- a/compression/main.py +++ b/compression/main.py @@ -1,16 +1,47 @@ #!/usr/bin/env python3 -import sys +import subprocess -from ffmpy import FFmpeg + +class FFmpeg: + input: str + output: str + format: str + bitrate: str | None + freq: int | None + + def __init__( + self, + input="pipe:0", + output="pipe:1", + format="mp3", + bitrate=None, + freq=None, + ): + self.input = input + self.output = output + self.format = format + self.bitrate = bitrate + self.freq = freq + + def __get_cmd(self) -> list[str]: + cmd = ["ffmpeg", "-i", self.input, "-f", self.format, self.output] + + if self.bitrate is not None: + cmd += ["-b:a", self.bitrate] + if self.freq is not None: + cmd += ["-ar", str(self.freq)] + return cmd + + def run(self): + cmd = self.__get_cmd() + + subprocess.Popen(cmd) def main(): - ff = FFmpeg( - inputs={"pipe:0": None}, - outputs={"pipe:1": "-ar 8000 -b:a 128k -f mp3"}, - ) - ff.run(input_data=sys.stdin.buffer.read()) + ff = FFmpeg(bitrate="128k", freq=8000) + ff.run() if __name__ == "__main__": diff --git a/compression/requirements.txt b/compression/requirements.txt deleted file mode 100644 index 0aafc23..0000000 --- a/compression/requirements.txt +++ /dev/null @@ -1 +0,0 @@ -ffmpy~=0.3 From c9d670e7176c16ad6d67b8b8a9eb6a2e16603916 Mon Sep 17 00:00:00 2001 From: ange Date: Mon, 1 Jul 2024 17:19:51 +0200 Subject: [PATCH 2/3] add test python c ffmpeg bindings --- compression/ctest/main.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 compression/ctest/main.py diff --git a/compression/ctest/main.py b/compression/ctest/main.py new file mode 100644 index 0000000..a660274 --- /dev/null +++ b/compression/ctest/main.py @@ -0,0 +1,15 @@ +import ctypes + + +def avcodec_version(): + f = ctypes.CDLL("libavcodec.so") + + print(f.avcodec_version()) + + +def main(): + avcodec_version() + + +if __name__ == "__main__": + main() From f8cdf675608e18762363bb0aa2a38496be9b12bc Mon Sep 17 00:00:00 2001 From: ange Date: Mon, 1 Jul 2024 18:45:15 +0200 Subject: [PATCH 3/3] add amr compression --- .gitignore | 1 + compression/main.py | 35 ++++++++++++++++++++++++----------- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index bceb365..b40a346 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ ### Audio ### *.aif +*.amr *.flac *.iff *.m3u diff --git a/compression/main.py b/compression/main.py index 66c95c5..e5b28ec 100755 --- a/compression/main.py +++ b/compression/main.py @@ -7,31 +7,44 @@ class FFmpeg: input: str output: str format: str - bitrate: str | None - freq: int | None + bitrate: int | None + fpass: tuple[int, int] | None def __init__( self, input="pipe:0", output="pipe:1", - format="mp3", + format="amr", bitrate=None, - freq=None, + fpass=None, ): self.input = input self.output = output self.format = format self.bitrate = bitrate - self.freq = freq + self.fpass = fpass def __get_cmd(self) -> list[str]: - cmd = ["ffmpeg", "-i", self.input, "-f", self.format, self.output] + cmd = [ + "ffmpeg", + "-i", + self.input, + "-f", + self.format, + "-ac", + "1", + "-ar", + "8000", + ] if self.bitrate is not None: - cmd += ["-b:a", self.bitrate] - if self.freq is not None: - cmd += ["-ar", str(self.freq)] - return cmd + 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] def run(self): cmd = self.__get_cmd() @@ -40,7 +53,7 @@ class FFmpeg: def main(): - ff = FFmpeg(bitrate="128k", freq=8000) + ff = FFmpeg(bitrate=12200, fpass=[200, 3400]) ff.run()