-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaudio_splitter.py
134 lines (107 loc) · 3.45 KB
/
audio_splitter.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
127
128
129
130
131
132
133
134
from pathlib import Path
import boto3
import click
import json
import logging
import os
import requests
import shutil
import sys
import time
import typing
def start_transcription(
key,
*,
storage,
transcribe,
bucket,
ChannelIdentification=True,
lang='en-US',
):
transcribe_job_uri = f'{storage.meta.endpoint_url}/{bucket}/{key}'
click.echo(Path(key).suffix[1:])
transcribe.start_transcription_job(
TranscriptionJobName=key,
Media={'MediaFileUri': transcribe_job_uri},
MediaFormat= Path(key).suffix[1:],
LanguageCode=lang,
Settings={'ChannelIdentification': ChannelIdentification},
)
return key
@click.group()
def cli():
pass
@cli.command()
@click.option('--channels', is_flag=True)
@click.option('--check', '-c', is_flag=True)
@click.argument('base_audio_file')
def transcription(
channels,
base_audio_file,
check,
directory,
):
# Amazon Information
bucket = os.environ['BUCKET_NAME']
storage = boto3.client('s3')
transcribe = boto3.client('transcribe')
key=Path(base_audio_file).name.replace(' ', '')
if check:
job = transcribe.get_transcription_job(TranscriptionJobName=key)
job_status = job['TranscriptionJob']['TranscriptionJobStatus']
if job_status != 'IN_PROGRESS':
job_uri = job['TranscriptionJob']['Transcript']['TranscriptFileUri']
r = requests.get(job_uri)
r.raise_for_status()
with open(f'{key}.json', 'w') as json_file:
json_file.write(json.dumps(r.json()))
click.echo('Job: {key} complete and written')
return # Terminates the script
storage.upload_file(
Filename=base_audio_file,
Bucket=bucket,
Key=key,
)
return start_transcription(
key=key, bucket=bucket, storage=storage,
transcribe=transcribe, ChannelIdentification=channels,
)
@cli.command()
@click.argument('directory')
@click.option('--check', '-c', is_flag=True)
@click.option('--channels', is_flag=True)
@click.option('--file-format', default='mp3')
def bulk_transcription(
directory,
check,
channels,
file_format,
):
bucket = os.environ['BUCKET_NAME']
storage = boto3.client('s3')
transcribe = boto3.client('transcribe')
for path in Path(directory).rglob(f'*.{file_format}'):
key = path.name.replace(' ', '')
if check:
job = transcribe.get_transcription_job(TranscriptionJobName=key)
job_status = job['TranscriptionJob']['TranscriptionJobStatus']
if job_status != 'IN_PROGRESS':
job_uri = job['TranscriptionJob']['Transcript']['TranscriptFileUri']
r = requests.get(job_uri)
r.raise_for_status()
with open(f'{key}.json', 'w') as json_file:
json_file.write(json.dumps(r.json()))
click.echo(f'Job: {key} complete and written')
continue # Terminates the script
storage.upload_file(
Filename=str(path),
Bucket=bucket,
Key=key,
)
start_transcription(
key=key, bucket=bucket, storage=storage,
transcribe=transcribe, ChannelIdentification=channels,
)
return
if __name__ == '__main__':
cli()