fix: parse ffprobe sample rates strictly · openclaw/openclaw@f3a23f8
steipete
·
2026-05-29
·
via Recent Commits to openclaw:main
| Original file line number | Diff line number | Diff line change |
|---|
@@ -99,6 +99,38 @@ describe("parseFfprobeCodecAndSampleRate", () => {
|
99 | 99 | sampleRateHz: null, |
100 | 100 | }, |
101 | 101 | }, |
| 102 | +{ |
| 103 | +name: "rejects partially numeric sample rates", |
| 104 | +input: "opus,48000hz", |
| 105 | +expected: { |
| 106 | +codec: "opus", |
| 107 | +sampleRateHz: null, |
| 108 | +}, |
| 109 | +}, |
| 110 | +{ |
| 111 | +name: "rejects missing sample rates", |
| 112 | +input: "opus,", |
| 113 | +expected: { |
| 114 | +codec: "opus", |
| 115 | +sampleRateHz: null, |
| 116 | +}, |
| 117 | +}, |
| 118 | +{ |
| 119 | +name: "rejects zero sample rates", |
| 120 | +input: "opus,0", |
| 121 | +expected: { |
| 122 | +codec: "opus", |
| 123 | +sampleRateHz: null, |
| 124 | +}, |
| 125 | +}, |
| 126 | +{ |
| 127 | +name: "rejects signed sample rates", |
| 128 | +input: "opus,-48000", |
| 129 | +expected: { |
| 130 | +codec: "opus", |
| 131 | +sampleRateHz: null, |
| 132 | +}, |
| 133 | +}, |
102 | 134 | ] as const)("$name", ({ input, expected }) => { |
103 | 135 | expectParsedCodecAndSampleRateCase(input, expected); |
104 | 136 | }); |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -92,15 +92,22 @@ export function parseFfprobeCsvFields(stdout: string, maxFields: number): string
|
92 | 92 | .map((field) => normalizeLowercaseStringOrEmpty(field)); |
93 | 93 | } |
94 | 94 | |
| 95 | +function parseFfprobeSampleRateHz(value: string | undefined): number | null { |
| 96 | +if (!value || !/^\d+$/.test(value)) { |
| 97 | +return null; |
| 98 | +} |
| 99 | +const sampleRate = Number(value); |
| 100 | +return Number.isSafeInteger(sampleRate) && sampleRate > 0 ? sampleRate : null; |
| 101 | +} |
| 102 | + |
95 | 103 | export function parseFfprobeCodecAndSampleRate(stdout: string): { |
96 | 104 | codec: string | null; |
97 | 105 | sampleRateHz: number | null; |
98 | 106 | } { |
99 | 107 | const [codecRaw, sampleRateRaw] = parseFfprobeCsvFields(stdout, 2); |
100 | 108 | const codec = codecRaw ? codecRaw : null; |
101 | | -const sampleRate = sampleRateRaw ? Number.parseInt(sampleRateRaw, 10) : Number.NaN; |
102 | 109 | return { |
103 | 110 | codec, |
104 | | -sampleRateHz: Number.isFinite(sampleRate) ? sampleRate : null, |
| 111 | +sampleRateHz: parseFfprobeSampleRateHz(sampleRateRaw), |
105 | 112 | }; |
106 | 113 | } |
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。