Skip to content

Commit 1a02663

Browse files
committed
Disallow invalid samplerates
1 parent a61d33b commit 1a02663

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

src/render/format.nim

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,25 @@ proc initPriority(index: float64, frame: ptr AVFrame, stream: ptr AVStream): Pri
2525

2626
proc `<`(a, b: Priority): bool = a.index < b.index
2727

28+
29+
proc checkAudioEncoder(encoder: ptr AVCodec, rate: cint) =
30+
if encoder.sample_fmts == nil:
31+
error &"{encoder.name}: No known audio formats avail."
32+
33+
if encoder.supported_samplerates == nil:
34+
return
35+
36+
var allowed: seq[cint]
37+
var i: cint = 0
38+
let samplerates = cast[ptr UncheckedArray[cint]](encoder.supported_samplerates)
39+
while samplerates[i] != 0:
40+
allowed.add samplerates[i]
41+
inc i
42+
43+
if rate notin allowed:
44+
let allowedStr = allowed.join(" ")
45+
error &"samplerate '{rate}' not allowed for {encoder.name} codec.\nAllowed: {allowedStr}"
46+
2847
proc makeMedia*(args: mainArgs, tl: v3, outputPath: string, rules: Rules, bar: Bar) =
2948
var options: Table[string, string]
3049
var movFlags: seq[string] = @[]
@@ -68,9 +87,7 @@ proc makeMedia*(args: mainArgs, tl: v3, outputPath: string, rules: Rules, bar: B
6887
var (aOutStream, aEncCtx) = output.addStream(args.audioCodec, rate = rate,
6988
layout = tl.layout, metadata = {"language": "und"}.toTable)
7089
let encoder = aEncCtx.codec
71-
if encoder.sample_fmts == nil:
72-
error &"{encoder.name}: No known audio formats avail."
73-
90+
checkAudioEncoder(encoder, tl.sr)
7491
aEncCtx.open()
7592

7693
# Update stream parameters after opening encoder for formats like AAC in MKV
@@ -98,9 +115,7 @@ proc makeMedia*(args: mainArgs, tl: v3, outputPath: string, rules: Rules, bar: B
98115
var (aOutStream, aEncCtx) = output.addStream(args.audioCodec, rate = rate,
99116
layout = tl.layout, metadata = {"language": tl.a[i].lang}.toTable)
100117
let encoder = aEncCtx.codec
101-
if encoder.sample_fmts == nil:
102-
error &"{encoder.name}: No known audio formats avail."
103-
118+
checkAudioEncoder(encoder, tl.sr)
104119
aEncCtx.open()
105120

106121
# Update stream parameters after opening encoder for formats like AAC in MKV

src/util/rules.nim

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@ import std/strformat
33
import ../ffmpeg
44
import ../log
55

6-
proc defaultVideoCodec*(self: ptr AVOutputFormat): string =
6+
func defaultVideoCodec*(self: ptr AVOutputFormat): string =
77
let codecId = self.video_codec
88
if codecId != AV_CODEC_ID_NONE:
99
let codecName = avcodec_get_name(codecId)
1010
if codecName != nil:
1111
return $codecName
1212
return "none"
1313

14-
proc defaultAudioCodec*(self: ptr AVOutputFormat): string =
14+
func defaultAudioCodec*(self: ptr AVOutputFormat): string =
1515
let codecId = self.audio_codec
1616
if codecId != AV_CODEC_ID_NONE:
1717
let codecName = avcodec_get_name(codecId)
1818
if codecName != nil:
1919
return $codecName
2020
return "none"
2121

22-
proc defaultSubtitleCodec*(self: ptr AVOutputFormat): string =
22+
func defaultSubtitleCodec*(self: ptr AVOutputFormat): string =
2323
let codecId = self.subtitle_codec
2424
if codecId != AV_CODEC_ID_NONE:
2525
let codecName = avcodec_get_name(codecId)

0 commit comments

Comments
 (0)