Skip to content

Commit 81a9970

Browse files
committed
Disallow invalid samplerates
1 parent a61d33b commit 81a9970

File tree

3 files changed

+29
-10
lines changed

3 files changed

+29
-10
lines changed

src/log.nim

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ proc closeTempDir*() =
116116
if tempDir != "":
117117
try:
118118
removeDir(tempDir)
119-
debug "Removed Temp Directory."
120119
except OSError:
121120
discard
122121

src/render/format.nim

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,30 @@ 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+
var allowed: seq[cint]
34+
35+
if encoder.supported_samplerates != nil:
36+
var i: cint = 0
37+
let samplerates = cast[ptr UncheckedArray[cint]](encoder.supported_samplerates)
38+
while samplerates[i] != 0:
39+
allowed.add samplerates[i]
40+
inc i
41+
else:
42+
if encoder.id == 86018: # aac_at
43+
allowed = @[48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000]
44+
else:
45+
debug "audio encoder claims to support every samplerate"
46+
return
47+
48+
if rate notin allowed:
49+
let allowedStr = allowed.join(" ")
50+
error &"samplerate '{rate}' not allowed for {encoder.name}.\nAllowed: {allowedStr}"
51+
2852
proc makeMedia*(args: mainArgs, tl: v3, outputPath: string, rules: Rules, bar: Bar) =
2953
var options: Table[string, string]
3054
var movFlags: seq[string] = @[]
@@ -68,9 +92,7 @@ proc makeMedia*(args: mainArgs, tl: v3, outputPath: string, rules: Rules, bar: B
6892
var (aOutStream, aEncCtx) = output.addStream(args.audioCodec, rate = rate,
6993
layout = tl.layout, metadata = {"language": "und"}.toTable)
7094
let encoder = aEncCtx.codec
71-
if encoder.sample_fmts == nil:
72-
error &"{encoder.name}: No known audio formats avail."
73-
95+
checkAudioEncoder(encoder, tl.sr)
7496
aEncCtx.open()
7597

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

106126
# 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)