{"record":{"id":"9f34c3d52d114a48","repo":"TeamNewPipe/NewPipe","slug":"cannot-get-the-audio-sample-rate","errorCode":null,"errorMessage":"cannot get the audio sample rate","messagePattern":"cannot get the audio sample rate","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"app/src/main/java/org/schabi/newpipe/streams/OggFromWebMWriter.java","lineNumber":173,"sourceCode":"\n        source.close();\n        output.close();\n    }\n\n    public void build() throws IOException {\n        final float resolution;\n        SimpleBlock bloq;\n        final ByteBuffer header = ByteBuffer.allocate(27 + (255 * 255));\n        final ByteBuffer page = ByteBuffer.allocate(64 * 1024);\n\n        header.order(ByteOrder.LITTLE_ENDIAN);\n\n        /* step 1: get the amount of frames per seconds */\n        switch (webmTrack.kind) {\n            case Audio:\n                resolution = getSampleFrequencyFromTrack(webmTrack.bMetadata);\n                if (resolution == 0f) {\n                    throw new RuntimeException(\"cannot get the audio sample rate\");\n                }\n                break;\n            case Video:\n                // WARNING: untested\n                if (webmTrack.defaultDuration == 0) {\n                    throw new RuntimeException(\"missing default frame time\");\n                }\n                resolution = 1000f / ((float) webmTrack.defaultDuration\n                        / webmSegment.info.timecodeScale);\n                break;\n            default:\n                throw new RuntimeException(\"not implemented\");\n        }\n\n        /* step 2: create packet with code init data */\n        if (webmTrack.codecPrivate != null) {\n            addPacketSegment(webmTrack.codecPrivate.length);\n            makePacketheader(0x00, header, webmTrack.codecPrivate);","sourceCodeStart":155,"sourceCodeEnd":191,"githubUrl":"https://github.com/TeamNewPipe/NewPipe/blob/9e8be091560a69f35d44bd252eb00bc7911c977b/app/src/main/java/org/schabi/newpipe/streams/OggFromWebMWriter.java#L155-L191","documentation":"OggFromWebMWriter parses a WebM Audio track and calls getSampleFrequencyFromTrack(track.bMetadata) to extract the sampling rate. If the result is 0.0f the audio configuration is unusable (Ogg/Vorbis needs a real sample rate for granule positions) and a RuntimeException is thrown. The metadata blob lacked or had a corrupt SamplingFrequency element.","triggerScenarios":"Writing Ogg from a WebM audio track whose codec-private metadata block does not contain a parseable sampling frequency. This happens for audio tracks missing the SamplingFrequency EBML element or with a zero value.","commonSituations":"An Opus/Vorbis WebM where the track metadata is incomplete; a malformed WebM from a buggy encoder; a track extracted incorrectly from a higher-level container; an unusual codec whose metadata this writer does not parse.","solutions":["Use a different/known-good source WebM whose audio track exposes a valid sampling frequency.","Re-encode the source with ffmpeg so the audio track metadata is well-formed.","Pre-parse the track metadata and reject tracks whose sample frequency is 0 before invoking the writer.","Provide the audio track from a different extractor itag that includes complete metadata."],"exampleFix":"// before\nnew OggFromWebMWriter(source, out).write(); // throws if metadata lacks sample rate\n\n// after — pre-check the track before writing\nWebMReader r = new WebMReader(source);\nr.parse();\nWebMTrack t = r.getAvailableTracks()[0];\nif (getSampleFrequencyFromTrack(t.bMetadata) == 0f) {\n    throw new IOException(\"audio track has no sample rate; pick another source\");\n}","handlingStrategy":"validation","validationCode":"// Pre-parse the audio track metadata and reject a zero sample rate before writing.\nWebMReader r = new WebMReader(source);\nr.parse();\nWebMTrack audio = selectAudioTrack(r);\nif (getSampleFrequencyFromTrack(audio.bMetadata) == 0f) {\n    throw new IOException(\"audio track exposes no sample rate; choose another source\");\n}","typeGuard":"public static boolean audioTrackHasSampleRate(WebMTrack t) {\n    return t.kind == WebMTrack.Kind.Audio\n        && getSampleFrequencyFromTrack(t.bMetadata) != 0f;\n}","tryCatchPattern":"try {\n    writer.write();\n} catch (RuntimeException e) {\n    if (e.getMessage().contains(\"sample rate\")) {\n        // metadata lacks sample rate — pick a different source/itag\n        selectAlternateAudioSource();\n    } else throw e;\n}","preventionTips":["Validate the audio track's sample rate before invoking the writer.","Prefer well-formed WebM sources from major platforms.","Re-encode malformed sources with ffmpeg to repair metadata.","Filter tracks by kind==Audio before selection."],"tags":["ogg","webm","audio","metadata","java"],"backgroundTag":null,"analyzedSha":"9e8be091560a69f35d44bd252eb00bc7911c977b","analyzedAt":"2026-08-14T00:11:33.519Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}