Skip to content

Commit

Permalink
audio compression
Browse files Browse the repository at this point in the history
  • Loading branch information
Logerfo committed Dec 6, 2022
1 parent 08c095d commit 2d85f42
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## Introduction
This is a voice transcription bot for telegram. It runs Python 3 in a serverless AWS Lambda instance through [Chalice](https://github.com/aws/chalice) using [FFMpeg](https://ffmpeg.org/), [pyTelegramBotAPI](https://pypi.org/project/pyTelegramBotAPI/) and [Wit.AI](https://wit.ai/).
This is a lightweight voice transcription bot for telegram. It runs Python 3 in a serverless AWS Lambda instance through [Chalice](https://github.com/aws/chalice) using [FFMpeg](https://ffmpeg.org/), [pyTelegramBotAPI](https://pypi.org/project/pyTelegramBotAPI/) and [Wit.AI](https://wit.ai/).

## Running
```sh
Expand Down
2 changes: 1 addition & 1 deletion src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def webhook() -> None:
#&context=%7B%22locale%22%3A%22pt_BR%22%7D
'https://api.wit.ai/dictation',
headers = {
'Content-Type': 'audio/mpeg',
'Content-Type': file_utils.CONTENT_TYPE,
'Authorization': f'Bearer {wit_token}'
},
data = f,
Expand Down
19 changes: 4 additions & 15 deletions src/chalicelib/file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
MAX_DURATION = 300
MAX_SIZE = 20 * 1024 * 1024 #MB

CONTENT_TYPE = 'audio/mpeg'

OGG_FILE_NAME = '/tmp/audio.ogg'
MP3_FILE_NAME = '/tmp/audio.mp3'
COMPRESSED_FILE_NAME = '/tmp/compressed.mp3'
AAC_FILE_NAME = '/tmp/audio.aac'
MP4_FILE_NAME = '/tmp/video.mp4'
OUT_FILE_NAME = '/tmp/out%03d.mp3'
Expand All @@ -25,14 +26,15 @@ def __get_file_id(file) -> Union[str, None]:

def __get_voice_file(voice) -> Union[str, None]:
import subprocess
import os

file_id = __get_file_id(voice)
if file_id is None:
return

__download_file(file_id, OGG_FILE_NAME)

subprocess.Popen(['ffmpeg', '-y', '-i', OGG_FILE_NAME, MP3_FILE_NAME]).wait()
subprocess.Popen(['ffmpeg', '-y', '-i', OGG_FILE_NAME, '-map', '0:a:0', '-b:a', '41k', MP3_FILE_NAME]).wait()

return MP3_FILE_NAME

Expand All @@ -57,18 +59,6 @@ def __split_file(file: str):
subprocess.Popen(['ffmpeg', '-y', '-i', file, '-f', 'segment', '-segment_time', str(MAX_DURATION), '-c', 'copy', OUT_FILE_NAME]).wait()
return glob.glob(OUT_BLOB)

def __compress(file: str):
import os
import subprocess

stats = os.stat(file)
print(f'Original file size: {stats.st_size}')

subprocess.Popen(['ffmpeg', '-y', '-i', file, '-map', '0:a:0', '-b:a', '96k', COMPRESSED_FILE_NAME]).wait()

stats = os.stat(file)
print(f'Compressed file size: {stats.st_size}')

def get_files(message) -> Union[list[str], None]:
voice = message.get('voice')
file = None
Expand All @@ -83,7 +73,6 @@ def get_files(message) -> Union[list[str], None]:
duration = video.get('duration')

if file:
#__compress(file)
if duration and duration <= MAX_DURATION:
return [file]
else:
Expand Down

0 comments on commit 2d85f42

Please sign in to comment.