lossless concatenation of ogg vorbis files

I have a couple of ogg vorbis files, all encoded with the exact same properties that I want to concatenate into a single file.

I know that the ogg vorbis format supports plain concatenation of multiple files like:

$ cat file1.ogg file2.ogg > output.ogg

But sadly not all players are able to understand files created like that, for example my mobile audio player and I'd avoid to buy a new one. Other programs which don't understand it are gstreamer. That method also does not work if the input ogg files happen to all have the same stream id.

Additionally, since I'd like to throw away the original files and only keep the concatenated version, I'd like the concatenation to be done lossless, just as the cat solution.

I also want to avoid concatenating the files into a lossless format like flac as this would unnecessarily blow up the file size. If that would be okay, then I could keep the original ogg files and would use less space.

It seems that ffmpeg can sometimes do it using the concat demuxer:

$ cat inputs.txt
file 'in1.ogg'
file 'in2.ogg'
$ ffmpeg -f concat -i inputs.txt -c copy out.ogg

If I look at the raw hexdump of my input files, then I can find the vorbis packets exactly represented in the output file. So I guess really no re-encoding happens.

But this does not seem to work on all input files. Sometimes (not sure what triggers this), ffmpeg would give the warning:

Non-monotonous DTS in output stream 0:0; previous: 5011328, current: 5011200; changing to 5011329. This may result in incorrect timestamps in the output file.

And then I would hear a very faint "gap" between two files. Thus this certainly is not a global solution.

Since I couldn't find one I tried to write my own tool in this stackoverflow question.

Is there a way to concatenate multiple ogg vorbis files but without re-encoding them and with only a single stream per output? Which tool is able to do that job?


Solution 1:

ffmpeg -i "concat:ogg1.ogg|ogg2.ogg|ogg3.ogg" -c copy out.ogg creates a concatenated ogg file on my system, just a little bit smaller than the separate files combined (probably because of the shared metadata). It sounds the same to me, so the concatenation should be lossless. However, this doesn't add a small gap between the files.

ffmpeg -f concat -safe 0 -i ogg1.ogg -i ogg2.ogg -c copy out.ogg is supposed to work but it gives this error on my Homebrew (macOS) ffmpeg as of 3.3.2.

[concat @ 0x7f91e8800400] Line 1: unknown keyword 'OggS'
ogg1.ogg: Invalid data found when processing input

Source: https://trac.ffmpeg.org/wiki/Concatenate