-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudio_utils.py
More file actions
executable file
·92 lines (79 loc) · 2.56 KB
/
audio_utils.py
File metadata and controls
executable file
·92 lines (79 loc) · 2.56 KB
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
import os
import random
import json
import sys
import time
import logging
import config as conf
from pydub import AudioSegment
def load_sound(log,file_name): #,audio_type):
# автоопределение входного файла
try:
#sound = AudioSegment.from_file(file_name, audio_type) # audiu_type may be: "mp4", "wma", "aac", "ogg", "flv", "mp3", "wav"
sound = AudioSegment.from_file(file_name) # audiu_type may be: "mp4", "wma", "aac", "ogg", "flv", "mp3", "wav"
except:
log.error('Cannot load sound: %s'%file_name)
return None
return sound
def save_as_wav(log,sound,file_name):
try:
sound.export(file_name, format="wav")
except:
log.error('Cannot save sound as wav: %s'%file_name)
return True
def save_as_opus(log,sound,file_name):
try:
sound.export(file_name, format="opus")
except:
log.error('Cannot save sound as opus: %s'%file_name)
return True
if __name__ == '__main__':
log= logging.getLogger("audio_utils")
if conf.debug:
log.setLevel(logging.DEBUG)
else:
log.setLevel(logging.INFO)
# create the logging file handler
fh = logging.FileHandler(conf.log_path)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(filename)s:%(lineno)d - %(funcName)s() %(levelname)s - %(message)s')
fh.setFormatter(formatter)
if conf.debug:
# логирование в консоль:
#stdout = logging.FileHandler("/dev/stdout")
stdout = logging.StreamHandler(sys.stdout)
stdout.setFormatter(formatter)
log.addHandler(stdout)
# add handler to logger object
log.addHandler(fh)
log.info("Program started")
#sound = load_sound(log,"A1D6A591F31ED0F704EDD00C0B59297D.oga", "ogg")
sound = load_sound(log,"EE117081C56E0A51BBA8A9AC1E394411.mpga", "ogg")
save_as_opus(log,sound,"out.oga")
sys.exit()
f=open("A1D6A591F31ED0F704EDD00C0B59297D.oga","br")
data=f.read()
f.close()
job_id=voice2textLongAudioAddRequest(log,data)
if job_id==None:
log.error("error call testing function")
else:
# ждём результата:
print("\nSuccess request, job_id='%s'"%job_id)
time.sleep(3)
for iteration in range(0,10):
result=voice2textLongAudioResult(log,job_id)
if result == None:
log.error("error voice2textLongAudioResult()")
sys.exit(1)
else:
if result["done"]==False:
log.debug("need wait result...")
time.sleep(iteration*10)
continue
else:
log.info("result text = %s"%result["result"])
break
log.info("Program exit!")