I am facing an issue with encoding a flac file with seektables. The ffmpeg's flac encoder does not include seektables, so I have to resort to using the flac Command Line Interface (CLI). My goal is to convert any arbitrary audio file into a seekable flac file by first passing it through ffmpeg and then through the flac encoder.
export const transcodeToFlac: AudioTranscoder<{}> = ({
source,
destination
}) => {
return new Promise((resolve, reject) => {
let totalSize = 0
const { stdout: ffmpegOutput, stderr: ffmpegError } = spawn("ffmpeg", [
"-i",
source,
"-f",
"wav",
"pipe:1"
])
const { stdout: flacOutput, stdin: flacInput, stderr: flacError } = spawn(
"flac",
["-"]
)
flacOutput.on("data", (buffer: Buffer) => {
totalSize += buffer.byteLength
})
ffmpegError.on("data", error => {
console.log(error.toString())
})
flacError.on("data", error => {
console.log(error.toString())
})
//stream.on("error", reject)
destination.on("finish", () => {
resolve({
mime: "audio/flac",
size: totalSize,
codec: "flac",
bitdepth: 16,
ext: "flac"
})
})
ffmpegOutput.pipe(flacInput)
flacOutput.pipe(destination)
})
}
Although the above code appears to be working, there seems to be an issue in the resulting flac file. The duration of the source audio is 06:14
, but the flac file shows a duration of 06:45:47
. Manual encoding of the flac without piping ffmpeg works perfectly fine, however, in a server environment where stream utilization is necessary, this approach cannot work effectively.
The following output is provided by the flac encoder during transcoding:
flac 1.3.2
Copyright (C) 2000-2009 Josh Coalson, 2011-2016 Xiph.Org Foundation
flac comes with ABSOLUTELY NO WARRANTY. This is free software, and you are
welcome to redistribute it under certain conditions. Type `flac' for details.
-: WARNING: skipping unknown chunk 'LIST' (use --keep-foreign-metadata to keep)
-: WARNING, cannot write back seekpoints when encoding to stdout
-: 0% complete, ratio=0.357
0% complete, ratio=0.432
0% complete, ratio=0.482
0% complete, ratio=0.527
0% complete, ratio=0.541
1% complete, ratio=0.554
1% complete, ratio=0.563
1% complete, ratio=0.571
size= 36297kB time=00:03:30.70 bitrate=1411.2kbits/s speed= 421x
1% complete, ratio=0.572
1% complete, ratio=0.570
1% complete, ratio=0.577
1% complete, ratio=0.583
1% complete, ratio=0.584
1% complete, ratio=0.590
1% complete, ratio=0.592
size= 64512kB time=00:06:14.49 bitrate=1411.2kbits/s speed= 421x
video:0kB audio:64512kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead:
0.000185%
-: WARNING: unexpected EOF; expected 1073741823 samples, got 16510976 samples
2% complete, ratio=0.579