Skip to main content

Notice

Please note that most of the software linked on this forum is likely to be safe to use. If you are unsure, feel free to ask in the relevant topics, or send a private message to an administrator or moderator. To help curb the problems of false positives, or in the event that you do find actual malware, you can contribute through the article linked here.
Topic: How to merge opus files w/o "click" gap sound? (Read 2526 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

How to merge opus files w/o "click" gap sound?

I'm trying to merge opus files (and generate ogg chapters, cue files) with a custom script.

Unlike mp3 which seems to be mergeable without audio artifacts using mp3cat or mp3binder, I haven't succeeded in gaplessly merging opus files yet. I tried ffmpeg concat, mkvmerge and mp4box - each leaves a short "click" sound beween segments/chapters.

=> Is there a tool or method that merges opus files without this "click" sound?

I've read that the real deal is to decode to pcm, merge and recode - but I don't want to recode, just merge opus files with same settings (in my case vbr). I've discovered that delting the metadata in the segments reduces the filesize a little, but the "click" sound remains.

Re: How to merge opus files w/o "click" gap sound?

Reply #1
Have you tried simply concatenating the files? The Ogg container can be chained with that. Not all players support it though.

ffmpeg concat does concatenating of the packets as far as I know, but for this to work, you really need to concatenate the files. On a *nix shell that is simply cat file1.opus file2.opus > newfile.opus, don't know how to do that on Windows.
Music: sounds arranged such that they construct feelings.

Re: How to merge opus files w/o "click" gap sound?

Reply #2
Have you tried simply concatenating the files? The Ogg container can be chained with that. Not all players support it though.

Simply joining the files (Windows: copy /b 1.opus + 2.opus 0.opus) has the same drawback, a "click" sound at the seams.

Interestingly, Foobar2000 (Desktop) does show the joined files as individual chaptes - though I'd rather use a cue file and/or official ogg chapters. And, as you mentioned, this methods seems to be less compatible - my video player only shows the first segment, I didn't try mobile apps.

ffmpeg concat does concatenating of the packets as far as I know,

There are two concat methods in ffmpeg, stream or file level: https://trac.ffmpeg.org/wiki/Concatenate

The file level protocol ("This is analogous to using cat on UNIX-like systems or copy on Windows.") seems to be different from a command line copy though as it doesn't show the segments as chapters (but ffmpeg's method probably is more compatible).

Re: How to merge opus files w/o "click" gap sound?

Reply #3
Doesn't mp3 use a fixed 1152 blocksize? Can the last frame be less or is it always 1152 padded with silence? That would explain how mp3 can be losslessly concatenated. Otherwise mp3binder would have to re-encode the last frame to pad to 1152 size itself to maintain mostly-lossless merge status, it's either that or re-encode the entirety of the second track which isn't smart.

Try transcoding the opus that clicks to mp3 then merge them. If the length is slightly bigger than the opus merge then the silence exists and is probably why there's no click with mp3 but a click with opus.

AFAIK opus uses a fixed blocksize but that blocksize can change from stream to stream. Lossless merging that isn't chaining probably is possible (although tools might not exist to do it), but would have to use the same blocksize in addition to the standard sample rate/channels/bps. And pad what is probably an undersized last frame to the full blocksize.

Re: How to merge opus files w/o "click" gap sound?

Reply #4
Try transcoding the opus that clicks to mp3 then merge them. If the length is slightly bigger than the opus merge then the silence exists and is probably why there's no click with mp3 but a click with opus.

Doh. I had suspeced a problem with "preskip", but this indicates the problem probably is the last frame. The opus file from "mp3 => merge (mp3binder) => encode" has a slightly longer duration than "individual mp3 => encode => merge (copy/cat, ffmpeg, mp4box, mkvtoolnix)".

AFAIK opus uses a fixed blocksize but that blocksize can change from stream to stream. Lossless merging that isn't chaining probably is possible (although tools might not exist to do it), but would have to use the same blocksize in addition to the standard sample rate/channels/bps. And pad what is probably an undersized last frame to the full blocksize.

