Skip to content

Commit

Permalink
Fix audio previews for MU1/2
Browse files Browse the repository at this point in the history
  • Loading branch information
KillerX committed Nov 8, 2024
1 parent 093ba33 commit 34e3978
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 23 deletions.
82 changes: 59 additions & 23 deletions services/transcode/preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"

bccmflows "github.com/bcc-code/bcc-media-flows"
Expand All @@ -29,19 +30,14 @@ type AudioPreviewResult struct {

var previewWatermarkPath = environment.GetIsilonPrefix() + "/system/graphics/LOGO_BTV_Preview_960-540.mov"

func AudioPreview(input PreviewInput, progressCallback ffmpeg.ProgressCallback) (*AudioPreviewResult, error) {
out := &AudioPreviewResult{}

info, err := ffmpeg.ProbeFile(input.FilePath)
if err != nil {
return nil, err
}

isMU1 := strings.Contains(input.FilePath, "_MU1")
isMU2 := strings.Contains(input.FilePath, "_MU2")
type audioPreviewData struct {
FFMPEGParams []string
LanguageMap map[string]string
}

func prepareAudioPreview(isMU1, isMU2 bool, fileInfo *ffmpeg.FFProbeResult, inputFile, outputDir string) (*audioPreviewData, error) {
audioStreams := []ffmpeg.FFProbeStream{}
for _, stream := range info.Streams {
for _, stream := range fileInfo.Streams {
if stream.CodecType == "audio" {
audioStreams = append(audioStreams, stream)
}
Expand All @@ -54,59 +50,99 @@ func AudioPreview(input PreviewInput, progressCallback ffmpeg.ProgressCallback)
if len(audioStreams) == 16 {
if isMU1 {
for i, l := range bccmflows.LanguagesByMU1 {
fileName := filepath.Join(input.OutputDir, fmt.Sprintf("%d.%s.aac", i, l.ISO6391))
if l.MU1ChannelStart != i {
continue // skip duplicated languages
}

fileName := filepath.Join(outputDir, fmt.Sprintf("%d.%s.aac", i, l.ISO6391))

if l.MU1ChannelCount == 1 {
audioMap = append(audioMap, "-map", fmt.Sprintf("[0:%d]", l.MU1ChannelStart), fileName)
filterParts = append(filterParts, fmt.Sprintf("[0:%d]acopy[a%d]", l.MU1ChannelStart, i))
} else {
filterParts = append(filterParts, fmt.Sprintf("[0:%d][0:%d]amerge=inputs=2[a%d]", l.MU1ChannelStart, l.MU1ChannelStart+1, i))
audioMap = append(audioMap, "-map", fmt.Sprintf("[a%d]", i), fileName)
}

audioMap = append(audioMap, "-map", fmt.Sprintf("[a%d]", i), fileName)

fileMap[l.ISO6391] = fileName
}
} else if isMU2 {
for i, l := range bccmflows.LanguagesByMU2 {
fileName := filepath.Join(input.OutputDir, fmt.Sprintf("%d.%s.aac", i, l.ISO6391))
if l.MU2ChannelStart != i {
continue // skip duplicated languages
}

fileName := filepath.Join(outputDir, fmt.Sprintf("%d.%s.aac", i, l.ISO6391))

if l.MU2ChannelCount == 1 {
audioMap = append(audioMap, "-map", fmt.Sprintf("[0:%d]", l.MU2ChannelStart), fileName)
filterParts = append(filterParts, fmt.Sprintf("[0:%d]acopy[a%d]", l.MU2ChannelStart, i))
} else {
filterParts = append(filterParts, fmt.Sprintf("[0:%d][0:%d]amerge=inputs=2[a%d]", l.MU2ChannelStart, l.MU2ChannelStart+1, i))
audioMap = append(audioMap, "-map", fmt.Sprintf("[a%d]", i), fileName)
}

audioMap = append(audioMap, "-map", fmt.Sprintf("[a%d]", i), fileName)
fileMap[l.ISO6391] = fileName
}
} else {
return nil, fmt.Errorf("Unknow format of audio channels. Not generating preview")
return nil, fmt.Errorf("unknown format of audio channels. Not generating preview")
}

} else if len(audioStreams) == 1 && audioStreams[0].Channels == 64 {
for i, l := range bccmflows.LanguageBySoftron {
fileName := filepath.Join(input.OutputDir, fmt.Sprintf("%d.%s.aac", i, l.ISO6391))
fileName := filepath.Join(outputDir, fmt.Sprintf("%d.%s.aac", i, l.ISO6391))
filterParts = append(filterParts, fmt.Sprintf("[0:%d]pan=stereo|c0=c%d|c1=c%d[a%d]", audioStreams[0].Index, l.SoftronStartCh, l.SoftronStartCh+1, i))
audioMap = append(audioMap, "-map", fmt.Sprintf("[a%d]", i), fileName)
fileMap[l.ISO6391] = fileName
}
} else {
return out, nil
return nil, nil
}

out.AudioTracks = fileMap
// This is here to stabilize the string for unit tests
sort.Strings(filterParts)

args := []string{
"-i", input.FilePath,
"-i", inputFile,
"-c:a", "aac", "-b:a", "64k", "-ar", "44100", "-ac", "2", "-profile:a", "aac_low",
"-filter_complex", strings.Join(filterParts, ";"),
"-y",
}

args = append(args, audioMap...)

_, err = ffmpeg.Do(args, ffmpeg.ProbeResultToInfo(info), progressCallback)
return &audioPreviewData{
LanguageMap: fileMap,
FFMPEGParams: args,
}, nil
}

func AudioPreview(input PreviewInput, progressCallback ffmpeg.ProgressCallback) (*AudioPreviewResult, error) {
out := &AudioPreviewResult{}

isMU1 := strings.Contains(input.FilePath, "_MU1")
isMU2 := strings.Contains(input.FilePath, "_MU2")

info, err := ffmpeg.ProbeFile(input.FilePath)
if err != nil {
return nil, err
}

previewData, err := prepareAudioPreview(isMU1, isMU2, info, input.FilePath, input.OutputDir)
if err != nil {
return nil, err
}

if previewData == nil {
return out, nil
}

_, err = ffmpeg.Do(previewData.FFMPEGParams, ffmpeg.ProbeResultToInfo(info), progressCallback)
if err != nil {
return nil, err
}

out.AudioTracks = previewData.LanguageMap

return out, nil
}

Expand Down
Loading

0 comments on commit 34e3978

Please sign in to comment.