Is there any way to repair corrupted mp3 file and preserve timestamps and duration by replacing bad packets with silence?
Depends in what way it is corrupted. Also depends how the MP3 file was encoded. If it's a VBR file, you're kinda out of luck. When parts are unreadable it is impossible to tell the duration of the corrupted section. If it's CBR and the header is intact, you could perhaps reject parts of the file by calculating an assumed timeframe. If there is software that'll do it for you I don't know, but I remember FFmpeg could skip over broken sections of a file when reading. However I'm not sure whether is it capable of reencoding that file into some other format (such as simply decompressing it), while doing it. Especially not if it's able to preserve putting sections at correct points in time.
It's VBR, the header is OK.
An only useful tool I was able to find it's mp3DirectCut.
Using "Check for re-syncs" and then "Save split" it splits the bad mp3 file by those broken parts removing corrupted packets at the end of the each segment. Then using timestamps from Cue (created by "Check for re-syncs") it's possible to calculate differences between duration of original segments and splitted parts, create "patches" using ffmpeg -f lavfi -i anullsrc... , concatenate them back to one file.
But it's such a tedious job.
Maybe there is an better way? Some tool?
I can't believe that nobody has faced this sync problem before.
I don’t know if it can help in your particular case, but I’ve usually gotten good results using MP3 Diags (http://mp3diags.sourceforge.net/) when repairing MP3s. (It’s astounding how many are actually broken somehow.)
(It’s astounding how many are actually broken somehow.)
Yeah, MP3 players would silently ignore whatever they couldn't understand, and so when utilities would make all sorts of junk, the developer would never be yelled at, and so it went on ...
It does not help that the old ISO mp3 is a really shitty format (*cough* gaplessness ...), which invited all sort of "non-standard enhancements" retrofit into it.
(Before I get TOS#8 thrown at me: I didn't say mp3 streams are bad to listen to, but the
file format sucked big time.)