-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclipper.py
79 lines (58 loc) · 2.08 KB
/
clipper.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
import sys
from pydub import AudioSegment
import os
## Check to see if output folder exists, creates one if not
current_dir = os.getcwd()
output_dir = os.path.join(current_dir, 'output')
if not os.path.exists(output_dir):
os.makedirs(output_dir)
## Take every command line argument from index 1 onwards, and quit if there isn't any input.
args = []
args = sys.argv[1:]
if len(args) == 0:
print("Input 'clip' or 'merge and then the file name(s)")
sys.exit()
## Export path determination
clipped_path = "output/clipped_" if os.name == 'nt' else "output\clipped_"
merged_path = "output/merged_"if os.name == 'nt' else "output\merged_"
def main():
keyWord(args)
## Recognize first word command
def keyWord(args):
if len(args) < 2:
print("Error: Input 'merge' or 'clip and then the file name(s) you wish to execute the action on.")
return
if args[1] == "all":
args.remove("all")
current_files = []
current_files = os.listdir(current_dir)
for filename in current_files:
if filename.endswith('.MP3'):
args.append(filename)
if args[0] == "merge":
args.remove("merge")
mergeClips(args)
elif args[0] == "clip":
args.remove("clip")
backClip(args)
else:
print("Error: Input 'merge' or clip' before file name(s).")
## Formula = x * 1000, where x is desired seconds to clip off the end and * 1000 is accounting for millisecond format ##
back_clip_time = 2 * 1000
# Clip off last <seconds> of audio
def backClip(args):
for i in args:
whole_clip = AudioSegment.from_mp3(i)
desired_clip = whole_clip[:len(whole_clip) - back_clip_time]
desired_clip.export(clipped_path + i, format="mp3")
# Merges clips
def mergeClips(args):
length = len(args)
firstClip = AudioSegment.from_mp3(args[0])
for index in range(1, length):
i = args[index]
mergeClip = AudioSegment.from_mp3(i)
combined = firstClip + mergeClip
firstClip = combined
combined.export(merged_path + i, format="mp3")
main()