Skip to content

Commit

Permalink
Merge pull request #26 from deepgram-devs/adding-toSRT-and-WebVTT
Browse files Browse the repository at this point in the history
Adding to srt and web vtt
  • Loading branch information
briancbarrow authored Feb 9, 2023
2 parents 68d2e90 + e0f4e19 commit 1de858b
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 35 deletions.
2 changes: 1 addition & 1 deletion deepgram/deepgram.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"net/http"
)

var sdkVersion string = "0.4.0"
var sdkVersion string = "0.5.0"
var dgAgent string = "deepgram-go-sdk/v" + sdkVersion

type Client struct {
Expand Down
60 changes: 30 additions & 30 deletions deepgram/prerecorded.go
Original file line number Diff line number Diff line change
@@ -1,58 +1,58 @@
package deepgram

type Metadata struct {
RequestId string `json:"request_id"`
TransactionKey string `json:"transaction_key"`
Sha256 string `json:"sha256"`
Created string `json:"created"`
Duration int `json:"duration"`
Channels int `json:"channels"`
RequestId string `json:"request_id"`
TransactionKey string `json:"transaction_key"`
Sha256 string `json:"sha256"`
Created string `json:"created"`
Duration float64 `json:"duration"`
Channels int `json:"channels"`
}

type Hit struct {
Confidence float64 `json:"confidence"`
Start float64 `json:"start"`
End float64 `json:"end"`
Snippet string `json:"snippet"`
Start float64 `json:"start"`
End float64 `json:"end"`
Snippet string `json:"snippet"`
}

type Search struct {
Query string `json:"query"`
Hits []Hit `json:"hits"`
Hits []Hit `json:"hits"`
}

type WordBase struct {
Word string `json:"word"`
Start float64 `json:"start"`
End float64 `json:"end"`
Confidence float64 `json:"confidence"`
Punctuated_Word string `json:"punctuated_word"`
Speaker int `json:"speaker"`
Word string `json:"word"`
Start float64 `json:"start"`
End float64 `json:"end"`
Confidence float64 `json:"confidence"`
Punctuated_Word string `json:"punctuated_word"`
Speaker int `json:"speaker"`
}

type Alternative struct {
Transcript string `json:"transcript"`
Confidence float64 `json:"confidence"`
Words []WordBase `json:"words"`
Transcript string `json:"transcript"`
Confidence float64 `json:"confidence"`
Words []WordBase `json:"words"`
}

type Channel struct {
Search []Search `json:"search"`
Search []Search `json:"search"`
Alternatives []Alternative `json:"alternatives"`
}

type Utterance struct {
Start float64 `json:"start"`
End float64 `json:"end"`
Confidence float64 `json:"confidence"`
Channel int `json:"channel"`
Transcript string `json:"transcript"`
Words []WordBase `json:"words"`
Speaker int `json:"speaker"`
Id string `json:"id"`
Start float64 `json:"start"`
End float64 `json:"end"`
Confidence float64 `json:"confidence"`
Channel int `json:"channel"`
Transcript string `json:"transcript"`
Words []WordBase `json:"words"`
Speaker int `json:"speaker"`
Id string `json:"id"`
}

type Results struct {
Utterances []Utterance `json:"utterances"`
Channels []Channel `json:"channels"`
}
Channels []Channel `json:"channels"`
}
52 changes: 48 additions & 4 deletions deepgram/transcriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,22 @@ package deepgram
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net/http"
"net/url"
"strings"

"github.com/google/go-querystring/query"
"github.com/gorilla/websocket"
)

type PreRecordedResponse struct {
Request_id string `json:"request_id"`
Metadata interface{} `json:"metadata"`
Results interface{} `json:"results"`
Request_id string `json:"request_id"`
Metadata Metadata `json:"metadata"`
Results Results `json:"results"`
}

type LiveTranscriptionOptions struct {
Expand Down Expand Up @@ -85,7 +87,6 @@ func (dg *Client) LiveTranscription(options LiveTranscriptionOptions) (*websocke
log.Fatal("dial:", err)
}
return c, resp, nil

}

func (dg *Client) PreRecordedFromURL(source UrlSource, options PreRecordedTranscriptionOptions) (PreRecordedResponse, error) {
Expand Down Expand Up @@ -130,3 +131,46 @@ func (dg *Client) PreRecordedFromURL(source UrlSource, options PreRecordedTransc
}

}

func (resp *PreRecordedResponse) ToWebVTT() (string, error) {
if resp.Results.Utterances == nil {
return "", errors.New("This function requires a transcript that was generated with the utterances feature.")
}

vtt := "WEBVTT\n\n"

vtt += "NOTE\nTranscription provided by Deepgram\nRequest ID: " + resp.Request_id + "\nCreated: " + resp.Metadata.Created + "\n\n"

for i, utterance := range resp.Results.Utterances {
utterance := utterance
start := SecondsToTimestamp(utterance.Start)
end := SecondsToTimestamp(utterance.End)
vtt += fmt.Sprintf("%d\n%s --> %s\n%s\n\n", i+1, start, end, utterance.Transcript)
}
return vtt, nil
}

func (resp *PreRecordedResponse) ToSRT() (string, error) {
if resp.Results.Utterances == nil {
return "", errors.New("This function requires a transcript that was generated with the utterances feature.")
}

srt := ""

for i, utterance := range resp.Results.Utterances {
utterance := utterance
start := SecondsToTimestamp(utterance.Start)
end := SecondsToTimestamp(utterance.End)
end = strings.ReplaceAll(end, ".", ",")
srt += fmt.Sprintf("%d\n%s --> %s\n%s\n\n", i+1, start, end, utterance.Transcript)

}
return srt, nil
}

func SecondsToTimestamp(seconds float64) string {
hours := int(seconds / 3600)
minutes := int((seconds - float64(hours*3600)) / 60)
seconds = seconds - float64(hours*3600) - float64(minutes*60)
return fmt.Sprintf("%02d:%02d:%02.3f", hours, minutes, seconds)
}
43 changes: 43 additions & 0 deletions examples/preRecordedFromUrl_example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main

import (
"fmt"
"log"
"os"

"github.com/deepgram-devs/go-sdk/deepgram"
)

func main() {
dg := deepgram.NewClient("YOUR_API_KEY")
// Feel free to use this as your audio url to test https://anchor.fm/s/3e9db190/podcast/play/22624519/https%3A%2F%2Fd3ctxlq1ktw2nl.cloudfront.net%2Fstaging%2F2020-10-15%2F128822202-44100-1-79cab5de0d7af3c9.mp3
res, err := dg.PreRecordedFromURL(deepgram.UrlSource{Url: "AUDIO_FILE_URL"}, deepgram.PreRecordedTranscriptionOptions{Punctuate: true, Diarize: true, Language: "en-US", Utterances: true})
if err != nil {
fmt.Println("ERROR", err)
return
}
// Log the results
log.Printf("recv: %+v", res.Results)
f, err := os.Create("transcription.vtt")
if err != nil {
fmt.Printf("error creating VTT file: %v", err)
}
// Convert the results to WebVTT format
vtt, err := res.ToWebVTT()
if err != nil {
log.Fatal(err)
}
f.WriteString(vtt)

// Convert the results to SRT format
srtF, err := os.Create("transcription.srt")
if err != nil {
fmt.Printf("error creating SRT file: %v", err)
}
srt, err := res.ToSRT()
if err != nil {
log.Fatal(err)
}
srtF.WriteString(srt)

}

0 comments on commit 1de858b

Please sign in to comment.