-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwav-cutter.py
executable file
·63 lines (48 loc) · 1.6 KB
/
wav-cutter.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
#!/usr/bin/env python
import wave
def cut_footage(footage_path, max_duration, footage_dest=None):
'''
Cut footage file to max duration in seconds
Args:
footage_path (str): Path to footage file
max_duration (int): Max duration of footage (in seconds)
footage_dest (str): Destination path to new footage (default: None)
Raises:
IOError: Files I/O problem
wave.Error: Wave library errors
'''
import math
from contextlib import closing
destination_footage_path = footage_dest or footage_path + '.new'
with closing(wave.open(footage_path, 'rb')) as cf:
frames = cf.getnframes()
rate = float(cf.getframerate())
max_frames = int(math.ceil(max_duration * rate))
if max_frames > frames:
max_frames = frames
with closing(wave.open(destination_footage_path, 'wb')) as nf:
nf.setnchannels(cf.getnchannels())
nf.setsampwidth(cf.getsampwidth())
nf.setframerate(cf.getframerate())
nf.writeframes(cf.readframes(max_frames))
def main():
import os
import sys
import argparse
parser = argparse.ArgumentParser(description="Simple wav file cutter")
parser.add_argument('footage_path', type=str)
parser.add_argument('max_duration', type=int)
parser.add_argument('--footage_dest', type=str, default=None)
args = parser.parse_args()
try:
if not os.path.exists(args.footage_path):
sys.exit("File %s not exists" % args.footage_path)
if not os.access('.', os.W_OK):
sys.exit("This directory is not writable")
cut_footage(args.footage_path, args.max_duration, args.footage_dest)
except IOError as x:
sys.exit(x)
except wave.Error as x:
sys.exit(x)
if __name__ == "__main__":
main()