-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathformat_audio.py
More file actions
executable file
·64 lines (57 loc) · 2.09 KB
/
format_audio.py
File metadata and controls
executable file
·64 lines (57 loc) · 2.09 KB
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
#!/usr/bin/env python
import os
import subprocess
import sys
import re
def format_audio(local_audio):
# Find audio duration
duration = ["ffprobe",
# suppress all but error messages
"-v", "error",
# duration in seconds.microseconds
"-show_entries", "format=duration",
# set print format
"-of", "default=noprint_wrappers=1:nokey=1",
local_audio]
proc = subprocess.Popen(duration, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
seconds = float(proc.communicate()[0])
# Format audio
print("\nFormatting audio:")
# if audio > 4800 seconds (80 minutes), chunk it
if (seconds > 4800):
chunk = ["ffmpeg",
# suppress all but error messages
"-loglevel", "panic",
# input audio file path
"-i", local_audio,
# linear16 audio encoding
"-acodec", "pcm_s16le",
# mono channel
"-ac", "1",
"-f", "segment",
# chunk at every 4800 seconds interval
"-segment_time", "4800",
local_bucket+"/"+file_name+"%d.wav"]
subprocess.call(chunk)
# skip chunking if audio < 4800 seconds
else:
extract = ["ffmpeg",
"-loglevel", "panic",
"-i", local_audio,
"-acodec", "pcm_s16le",
"-ac", "1",
local_bucket+"/"+file_name+".wav"]
subprocess.call(extract)
print("\nFinished extracting audio.")
if __name__ == "__main__":
# gs:// path for audio file stored on GCS
gs_path = str(sys.argv[1])
# bucket name where audio file resides on GCS
gs_bucket = os.path.split(gs_path)[0]
# local path on cloud shell for audio file
local_bucket = re.split("://", gs_bucket)[1]
# audio file name
audio_name = os.path.split(gs_path)[1]
# audio file name without the extension
file_name = audio_name.split(".")[0]
format_audio(local_bucket+"/"+audio_name)