-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathffmpeg.py
63 lines (52 loc) · 1.98 KB
/
ffmpeg.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
import ffmpy
import subprocess
import glob, os
from pydub import AudioSegment
def convertUsingFfmpy():
ff = ffmpy.FFmpeg(
inputs={'foo.webm': None},
outputs={'foo.avi': None}
)
ff.run()
def convertUsingFFmpeg(filename, cmd=None):
# cmd = "ffmpeg -i C:/test.mp4 -ab 160k -ac 2 -ar 44100 -vn audio.wav"
# cmd = "ffmpeg -i foo.webm -vn -acodec copy output.oga"
# cmd = "ffmpeg -i " + filename + " -vn -acodec copy " + filename + ".oga"
cmd = "ffmpeg -i " + filename + " " + filename + ".flac"
print cmd
subprocess.call(cmd, shell=True)
def convertUsingSox(filename, cmd=None):
# cmd = "ffmpeg -i C:/test.mp4 -ab 160k -ac 2 -ar 44100 -vn audio.wav"
# cmd = "ffmpeg -i foo.webm -vn -acodec copy output.oga"
# cmd = "ffmpeg -i " + filename + " -vn -acodec copy " + filename + ".oga"
cmd = "sox " + filename + " --channels=1 --bits=16 --rate=16000 --endian=little " + filename + ".flac"
print cmd
subprocess.call(cmd, shell=True)
def splitUsingSox(folder, file_type, o_file_type, o_folder, th):
folder = 'data/transcribed-split'
o_folder = 'split'
file_type = 'flac'
o_file_type = 'wav'
th = 60
#th in milliseconds
os.chdir(folder)
for file in glob.glob("*." + file_type):
print(file)
audio_file = AudioSegment.from_file(file, file_type)
if (len(audio_file) < th):
audio_file.export(o_folder + '\\' + file + '.' + o_file_type, o_file_type)
continue
start = 0
end = th
audio_left = len(audio_file)
file_num = 1
while (audio_left > 0):
trimmed_file = audio_file[start:end]
trimmed_file.export(o_folder + '\\' + file + str(file_num) + '.' + o_file_type, o_file_type)
audio_left = audio_left - (end - start)
start = end
end = start + min(audio_left, th)
file_num = file_num + 1
if __name__ == '__main__':
#splitUsingSox('a', 'b', 'c', 12)
pass