Skip to content

Commit 2e6c690

Browse files
committed
Let Emby select audio stream
1 parent 4d4f949 commit 2e6c690

5 files changed

Lines changed: 8 additions & 27 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ It is intentionally narrow: normal API traffic is forwarded to the upstream serv
1111
- Client profile matching by `User-Agent` and `X-Emby-Authorization`.
1212
- PlaybackInfo rewriting for matched profiles.
1313
- Local FFmpeg HLS sessions under `/streambridge/transcode/`.
14-
- Audio track selection through Emby `AudioStreamIndex`, mapped to the matching FFmpeg audio stream.
14+
- Audio track selection through Emby `AudioStreamIndex`, with local transcode restart on audio changes.
1515
- Playback lifecycle tracking through Emby `/Sessions/Playing*` check-ins plus HLS access.
1616
- Conservative output target: H.264 video, AAC audio, HLS MPEG-TS segments.
1717

internal/proxy/server.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,7 @@ func (s *Server) upstreamURL(in *url.URL) string {
242242
func transcodeInputURL(upstream *url.URL, id string, r *http.Request) string {
243243
u := *upstream
244244
u.Path = singleJoiningSlash(upstream.Path, path.Join("/emby/Videos", id, "stream"))
245-
query := r.URL.Query()
246-
query.Del("AudioStreamIndex")
247-
u.RawQuery = query.Encode()
245+
u.RawQuery = r.URL.RawQuery
248246
return u.String()
249247
}
250248

internal/proxy/transcode_input_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestTranscodeInputURLPreservesStartTimeTicks(t *testing.T) {
2727
}
2828
}
2929

30-
func TestTranscodeInputURLStripsAudioStreamIndexForLocalMapping(t *testing.T) {
30+
func TestTranscodeInputURLPreservesAudioStreamIndexForEmbySelection(t *testing.T) {
3131
upstream, err := url.Parse("http://upstream.local")
3232
if err != nil {
3333
t.Fatal(err)
@@ -36,8 +36,8 @@ func TestTranscodeInputURLStripsAudioStreamIndexForLocalMapping(t *testing.T) {
3636

3737
got := transcodeInputURL(upstream, "item123", req)
3838

39-
if strings.Contains(got, "AudioStreamIndex=") {
40-
t.Fatalf("input url should not forward AudioStreamIndex to upstream when local ffmpeg maps audio: %s", got)
39+
if !strings.Contains(got, "AudioStreamIndex=2") {
40+
t.Fatalf("input url should forward AudioStreamIndex so Emby returns the selected audio track: %s", got)
4141
}
4242
if !strings.Contains(got, "MediaSourceId=source1") || !strings.Contains(got, "X-Emby-Token=abc") {
4343
t.Fatalf("input url should preserve non-audio query params: %s", got)

internal/transcode/ffmpeg_args_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func TestBuildFFmpegArgsAppliesVAAPIHardwareDecodeBeforeInput(t *testing.T) {
103103
}
104104
}
105105

106-
func TestBuildFFmpegArgsMapsRequestedEmbyAudioStreamIndex(t *testing.T) {
106+
func TestBuildFFmpegArgsMapsFirstAudioStreamReturnedByEmby(t *testing.T) {
107107
session := &Session{
108108
ID: "item123",
109109
Dir: t.TempDir(),
@@ -125,7 +125,7 @@ func TestBuildFFmpegArgsMapsRequestedEmbyAudioStreamIndex(t *testing.T) {
125125
if len(mapIndexes) < 2 {
126126
t.Fatalf("missing map args: %v", args)
127127
}
128-
if got := args[mapIndexes[1]+1]; got != "0:a:1?" {
128+
if got := args[mapIndexes[1]+1]; got != "0:a:0?" {
129129
t.Fatalf("audio map = %q, args=%v", got, args)
130130
}
131131
}

internal/transcode/manager.go

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -904,24 +904,7 @@ func buildFFmpegArgs(session *Session, request Request, options ...FFmpegOptions
904904
}
905905

906906
func audioMapArg(session *Session, request Request) string {
907-
ordinal := audioOrdinalForRequest(session, request)
908-
return fmt.Sprintf("0:a:%d?", ordinal)
909-
}
910-
911-
func audioOrdinalForRequest(session *Session, request Request) int {
912-
if request.AudioStreamIndex < 0 {
913-
return 0
914-
}
915-
media := request.Media
916-
if media.IsZero() && session != nil {
917-
media = session.Media
918-
}
919-
for _, stream := range media.AudioStreams {
920-
if stream.Index == request.AudioStreamIndex && stream.Ordinal >= 0 {
921-
return stream.Ordinal
922-
}
923-
}
924-
return 0
907+
return "0:a:0?"
925908
}
926909

927910
func appendHardwareDecodeArgs(args []string, options FFmpegOptions) []string {

0 commit comments

Comments
 (0)