Skip to content

Commit

Permalink
#1206 implemented getInfoForAudioNormalization(), which executes the …
Browse files Browse the repository at this point in the history
…first pass of loudnorm filter, and store the information in the struct InfoForAudioNormalization. The information will be used in the second pass.
  • Loading branch information
YiranDuan721 committed Nov 6, 2023
1 parent e337d87 commit dc6f11e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
File renamed without changes.
53 changes: 53 additions & 0 deletions worker/worker/transcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package worker
import (
"bufio"
"context"
"encoding/json"
"fmt"
"github.com/TUM-Dev/gocast/worker/cfg"
"github.com/TUM-Dev/gocast/worker/pb"
Expand All @@ -15,6 +16,58 @@ import (
"time"
)

type InfoForAudioNormalization struct {
InputI string `json:"input_i"`
InputTp string `json:"input_tp"`
InputLra string `json:"input_lra"`
InputThresh string `json:"input_thresh"`
OutputI string `json:"output_i"`
OutputTp string `json:"output_tp"`
OutputLra string `json:"output_lra"`
OutputThresh string `json:"output_thresh"`
NormalizationType string `json:"normalization_type"`
TargetOffset string `json:"target_offset"`
}

func getInfoForAudioNormalization(niceness int, mediaFilename string) (*InfoForAudioNormalization, error) {
c := []string{
"-n", fmt.Sprintf("%d", niceness),
"ffmpeg", "-nostats", "-y",
"-i", mediaFilename,
// The selection of loudnorm parameters follows the EBU R128 standard
"-af", "loudnorm=I=-23:TP=-2:LRA=7:print_format=json",
"-f", "null", "-"}
cmd := exec.Command("nice", c...)
output, err := cmd.CombinedOutput()
if err != nil {
return nil, err
}

lines := strings.Split(string(output), "\n")
strOfJson := "{"
shouldAppendToStrOfJson := false
for _, str := range lines {
if shouldAppendToStrOfJson {
strOfJson += str
}
if str == "{" {
shouldAppendToStrOfJson = true
} else if str == "}" {
shouldAppendToStrOfJson = false
}
}
jsonData := []byte(strOfJson)

var info InfoForAudioNormalization

err = json.Unmarshal(jsonData, &info)
if err != nil {
return nil, err
}

return &info, nil
}

func buildCommand(niceness int, infile string, outfile string, tune string, crf int) *exec.Cmd {
c := []string{
"-n", fmt.Sprintf("%d", niceness),
Expand Down

0 comments on commit dc6f11e

Please sign in to comment.