Skip to content

Commit eb20c63

Browse files
authored
feat - add --srt flag (#87)
* feat - add --srt flag * feat - add srt to readme * feat - add srt to readme * feat - add srt to readme * fix - improve error message
1 parent 588e5bc commit eb20c63

7 files changed

Lines changed: 64 additions & 3 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ testfile.*
33
!.github/*
44
brew/*
55
tmp-*
6-
vendor
6+
vendor
7+
*.srt

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,11 @@ assemblyai transcribe [local file | remote url | youtube links] [--flags]
211211
> example: `--custom_spelling "[{\"from\": [\"ariana\"], \"to\": \"Arianna\"}]"` or `--custom_spelling ./custom_spelling.json`
212212
> Specify how words are spelled or formatted in the transcript text.
213213
214+
> **--srt**
215+
> default: false
216+
> example: `--srt`
217+
> Create an SRT file named `[id].srt` in the current directory.
218+
214219
</details>
215220

216221
### Get
@@ -234,6 +239,11 @@ assemblyai get [id]
234239
> example: `--poll=false`
235240
> The CLI will poll the transcription every 3 seconds until it's complete.
236241
242+
> **--srt**
243+
> default: false
244+
> example: `--srt`
245+
> Create an SRT file named `[id].srt` in the current directory.
246+
237247
</details>
238248

239249
## Contributing
@@ -242,7 +252,7 @@ We're more than happy to welcome new contributors. If there's something you'd li
242252

243253
## Telemetry
244254

245-
The AssemblyAI CLI includes a telemetry feature that collects usage data, and is enabled by default.
255+
The AssemblyAI CLI includes a telemetry feature that collects usage data and is enabled by default.
246256

247257
To opt out of telemetry, set the telemetry variable in the `config.toml` file to false.
248258

cmd/get.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ var getCmd = &cobra.Command{
3131
id := args[0]
3232
flags.Poll, _ = cmd.Flags().GetBool("poll")
3333
flags.Json, _ = cmd.Flags().GetBool("json")
34+
flags.Srt, _ = cmd.Flags().GetBool("srt")
3435

3536
U.Token = U.GetStoredToken()
3637
if U.Token == "" {
@@ -51,5 +52,6 @@ func init() {
5152
getCmd.Flags().BoolP("json", "j", false, "If true, the CLI will output the JSON.")
5253
getCmd.Flags().BoolP("poll", "p", true, "The CLI will poll the transcription until it's complete.")
5354
getCmd.Flags().Bool("test", false, "Flag for test executing purpose")
55+
getCmd.PersistentFlags().BoolP("srt", "", false, "Generate an SRT file for the audio file transcribed.")
5456
getCmd.Flags().MarkHidden("test")
5557
}

cmd/transcribe.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ var transcribeCmd = &cobra.Command{
4040

4141
flags.Json, _ = cmd.Flags().GetBool("json")
4242
flags.Poll, _ = cmd.Flags().GetBool("poll")
43+
flags.Srt, _ = cmd.Flags().GetBool("srt")
4344
params.AutoChapters, _ = cmd.Flags().GetBool("auto_chapters")
4445
params.AutoHighlights, _ = cmd.Flags().GetBool("auto_highlights")
4546
params.ContentModeration, _ = cmd.Flags().GetBool("content_moderation")
@@ -265,8 +266,8 @@ func init() {
265266
transcribeCmd.PersistentFlags().BoolP("auto_chapters", "s", false, "A \"summary over time\" for the audio file transcribed.")
266267
transcribeCmd.PersistentFlags().BoolP("auto_highlights", "a", false, "Automatically detect important phrases and words in the text.")
267268
transcribeCmd.PersistentFlags().BoolP("content_moderation", "c", false, "Detect if sensitive content is spoken in the file.")
268-
transcribeCmd.PersistentFlags().BoolP("dual_channel", "d", false, "Enable dual channel")
269269
transcribeCmd.PersistentFlags().BoolP("disfluencies", "D", false, "Include Filler Words in your transcripts")
270+
transcribeCmd.PersistentFlags().BoolP("dual_channel", "d", false, "Enable dual channel")
270271
transcribeCmd.PersistentFlags().BoolP("entity_detection", "e", false, "Identify a wide range of entities that are spoken in the audio file.")
271272
transcribeCmd.PersistentFlags().BoolP("format_text", "f", true, "Enable text formatting")
272273
transcribeCmd.PersistentFlags().BoolP("json", "j", false, "If true, the CLI will output the JSON.")
@@ -276,6 +277,7 @@ func init() {
276277
transcribeCmd.PersistentFlags().BoolP("redact_pii", "r", false, "Remove personally identifiable information from the transcription.")
277278
transcribeCmd.PersistentFlags().BoolP("sentiment_analysis", "x", false, "Detect the sentiment of each sentence of speech spoken in the file.")
278279
transcribeCmd.PersistentFlags().BoolP("speaker_labels", "l", true, "Automatically detect the number of speakers in your audio file, and each word in the transcription text can be associated with its speaker.")
280+
transcribeCmd.PersistentFlags().BoolP("srt", "", false, "Generate an SRT file for the audio file transcribed.")
279281
transcribeCmd.PersistentFlags().BoolP("summarization", "m", false, "Generate a single abstractive summary of the entire audio.")
280282
transcribeCmd.PersistentFlags().BoolP("topic_detection", "t", false, "Label the topics that are spoken in the file.")
281283
transcribeCmd.PersistentFlags().StringP("boost_param", "z", "", "Control how much weight should be applied to your boosted keywords/phrases. This value can be either low, default, or high.")

schemas/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ type UploadResponse struct {
179179
type TranscribeFlags struct {
180180
Poll bool `json:"poll"`
181181
Json bool `json:"json"`
182+
Srt bool `json:"srt"`
182183
}
183184

184185
type TranscribeParams struct {

utils/transcribe.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,9 @@ func getFormattedOutput(transcript S.TranscriptResponse, flags S.TranscribeFlags
357357
fmt.Fprintf(os.Stdin, "\033[1m%s\033[0m\n", "Summary")
358358
summaryPrintFormatted(transcript.Summary)
359359
}
360+
if flags.Srt {
361+
srtDownloadTranscript(*transcript.ID, transcript.Words)
362+
}
360363
}
361364

362365
func textPrintFormatted(text string, words []S.SentimentAnalysisResult) {
@@ -589,6 +592,28 @@ func summaryPrintFormatted(summary *string) {
589592
fmt.Println()
590593
}
591594

595+
func srtDownloadTranscript(id string, words []S.SentimentAnalysisResult) {
596+
// Create a new file.
597+
f, err := os.Create(fmt.Sprintf("%s.srt", id))
598+
if err != nil {
599+
fmt.Println("Could not create .srt file, please check your permissions")
600+
return
601+
}
602+
defer f.Close()
603+
604+
output := GetSrtText(words)
605+
606+
// Write bytes to file.
607+
_, err = f.WriteString(output)
608+
if err != nil {
609+
fmt.Println("Could not write to .srt file, please check your permissions")
610+
f.Close()
611+
return
612+
}
613+
614+
fmt.Printf("Successfully created file %s.srt\n", id)
615+
}
616+
592617
type ArrayCategories struct {
593618
Score float64 `json:"score"`
594619
Category string `json:"category"`

utils/utils.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,26 @@ func GetSentenceTimestamps(sentences []string, words []S.SentimentAnalysisResult
288288
return timestamps
289289
}
290290

291+
func GetSrtText(words []S.SentimentAnalysisResult) string {
292+
srtText := ""
293+
index := 1
294+
sentence := ""
295+
var start string
296+
for _, word := range words {
297+
if sentence == "" {
298+
start = TransformMsToTimestamp(*word.Start)
299+
}
300+
sentence += word.Text + " "
301+
if strings.Contains(word.Text, ".") {
302+
srtText += fmt.Sprintf("%d\n%s --> %s\n%s\n\n", index, start, TransformMsToTimestamp(*word.End), sentence)
303+
sentence = ""
304+
index++
305+
}
306+
}
307+
308+
return srtText
309+
}
310+
291311
func GetSentenceTimestampsAndSpeaker(sentences []string, words []S.SentimentAnalysisResult) [][]string {
292312
var lastIndex int
293313
timestamps := [][]string{}

0 commit comments

Comments
 (0)