@@ -5,6 +5,7 @@ package cmd
55
66import (
77 "bytes"
8+ "context"
89 "encoding/json"
910 "fmt"
1011 "math"
@@ -18,11 +19,12 @@ import (
1819 "text/tabwriter"
1920 "time"
2021
22+ "github.com/briandowns/spinner"
2123 "github.com/spf13/cobra"
2224 "golang.org/x/term"
25+ pb "gopkg.in/cheggaaa/pb.v1"
2326)
2427
25- // transcribeCmd represents the transcribe command
2628var transcribeCmd = & cobra.Command {
2729 Use : "transcribe <url | path | youtube URL>" ,
2830 Short : "A brief description of your command" ,
@@ -85,9 +87,8 @@ func init() {
8587}
8688
8789func transcribe (params TranscribeParams , flags TranscribeFlags ) {
88- token := GetStoredToken ()
89-
90- if token == "" {
90+ Token = GetStoredToken ()
91+ if Token == "" {
9192 fmt .Println ("You must login first. Run `assemblyai config <token>`" )
9293 return
9394 }
@@ -109,7 +110,7 @@ func transcribe(params TranscribeParams, flags TranscribeFlags) {
109110 }
110111 youtubeVideoURL := YoutubeDownload (youtubeId )
111112 if youtubeVideoURL == "" {
112- fmt .Println ("Please try again with a different one." )
113+ fmt .Println (" Please try again with a different one." )
113114 return
114115 }
115116 params .AudioURL = youtubeVideoURL
@@ -122,7 +123,7 @@ func transcribe(params TranscribeParams, flags TranscribeFlags) {
122123 }
123124 }
124125 } else {
125- uploadedURL := uploadFile ( token , params .AudioURL )
126+ uploadedURL := UploadFile ( params .AudioURL )
126127 if uploadedURL == "" {
127128 fmt .Println ("The file doesn't exist. Please try again with a different one." )
128129 return
@@ -135,7 +136,7 @@ func transcribe(params TranscribeParams, flags TranscribeFlags) {
135136
136137 TelemetryCaptureEvent ("CLI transcription created" , nil )
137138 body := bytes .NewReader (paramsJSON )
138- response := QueryApi (token , "/transcript" , "POST" , body )
139+ response := QueryApi ("/transcript" , "POST" , body )
139140 var transcriptResponse TranscriptResponse
140141 if err := json .Unmarshal (response , & transcriptResponse ); err != nil {
141142 fmt .Println ("Can not unmarshal JSON" )
@@ -153,7 +154,7 @@ func transcribe(params TranscribeParams, flags TranscribeFlags) {
153154 return
154155 }
155156
156- PollTranscription (token , * id , flags )
157+ PollTranscription (* id , flags )
157158}
158159
159160func isUrl (str string ) bool {
@@ -179,7 +180,7 @@ func checkAAICDN(url string) bool {
179180 return strings .HasPrefix (url , "https://cdn.assemblyai.com/" )
180181}
181182
182- func uploadFile ( token string , path string ) string {
183+ func UploadFile ( path string ) string {
183184 isAbs := filepath .IsAbs (path )
184185 if ! isAbs {
185186 wd , err := os .Getwd ()
@@ -196,47 +197,91 @@ func uploadFile(token string, path string) string {
196197 }
197198
198199 TelemetryCaptureEvent ("CLI upload started" , nil )
199- s := CallSpinner (" Your file is being uploaded..." )
200- response := QueryApi (token , "/upload" , "POST" , file )
201200
201+ fileInfo , _ := file .Stat ()
202+ bar := pb .New (int (fileInfo .Size ()))
203+ bar .SetUnits (pb .U_BYTES_DEC )
204+ bar .Prefix (" Uploading file to our servers: " )
205+ bar .ShowBar = false
206+ bar .ShowTimeLeft = false
207+ bar .Start ()
208+
209+ response := QueryApi ("/upload" , "POST" , bar .NewProxyReader (file ))
210+
211+ bar .Finish ()
202212 var uploadResponse UploadResponse
203213 if err := json .Unmarshal (response , & uploadResponse ); err != nil {
204214 return ""
205215 }
206- s .Stop ()
207216 TelemetryCaptureEvent ("CLI upload ended" , nil )
208217
209218 return uploadResponse .UploadURL
210219}
211220
212- func PollTranscription (token string , id string , flags TranscribeFlags ) {
213- fmt .Println ("Your file is being transcribed (id " + id + ")..." )
214- s := CallSpinner (" Processing time is usually 20% of the file's duration." )
215- for {
216- response := QueryApi (token , "/transcript/" + id , "GET" , nil )
221+ func PollTranscription (id string , flags TranscribeFlags ) {
222+ fmt .Println (" Transcribing file with id " + id )
223+ showProgressBar := TranscriptionLength != 0
224+ timePercentage := (TranscriptionLength * 30 ) / 100
225+
226+ ctx , cancelCtx := context .WithCancel (context .Background ())
227+ defer cancelCtx ()
228+
229+ var s * spinner.Spinner
230+ var bar * pb.ProgressBar
231+
232+ if showProgressBar {
233+ fmt .Println (" Processing time is usually 20% of the file's duration." )
234+ bar = pb .StartNew (timePercentage )
235+ go showProgress (timePercentage , ctx , bar )
236+ } else {
237+ s = CallSpinner (" Processing time is usually 20% of the file's duration." )
238+ }
217239
240+ for {
241+ response := QueryApi ("/transcript/" + id , "GET" , nil )
218242 if response == nil {
219- s .Stop ()
220- fmt .Printf ("\033 [1A\033 [2K" )
243+ if showProgressBar {
244+ cancelCtx ()
245+ bar .Set (timePercentage )
246+ bar .Finish ()
247+ } else {
248+ s .Stop ()
249+ }
221250 fmt .Println ("Something went wrong. Please try again later." )
222251 return
223252 }
224253 var transcript TranscriptResponse
225254 if err := json .Unmarshal (response , & transcript ); err != nil {
226255 fmt .Println (err )
227- s .Stop ()
228- fmt .Printf ("\033 [1A\033 [2K" )
256+ if showProgressBar {
257+ cancelCtx ()
258+ bar .Set (timePercentage )
259+ bar .Finish ()
260+ } else {
261+ s .Stop ()
262+ }
229263 return
230264 }
231265 if transcript .Error != nil {
232- s .Stop ()
233- fmt .Printf ("\033 [1A\033 [2K" )
266+ if showProgressBar {
267+ cancelCtx ()
268+ bar .Set (timePercentage )
269+ bar .Finish ()
270+ } else {
271+ s .Stop ()
272+ }
234273 fmt .Println (* transcript .Error )
235274 return
236275 }
237276 if * transcript .Status == "completed" {
277+ if showProgressBar {
278+ cancelCtx ()
279+ bar .Set (timePercentage )
280+ bar .Finish ()
281+ } else {
282+ s .Stop ()
283+ }
238284 var properties * PostHogProperties = new (PostHogProperties )
239-
240285 properties .Poll = flags .Poll
241286 properties .Json = flags .Json
242287 properties .AutoChapters = * transcript .AutoChapters
@@ -252,8 +297,7 @@ func PollTranscription(token string, id string, flags TranscribeFlags) {
252297 properties .TopicDetection = * transcript .IabCategories
253298
254299 TelemetryCaptureEvent ("CLI transcription finished" , properties )
255- s .Stop ()
256- fmt .Printf ("\033 [1A\033 [2K" )
300+
257301 if flags .Json {
258302 print := BeutifyJSON (response )
259303 fmt .Println (string (print ))
@@ -262,7 +306,7 @@ func PollTranscription(token string, id string, flags TranscribeFlags) {
262306 getFormattedOutput (transcript , flags )
263307 return
264308 }
265- time .Sleep (5 * time .Second )
309+ time .Sleep (3 * time .Second )
266310 }
267311}
268312
@@ -271,7 +315,7 @@ func getFormattedOutput(transcript TranscriptResponse, flags TranscribeFlags) {
271315 if err != nil {
272316 width = 512
273317 }
274-
318+ fmt . Print ( " \033 [H \033 [2J" )
275319 fmt .Println ("Transcript" )
276320 if transcript .SpeakerLabels == true {
277321 speakerLabelsPrintFormatted (transcript .Utterances , width )
0 commit comments