Skip to content

Commit

Permalink
✨ Add mp3 split experiments
Browse files Browse the repository at this point in the history
  • Loading branch information
wesen committed Oct 2, 2023
1 parent 08d14b3 commit c32d8e8
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 2 deletions.
2 changes: 1 addition & 1 deletion cmd/mp3-slice/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func main() {
endSec = length
}

outputFilePath := filepath.Join(*outputDir, fmt.Sprintf("slice_%d.mp3", i+1))
outputFilePath := filepath.Join(*outputDir, fmt.Sprintf("slice_%.2d.mp3", i+1))
err := mp3lib.ExtractSectionToFile(*mp3FilePath, outputFilePath, startSec, endSec)
if err != nil {
fmt.Printf("Error extracting segment from %d to %d seconds: %v\n", startSec, endSec, err)
Expand Down
85 changes: 84 additions & 1 deletion cmd/transcribe/main.go
Original file line number Diff line number Diff line change
@@ -1 +1,84 @@
package transcribe
package main

import (
"context"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"sync"

openai "github.com/sashabaranov/go-openai"
)

const authToken = "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"

func transcribeFile(client *openai.Client, mp3FilePath string, out chan<- string, wg *sync.WaitGroup) {
defer wg.Done()

// Set up the audio request
req := openai.AudioRequest{
Model: openai.Whisper1,
FilePath: mp3FilePath,
Format: openai.AudioResponseFormatJSON,
}

// Call the CreateTranscription method
resp, err := client.CreateTranscription(context.Background(), req)
if err != nil {
log.Printf("Failed to transcribe %s: %v\n", mp3FilePath, err)
out <- ""
return
}

out <- resp.Text
}

func main() {
// CLI arguments
dirPath := flag.String("d", "", "Path to the directory containing MP3 files")
workers := flag.Int("w", 4, "Number of parallel workers")
flag.Parse()

if *dirPath == "" {
fmt.Println("Please specify a directory path containing MP3 files using -d flag.")
os.Exit(1)
}

// Read the directory
files, err := ioutil.ReadDir(*dirPath)
if err != nil {
log.Fatalf("Failed to read the directory: %v", err)
}

client := openai.NewClient(authToken)

var wg sync.WaitGroup
out := make(chan string, len(files))

for _, file := range files {
if strings.HasSuffix(file.Name(), ".mp3") {
wg.Add(1)
go transcribeFile(client, filepath.Join(*dirPath, file.Name()), out, &wg)

// Limit concurrent workers
for len(out) >= *workers {
<-out
}
}
}

wg.Wait()
close(out)

// Collect and reassemble transcriptions
var transcriptions []string
for transcription := range out {
transcriptions = append(transcriptions, transcription)
}

fmt.Println("Combined Transcription:", strings.Join(transcriptions, "\n"))
}

0 comments on commit c32d8e8

Please sign in to comment.