-
Notifications
You must be signed in to change notification settings - Fork 1
Add logic to convert to srt #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -149,6 +149,60 @@ def transcript_file_name_from_video_file_name(video_file_name): | |
|
|
||
| return transcript_job_name + ".json" | ||
|
|
||
| def format_time(val): | ||
| secs = float(val) | ||
| hours = int(secs/3600) | ||
| secs = secs - (hours * 3600) | ||
| mins = int(secs/60) | ||
| secs = secs - (mins * 60) | ||
| m_secs = int(secs*1000) | ||
| secs = int(secs) | ||
| return pad_time(hours) + ':' + pad_time(mins) + ':' + pad_time(secs) + ',' + pad_time(m_secs, 3) | ||
|
|
||
| def pad_time(val, l= 2): | ||
| return ('0'.join([''] * (l+1)) + str(val))[-l:] | ||
|
|
||
| def convert_transcribe_to_srt(video_file): | ||
| if not transcript_exists_in_s3(video_file): | ||
| logger.info("Transcript for video file %s does not exist" % video_file) | ||
| return "Failed to convert to srt. Transcript does not exist." | ||
|
|
||
| logger.info("Conversion to srt started for video file: %s" % video_file) | ||
| with open(transcript_file_name_from_video_file_name(video_file), encoding='utf-8') as f: | ||
| raw = json.load(f) | ||
| items = raw['results']['items'] | ||
| start = end = counter = index = 0 | ||
| current = float(items[0]['start_time']) | ||
| output = next_line = '' | ||
|
|
||
| for token in items: | ||
| start = format_time(current) | ||
| if token['type'] == 'punctuation': | ||
| next_line = next_line[0:-1] + token['alternatives'][0]['content'] | ||
| end = format_time(items[counter - 1]['end_time']) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the punctuation is at the beginning of the transcript, won't this throw an index-out-of-bounds exception ? |
||
| index += 1 | ||
| output += str(index) + '\n' + str(start) + ' --> ' + str(end) + '\n' + next_line + '\n\n' | ||
| next_line = '' | ||
| if counter + 1 < len(items): | ||
| current = float(items[counter + 1]['start_time']) | ||
| elif float(token['end_time']) - float(token['start_time']) > 5.0: | ||
| end = format_time(items[counter - 1]['end_time']) | ||
| index += 1 | ||
| output += str(index) + '\n' + str(start) + ' --> ' + str(end) + '\n' + next_line + '\n\n' | ||
| next_line = token['alternatives'][0]['content'] + ' ' | ||
| current = float(token['start_time']) | ||
| else: | ||
| next_line += token['alternatives'][0]['content'] + ' ' | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems like any token with end_time - start_time > 5.0 will be written to the srt file. However, I'm not seeing how a sequence of tokens with smaller individual time spans will be strung together in a single sentence ? |
||
|
|
||
| start = format_time(current) | ||
| if items[len(items)-1]['type'] == 'punctuation': | ||
| end = format_time(items[len(items)-2]['end_time']) | ||
| else: | ||
| end = format_time(items[len(items)-1]['end_time']) | ||
|
|
||
| with open(transcript_file_name_from_video_file_name(video_file).replace('.json', '.srt'), 'w', encoding='utf-8') as f: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ideally we'd like to create one new srt file and one new txt file containing the topmost ranked transcript. |
||
| f.write(output) | ||
| logger.info("Conversion to srt completed for video file: %s" % video_file) | ||
|
|
||
| def transcribe_all(): | ||
| video_files = list_video_files() | ||
|
|
@@ -158,6 +212,7 @@ def transcribe_all(): | |
| logger.info("Starting Transcription of Video File: %s" % video_file) | ||
| comment = transcribe_video_file(video_file) | ||
| output[video_file] = comment | ||
| convert_transcribe_to_srt(video_file) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nice to do a best-effort post-processing of comments within a try-catch block, so that if the post-processing of a single transcript fails we can still move on and complete the rest. |
||
|
|
||
| return output | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if results are null or empty and items are null or empty, log the same and move on.