-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.py
127 lines (95 loc) · 3.51 KB
/
index.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
import sys
import pathlib
import os
import shutil
import subprocess as sp
import uuid
import random
import datetime
def get_files_in_dir(directory):
file_list = []
for x in directory.iterdir():
if x.is_file():
file_list.append(x)
else:
file_list.append(searching_all_files(directory/x))
return file_list
def clearFolder(pth):
for child in pth.glob('*'):
if child.is_file():
child.unlink()
else:
rm_tree(child)
def oggToMp3(ogg_path, intermed_dir):
mp3_full_dir = intermed_dir/'mp3_full'
mp3_name = ogg_path.stem + '.mp3'
os.chdir(ogg_path.parents[0])
print('CONVERTING {} TO {}'.format(ogg_path.name, mp3_name))
cmd = 'ffmpeg -hide_banner -i {} {}'.format(ogg_path.name, mp3_name)
sp.call(cmd, stdout=sp.PIPE, stderr=sp.PIPE, shell=True)
shutil.move(mp3_name, str(mp3_full_dir))
return pathlib.Path(mp3_full_dir, mp3_name)
def mp3ToCuts(mp3_path, cuts_length):
mp3_cuts_dir = intermed_dir/'mp3_cuts'
os.chdir(mp3_path.parents[0])
print('CUTTING {} INTO PIECES'.format(mp3_path.name))
cut_id = uuid.uuid4()
cmd = 'ffmpeg -hide_banner -i {} -c pcm_s16le -map 0 -f segment -fflags +genpts -segment_time {} %d_{}_{}_seg.mov'.format(mp3_path.name, cuts_length, cut_id, mp3_path.stem)
sp.call(cmd, stdout=sp.PIPE, shell=True)
mp3_files = get_files_in_dir(mp3_path.parents[0])
# add new cuts
for mp3_file in mp3_files:
if mp3_file.stem[-3:] == 'seg':
shutil.move(str(mp3_file), str(mp3_cuts_dir))
def shufleConcatCuts(intermed_dir, cut_length):
mp3_cuts_dir = intermed_dir/'mp3_cuts'
output_dir = intermed_dir.parents[0]/'output'
concat_list_path = pathlib.Path(mp3_cuts_dir, 'concat_list.txt')
cuts = get_files_in_dir(mp3_cuts_dir)
shuffled_cuts_win_path = random.sample(cuts, len(cuts))
shuffled_cuts = map(lambda p: str(p.name), shuffled_cuts_win_path)
cut_id = uuid.uuid4()
# del old cuts in the concat_list
open(concat_list_path, 'a').close()
for cut in shuffled_cuts:
with open(concat_list_path, 'a') as concat_list:
concat_list.write('file {}\n'.format(cut))
# concat_list.write('duration {}\n'.format(cut_length))
now = datetime.datetime.now()
time = "%s%s%s" % (now.hour, now.minute, now.microsecond)
os.chdir(mp3_cuts_dir)
cmd1 = 'ffmpeg -hide_banner -f concat -safe 0 -i concat_list.txt -c:v copy output_{}.mp4'.format(time)
sp.call(cmd1, stdout=sp.PIPE, shell=True)
cmd2 = 'ffmpeg -hide_banner -i output_{}.mp4 -q:a 0 -map a output_{}.mp3'.format(time, time)
sp.call(cmd2, stdout=sp.PIPE, shell=True)
shutil.move(str('output_{}.mp3'.format(time)), str(output_dir))
print('\n\n\n OUTPUT FILE PATH {} \n\n'.format(str(output_dir/'output_{}.mp3'.format(time))))
if len(sys.argv) != 2:
print('Set cut length')
exit()
cut_length = sys.argv[1]
curr_dir = pathlib.Path(__file__).parent.absolute()
input_dir = curr_dir/'input'
intermed_dir = curr_dir/'intermed'
mp3_cuts_dir = intermed_dir/'mp3_cuts'
output_dir = curr_dir/'output'
ogg_files = []
mp3_files = []
input_files = get_files_in_dir(input_dir)
# Clear old mp3 before creating new
mp3_full_dir = intermed_dir/'mp3_full'
clearFolder(mp3_full_dir)
for filename in input_files:
if filename.suffix == ".ogg":
ogg_files.append(filename)
elif filename.suffix == ".mp3":
mp3_files.append(filename)
for ogg_file in ogg_files:
mp3_path = oggToMp3(ogg_file, intermed_dir)
mp3_files.append(mp3_path)
# del old cuts
clearFolder(mp3_cuts_dir)
for mp3_file in mp3_files:
mp3ToCuts(mp3_file, cut_length)
# concat cuts
shufleConcatCuts(intermed_dir, cut_length)