-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio_editor.py
335 lines (283 loc) · 12.1 KB
/
audio_editor.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import subprocess
import os
import shutil
import ffmpeg
from progress_saver import ProgressSaver
from audio_editor_dialogs import *
from audio_data import *
APPDATA = 'appdata'
ACTIONS_NAME = {Actions.speed: 's', Actions.volume: 'v', Actions.trim: 't',
Actions.concat: 'c', Actions.reverse: 'r', Actions.partial: 'p'}
RESULT_PATH = 'results'
def get_length(audio):
ffmpeg.probe(audio)
result = ffmpeg.probe(audio)['format']['duration']
return float(result)
def _create_appdata():
path = pathlib.Path(APPDATA)
if not path.exists():
os.mkdir(APPDATA)
def _create_result_folder():
path = pathlib.Path(RESULT_PATH)
if not path.exists():
os.mkdir(RESULT_PATH)
def set_supportive_editor(editor, file_name):
editor.current_file = file_name
editor.audio = file_name
editor.edition_history = EditionHistory('(частичное редактирование)')
editor.partial_editing = True
editor.edition_commands = {
'e': editor.stop,
'1': editor.change_volume,
'2': editor.change_speed,
'3': editor.trim,
'4': editor.concat,
'r': editor.reverse,
'hs': editor.show_history
}
def speed_function(file_name, speed, out):
proc = subprocess.Popen(['ffmpeg', '-loglevel', '-8', '-i', file_name, '-af',
f'atempo={speed}', out])
proc.wait()
def volume_function(file_name, volume, out):
proc = subprocess.Popen(['ffmpeg', '-loglevel', '-8', '-i', file_name, '-af',
f'volume={volume}', out])
proc.wait()
def trim_function(start, end, inp, out):
proc = subprocess.Popen(['ffmpeg', '-loglevel', '-8', '-ss', start, '-i', inp, '-to',
end, out])
proc.wait()
def concat_function(first_audio, second_audio, out):
proc = subprocess.Popen(['ffmpeg', '-loglevel', '-8', '-i', first_audio, '-i', second_audio,
'-filter_complex', '[0:a][1:a]concat=n=2:v=0:a=1', out])
proc.wait()
class AudioEditor:
def __init__(self):
_create_appdata()
_create_result_folder()
self.running = False
self.editing = False
self.partial_editing = False
self.current_file = None
self.audio = None
self.format = 'mp3'
self.bin = []
self.output_name = None
self.saved = True
self.edition_history = EditionHistory('отсутствует')
self.menu_commands = {
's': self.first_edition,
'l': self.repeated_edition,
'0': self.stop
}
self.edition_commands = {
'm': self.back_to_menu,
'0': self.stop,
'1': self.change_volume,
'2': self.change_speed,
'3': self.trim,
'4': self.concat,
'5': self.partial_edition,
'r': self.reverse,
'p': self.play,
'hs': self.show_history,
's': self.save
}
def run(self):
print('Добро пожаловать в аудио редактор!')
self.running = True
self.menu()
def _get_current_output_filename(self, action: Actions, part=0):
short_name = self.current_file.split('\\')[-1].split('.')[-2]
audio_format = self.current_file.split('.')[-1]
suffix = ACTIONS_NAME[action]
if action == Actions.partial:
suffix += str(part)
if len(self.edition_history.history) == 0:
path = f'{APPDATA}\\{short_name}_{suffix}.{audio_format}'
else:
path = f'{APPDATA}\\{short_name}{suffix}.{audio_format}'
return path
def set_output_filename(self):
path = self.audio.split('.')[-2]
audio_format = self.audio.split('.')[-1]
self.output_name = f'{RESULT_PATH}\\{path}_result.{audio_format}'
i = 1
while os.path.exists(self.output_name):
self.output_name = f'{RESULT_PATH}\\{path}_result({i}).{audio_format}'
i += 1
def update_current_file(self, new_name):
self.saved = False
self.current_file = new_name
self.bin.append(new_name)
def passive_saving(self):
print('Текущие изменения сохранены, вернутся к редактированию можно в меню')
saver = ProgressSaver()
old_audios = saver.delete_old()
self.clear_bin(exceptional_files=[self.edition_history.history[-1].file_name])
for name in old_audios:
os.remove(name)
saver.add_unfinished(self.audio, self.edition_history)
def menu(self):
while self.running:
print('Введите команду, чтобы продолжить\n')
show_commands()
command = read_command(self.menu_commands)
function = self.menu_commands[command]
function()
def back_to_menu(self):
if not self.saved and not self.partial_editing:
self.passive_saving()
self.editing = False
def stop(self):
if not self.saved and not self.partial_editing:
self.passive_saving()
self.editing = False
self.running = False
if not self.partial_editing:
print('Приложение успешно завершило работу')
def change_volume(self):
out = self._get_current_output_filename(Actions.volume)
volume = read_volume()
volume_function(self.current_file, volume, out)
self.update_current_file(out)
log = Log(Actions.volume, out, volume=volume)
self.edition_history.add(log)
print('Громкость успешно изменена\n')
def change_speed(self):
out = self._get_current_output_filename(Actions.speed)
speed = read_speed()
speed_function(self.current_file, speed, out)
self.update_current_file(out)
log = Log(Actions.speed, out, speed=speed)
self.edition_history.add(log)
print('Скорость успешно изменена\n')
def trim(self):
out = self._get_current_output_filename(Actions.trim)
length = get_length(self.current_file)
time = datetime.timedelta(seconds=int(length), milliseconds=round(length % 1 * 1000))
start = read_time(f'Введите время начала (общее время аудио: {time})', length)
end = read_time(f'Введите время конца (общее время аудио: {time})', length)
trim_function(start, end, self.current_file, out)
self.update_current_file(out)
log = Log(Actions.trim, out, start=start, end=end)
self.edition_history.add(log)
print('Аудиозапись успешно обрезана\n')
def concat(self):
out = self._get_current_output_filename(Actions.concat)
second_audio = read_audio(True)
if second_audio == 'm':
self.back_to_menu()
else:
print('Аудиотрек успешно загружен')
first_audio = self.current_file
in_order = is_in_order()
if not in_order:
first_audio = second_audio
second_audio = self.current_file
concat_function(first_audio, second_audio, out)
self.update_current_file(out)
log = Log(Actions.concat, out, names=[first_audio, second_audio])
self.edition_history.add(log)
print(f'Аудиозаписи {first_audio}, {second_audio} успешно склеены!')
def partial_edition(self):
result_out = self._get_current_output_filename(Actions.partial)
length = get_length(self.current_file)
time = datetime.timedelta(seconds=int(length), milliseconds=round(length % 1 * 1000))
start = read_time(f'Введите время начала фрагмента (общее время аудио: {time})', length)
end = read_time(f'Введите время конца фрагмента (общее время аудио: {time})', length)
first_part = self._get_current_output_filename(Actions.partial, 1)
second_part = self._get_current_output_filename(Actions.partial, 2)
third_part = self._get_current_output_filename(Actions.partial, 3)
trim_function('00:00:00', start, self.current_file, first_part)
trim_function(start, end, self.current_file, second_part)
trim_function(end, str(time), self.current_file, third_part)
editor = AudioEditor()
set_supportive_editor(editor, second_part)
editor.edition(show_partial_edition_commands)
concat_out_1 = APPDATA + '\\part1.' + self.format
concat_function(first_part, editor.current_file, concat_out_1)
concat_function(concat_out_1, third_part, result_out)
audios_bin = [first_part, second_part, third_part, concat_out_1]
audios_bin += list(map(lambda x: x.file_name, editor.edition_history.history))
self.clear_bin(audios_bin)
self.update_current_file(result_out)
log = Log(Actions.partial, result_out, start=start, end=end, logs=editor.edition_history.history)
self.edition_history.add(log)
print(f'Фрагмент аудиозаписи успешно изменен!')
def reverse(self):
out = self._get_current_output_filename(Actions.reverse)
proc = subprocess.Popen(['ffmpeg', '-loglevel', '-8', '-i', self.current_file, '-af',
'areverse', out])
proc.wait()
self.update_current_file(out)
log = Log(Actions.reverse, out)
self.edition_history.add(log)
print('Аудиозапись «развернута»\n')
def play(self):
proc = subprocess.Popen(['ffplay', '-loglevel', '-8', self.current_file])
proc.wait()
def show_history(self):
self.edition_history.show()
print()
def save(self):
if len(self.edition_history.history) > 0:
self.set_output_filename()
shutil.move(self.current_file, self.output_name)
print('Аудиозапись успешно сохранена')
print(f'Новое название: {self.output_name}')
log = Log(Actions.save, self.output_name)
self.edition_history.add(log)
self.bin.pop()
self.saved = True
else:
print('Аудиозапись не изменена')
def load_audio(self):
audio = read_audio()
self.format = audio.split('.')[-1]
if audio == 'm':
self.back_to_menu()
else:
self.audio = audio
self.current_file = audio
self.edition_history = EditionHistory(self.audio)
print('Аудиотрек успешно загружен\n')
def clear_bin(self, files=None, exceptional_files=[]):
if files is None:
files = self.bin
for file in files:
if file in exceptional_files:
continue
try:
os.remove(file)
except FileNotFoundError:
pass
self.bin = []
def update_from_repeated(self, file):
saver = ProgressSaver()
edition_history = saver.pop(file)
self.audio = file
self.saved = False
self.current_file = edition_history.history[-1].file_name
self.edition_history = edition_history
for log in edition_history.history:
self.bin.append(log.file_name)
def repeated_edition(self):
saver = ProgressSaver()
unfinished_files = saver.get_all()
file = select_file(unfinished_files)
if file is not None:
self.update_from_repeated(file)
self.edition()
def first_edition(self):
self.load_audio()
self.edition()
def edition(self, edition_commands=show_edition_commands):
self.editing = True
while self.editing:
edition_commands()
command = read_command(self.edition_commands)
function = self.edition_commands[command]
function()
if self.saved and not self.partial_editing:
self.clear_bin()