-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranscoder.py
117 lines (94 loc) · 3.87 KB
/
transcoder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# This code uses opusdec library of opus audio codec
# under three-clause BSD license.
# For details, see https://opus-codec.org/license/
# This code uses LAME mp3 encoder of The LAME Project
# under LGPL license.
# For details, see https://lame.sourceforge.io/
import config as cfg
import requests
import audio_extract
import tempfile
import pathlib
import subprocess
from logging import info, debug
def transcode_wav_to_mp3(wav_bytes: bytes) -> bytes:
debug('start')
if cfg.IN_LINUX:
lame_path = str(pathlib.Path('lame')) # apt install lame
else:
lame_path = str(pathlib.Path('lame', 'lame_win', 'lame.exe'))
result = subprocess.run([lame_path,
'-s',
'16',
'--resample',
'16',
'-f',
'--abr',
'56',
'-m',
'm',
'-',
'-',
],
input=wav_bytes,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
mp3_bytes: bytes = result.stdout
# debug(f'{result.stderr = }')
debug(f'finish')
return mp3_bytes
def transcode_opus_ogg_to_wav(source_url: str) -> bytes:
debug('start')
if cfg.IN_LINUX:
response = requests.get(source_url)
ogg_bytes = response.content
opus_path = str(pathlib.Path('opus', 'opus_linux', 'opusdec'))
result = subprocess.run([opus_path,
'--force-wav',
'--rate',
'16000',
'-',
'-'],
input=ogg_bytes,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
else:
opus_path = str(pathlib.Path('opus', 'opus_win', 'opusdec.exe'))
result = subprocess.run([opus_path,
'--force-wav',
'--rate',
'16000',
source_url,
'-'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
voice_wav_bytes: bytes = result.stdout
debug(f'{result.stderr = }')
debug(f'finish')
return voice_wav_bytes
def extract_mp3_from_video(mp4_bytes: bytes, video_ext: str) -> bytes:
debug('start')
with tempfile.NamedTemporaryFile(mode='wb', suffix=video_ext,
delete_on_close=False) as video_file:
with tempfile.NamedTemporaryFile(mode='wb', suffix='.mp3',
) as audio_file:
video_filename = video_file.name
audio_filename = audio_file.name
video_file.write(mp4_bytes)
video_file.close()
audio_file.close() # TODO why we have to close and open audio file?
with open(video_filename, 'rb') as video_file:
with open(audio_filename, 'wb') as audio_file:
audio_extract.extract_audio(input_path=video_filename,
output_path=audio_filename,
output_format='mp3',
overwrite=True)
with open(audio_filename, 'rb') as audio_file:
mp3_bytes = audio_file.read()
audio_file = pathlib.Path(audio_filename)
if audio_file.exists():
audio_file.unlink()
debug('finish')
return mp3_bytes