What a pity that opus isn't as tooled up as legacy mp3 yet. Where would one request such a tool to pad-merge opus files - the official libopus repo, or gpac, mkvtoolnix? I guess padding/modifying input streams might not be the job of gpac/mkvtoolnix though - even if mkvtoolnix is able to generate mkv chapters from a bunch of input files.

Re: How to merge opus files w/o "click" gap sound?

Reply #5
Searching for ogg instead of opus shows this is a well-known problem - without a solution (yet).

There was some experimental C code back in 2015 - which seemed to be an improvament at least. I didn't compile it and try it on opus files. https://stackoverflow.com/questions/27980960/how-to-lossless-concatenate-ogg-vorbis-files ... here's some more information on the "click"-gap: https://github.com/villermen/runescape-cache-tools/issues/8

The ancient 2010 OggCat doesn't at all on opus (even in an ogg container), I just tried.


Re: How to merge opus files w/o "click" gap sound?

Reply #6
I'm trying to merge opus files (and generate ogg chapters, cue files) with a custom script.
Did you encode these files yourself, or did you get them from somewhere else?

AFAIK opus uses a fixed blocksize but that blocksize can change from stream to stream. Lossless merging that isn't chaining probably is possible (although tools might not exist to do it), but would have to use the same blocksize in addition to the standard sample rate/channels/bps. And pad what is probably an undersized last frame to the full blocksize.
Lossless merging that isn't chaining is always possible for mono and stereo Opus regardless of the other stream parameters. The merge won't be seamless, though, since the first stream's end trim and the second stream's pre-skip will both be decoded.

Streams that were initially encoded gaplessly can be merged seamlessly by discarding duplicate frame(s).

Re: How to merge opus files w/o "click" gap sound?

Reply #7
I'm trying to merge opus files (and generate ogg chapters, cue files) with a custom script.
Did you encode these files yourself, or did you get them from somewhere else?

Im encoding to opus myself, it's about saving space for audio books that are kept on my mobile device

The merge won't be seamless, though, since the first stream's end trim and the second stream's pre-skip will both be decoded.

I don't really care about "gapless", but about the very audible "click" sound between segments. As you pointed out, mp3 might be easier to handle, but reducing (like the attempt above from 2015) or eliminating the "click" with opus would be really welcome.

The only way to prevent the "click" at the moment ist to either use mp3binder/mp3cat (if the source happens to be mp3) before transcoding, or to convert to pcm => cat/copy => encode. Some audio books are in m4b format which eliminates this problem.

Re: How to merge opus files w/o "click" gap sound?

Reply #8
Im encoding to opus myself,
Your best option is to merge them into one file before encoding to Opus. Depending on the source file format, it might be best to decode the original files to some kind of PCM (e.g. WAV) before merging.

This is also how encoding gapless Opus files works; the only difference is that here you're not trying to split it into separate files after encoding.

I don't see why you want to avoid re-encoding. You still have the original source files, right? You can run multiple encodes at the same time to speed things up.

Re: How to merge opus files w/o "click" gap sound?

Reply #9
Joining multiple opus files without error from opusinfo.

Tools used:

ffmpeg
MP4box

1. Give all opus files a MP4 container
for i in *.opus; do ffmpeg -i "$i" -vn -c:a copy "${i%.opus}.mp4"; done

2. Write script : nano joiner.sh

#!/bin/bash
filesList=""
for file in $(ls *.mp4|sort -n);do
    filesList="$filesList -cat $file"
done
MP4Box $filesList -new merged_files_$(date +%Y%m%d_%H%M%S).mp4

3. chmod +x joiner.sh

4. run the script ./joiner.sh

5. ffmpeg -i merged_files_XXXXXXXX_XXXXXX.mp4 -vn -acodec copy testy.opus

6. opusinfo testy.opus

No granule errors !

Happy joining...