Skip to content

Commit

Permalink
feat: add ability to set caption line length for DG converter
Browse files Browse the repository at this point in the history
  • Loading branch information
SandraRodgers committed Nov 20, 2023
1 parent 0320802 commit be10644
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 7 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ transcription = DeepgramConverter(dg_response)
captions = srt(transcription)
```

### Line length

Add an optional integer parameter to set the line length of the caption.

```py
line_length = 10

deepgram = DeepgramConverter(dg_speakers)
captions = webvtt(deepgram, line_length)
```

## Other Converters

### Whisper
Expand Down
2 changes: 1 addition & 1 deletion deepgram_captions/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class DeepgramConverter:
def __init__(self, dg_response):
self.response = dg_response

def get_lines(self, line_length: int = 8):
def get_lines(self, line_length):
results = self.response["results"]
content = []

Expand Down
8 changes: 6 additions & 2 deletions deepgram_captions/srt.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
from .helpers import seconds_to_timestamp


def srt(converter):
def srt(converter, line_length=None):
output = []
lines = converter.get_lines()

if line_length == None:
line_length = 8

lines = converter.get_lines(line_length)
entry = 1

current_speaker = None
Expand Down
7 changes: 5 additions & 2 deletions deepgram_captions/webvtt.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from .helpers import seconds_to_timestamp


def webvtt(converter):
def webvtt(converter, line_length=None):
output = []
output.append("WEBVTT")
output.append("")

if line_length == None:
line_length = 8

if hasattr(converter, "get_headers") and callable(
getattr(converter, "get_headers")
):
Expand All @@ -17,7 +20,7 @@ def webvtt(converter):
output.append("")

if hasattr(converter, "get_lines") and callable(getattr(converter, "get_lines")):
lines = converter.get_lines()
lines = converter.get_lines(line_length)

speaker_labels = "speaker" in lines[0][0]

Expand Down
6 changes: 4 additions & 2 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@

# Uncomment a section to test the converter:

deepgram = DeepgramConverter(dg_speakers_no_utterances)
captions = srt(deepgram)
line_length = 10

deepgram = DeepgramConverter(dg_speakers)
captions = webvtt(deepgram, line_length)
print(captions)

# assembly = AssemblyAIConverter(assemblyai_utterances)
Expand Down

0 comments on commit be10644

Please sign in to comment.