|
16 | 16 | } |
17 | 17 | postData.DeviceProfile = getDeviceProfile() |
18 | 18 |
|
| 19 | + ' Dynamically adjust transcoding segment length and bitrate based on the video's |
| 20 | + ' bitrate, duration, and the device's video buffer size to prevent buffer overflow |
| 21 | + ' and playlist-too-large errors during HLS playback. |
| 22 | + if isValid(videoMetadata) and isValid(videoMetadata.json) and isValid(videoMetadata.json.MediaSources) and isValid(videoMetadata.json.MediaSources[0]) |
| 23 | + mediaSource = videoMetadata.json.MediaSources[0] |
| 24 | + sourceBitrate& = 0 |
| 25 | + videoDurationSeconds = 0 |
| 26 | + |
| 27 | + if isValid(mediaSource.Bitrate) |
| 28 | + sourceBitrate& = mediaSource.Bitrate |
| 29 | + end if |
| 30 | + |
| 31 | + if isValid(mediaSource.RunTimeTicks) and mediaSource.RunTimeTicks > 0 |
| 32 | + videoDurationSeconds = Int(mediaSource.RunTimeTicks / 10000000) |
| 33 | + end if |
| 34 | + |
| 35 | + if sourceBitrate& > 0 and videoDurationSeconds > 0 |
| 36 | + bufferSize& = getDeviceBufferSize() |
| 37 | + transcodingParams = calculateOptimalTranscodingParams(sourceBitrate&, videoDurationSeconds, bufferSize&) |
| 38 | + |
| 39 | + ' Apply optimized segment length to all video transcoding profiles |
| 40 | + if isValid(postData.DeviceProfile.TranscodingProfiles) |
| 41 | + for each profile in postData.DeviceProfile.TranscodingProfiles |
| 42 | + if isValid(profile.Type) and profile.Type = "Video" |
| 43 | + profile.SegmentLength = transcodingParams.segmentLength |
| 44 | + end if |
| 45 | + end for |
| 46 | + end if |
| 47 | + |
| 48 | + ' Reduce max streaming bitrate only when segment length alone isn't enough. |
| 49 | + ' Never set MaxStreamingBitrate below the source bitrate + 10% headroom - doing |
| 50 | + ' so would cause Jellyfin to reject a remux/direct-stream as exceeding the limit. |
| 51 | + if transcodingParams.maxBitrate > 0 |
| 52 | + minAllowedBitrate& = sourceBitrate& * 11& \ 10& |
| 53 | + if transcodingParams.maxBitrate >= minAllowedBitrate& |
| 54 | + postData.DeviceProfile.MaxStreamingBitrate = transcodingParams.maxBitrate |
| 55 | + end if |
| 56 | + end if |
| 57 | + end if |
| 58 | + end if |
| 59 | + |
19 | 60 | if subtitleTrackIndex <> SubtitleSelection.none and subtitleTrackIndex <> SubtitleSelection.notset |
20 | 61 | postData.SubtitleStreamIndex = subtitleTrackIndex |
21 | 62 | end if |
|
0 commit comments