lay295 / TwitchDownloader

Twitch VOD/Clip Downloader - Chat Download/Render/Replay
MIT License
2.68k stars 261 forks source link

Normalize TBN on downloaded clips #971

Open qwelias opened 7 months ago

qwelias commented 7 months ago

Checklist

Write your feature request here

I was trying to concat clips with ffmpeg after downloading, but due to different TBNs it breaks the result with Non-monotonic DTS in output stream 0:0.

The answer to the problem is to reencode them with the same TBN as follows:

ffmpeg -i video1.mp4 -video_track_timescale 90000 video1_fixed.mp4

see explanation here: https://stackoverflow.com/a/56002050

So the feature request is to encode (or at least have an option) all downloaded clips with default TBN.

superbonaci commented 7 months ago

Why do you want to concatenate the ts files with ffmpeg and not with tsmerge?

TwitchDownloaderCLI tsmerge -i list.txt -o output.ts

ffmpeg is not working yet as it should:

ffmpeg concat destroys metadata tracks when combining TS (Transport Stream) files

superbonaci commented 6 months ago

So you want to concatenate several .mp4 files? Can you provide at least 2 Twitch IDs that have the issue so we check?

qwelias commented 6 months ago

Yes, I'm concatenating .mp4 files.

Here's some clip ids which result in 90k tbn:

Here's some clip ids which result in 15360 tbn:

All ids are for clips from the same person (same stream setup). I use ffprobe to check for TBN.

superbonaci commented 6 months ago

Each clip can have a different video track timescale, but that's a Twitch decision, TwitchDonwloaderCLI doesn't have anything to do with that. It's just a downloader not a video editor so I think shouldn't set a fixed timescale for all clips just because of that.

Download:

TwitchDownloaderCLI clipdownload -u AmusedDignifiedMilkVoteNay-q4Sh6cs0MBbJipat -q best -o 1.mp4
TwitchDownloaderCLI clipdownload -u AliveUnsightlySalmonOpieOP-2lBDq63sLxK1IBCX -q best -o 2.mp4
TwitchDownloaderCLI clipdownload -u BovineZanyDragonfruitPoooound-gWpVSGgVddyOgtVr -q best -o 3.mp4

You can concatenate them like this and will fix the timescale:

MP4Box -add 1.mp4 -cat 2.mp4 -cat 3.mp4 clips.mp4

As I said before ffmpeg has some serious bugs to be fixed.

qwelias commented 6 months ago

Would it be possible to give an option to set the TBN?

superbonaci commented 6 months ago

Would it be possible to give an option to set the TBN?

Yes it's already available: https://github.com/superbonaci/TwitchDownloader

TwitchDownloaderCLI clipdownload -u AliveUnsightlySalmonOpieOP-2lBDq63sLxK1IBCX -q best --tbn 90000 -o clip.mp4

Forcing tbn can make it play bad on certain devices.

If you want to wait until it's merged here check https://github.com/lay295/TwitchDownloader/pull/985

qwelias commented 6 months ago

A bit offtopic, but I've tried using MP4Box for concatenation, it works great until I tried adding drawtext clips in between (audio broke, drawtext clips broke). So will probably stick to ffmpeg

superbonaci commented 6 months ago

Do you have some sample drawtext clip so I can try myself? If it's a bug in MP4Box it has to be reported.

qwelias commented 6 months ago

You can try this one

ffmpeg -f lavfi -r 60 -i color=black:1920x1080 -f lavfi -i anullsrc -vf drawtext=fontfile='/usr/share/fonts/ubuntu/Ubuntu-R.ttf':fontcolor=DDDDDD:fontsize=50:expansion=none:text='ICANT play the videogame':x='(main_w-text_w)/2':y='(main_h-text_h)/2',drawtext=fontfile='/usr/share/fonts/ubuntu/Ubuntu-R.ttf':fontcolor=DDDDDD:fontsize=40:expansion=none:text='(by rakanishus)':x='(main_w-text_w)/2':y='(main_h-text_h)/2+60' -c:v libx264 -b:v 1000k -pix_fmt yuv420p -video_track_timescale 15360 -c:a aac -ar 48000 -ac 2 -sample_fmt fltp -t 2 -y ./intro.mp4

or

ffmpeg -f lavfi -i color=size=1920x1080:duration=2:rate=60:color=blue -vf "drawtext=fontfile=/usr/share/fonts/ubuntu/Ubuntu-R.ttf:fontsize=30:fontcolor=white:x=(w-text_w)/2:y=(h-text_h)/2:text='Stack Overflow'" -y intro.mp4

I just run MP4Box -catpl playlist merged.mp4 playlist is something like:

./intro.mp4
./clip1.mp4
./intro.mp4
./clip2.mp4
./intro.mp4
./clip3.mp4
superbonaci commented 6 months ago

MP4Box concatenates the files but does not keep the audio tracks into as one, keeps them separated because it doesn't see them as the "same". Probably because the parameters of the audio don't match exactly since were not made with the same encoder. If you concatenate the same clip the will be no issue:

TwitchDownloaderCLI clipdownload -u AliveUnsightlySalmonOpieOP-2lBDq63sLxK1IBCX -q best --tbn 15360 -o clip.mp4
MP4Box -add clip.mp4 -cat clip.mp4 -cat clip.mp4 -new merged.mp4

or you can concatenate different clips but with same audio parameters (and tbn):

MP4Box -add clip.mp4 -cat clip2.mp4 -cat clip3.mp4 -new merged.mp4

When concatenating the intro with the clips, you have to remove first the metadata from the clips, because the intros don't have it and can't be concatenated, also MP4Box can't handle them properly https://github.com/gpac/gpac/issues/2784:

MP4Box -rem 3 clip.mp4

MP4Box does not merge because the audio does not match. Even if you put audio to intro will have to be with same exact parameters, and likely will not match exactly. Probably MP4Box is doing the right thing. Ffmpeg is more forgiving and allows to concatenate all the audio into a single track, but the stream can be corrupt, and you will never know if you don't play the whole merged.mp4 from beginning to end. I already reported several concat bugs to ffmpeg concat filters, but until some developers need the feature probably will never be fixed.

Both MP4Box and ffmpeg have serious bugs, so you should one program for each thing where excels, try not to do everything with one program.

superbonaci commented 6 months ago

What I would do in your case is to recode merged.mp4, since the intros will never have the same metadata/subitles and audio tracks with the same exact parameters as clip.mp4. You can concatenate them by brute force with ffmpeg but streams will be broken.

qwelias commented 6 months ago

Thanks a lot for the breakdown! Feel free to close it as soon as --tbn is released

ScrubN commented 4 months ago

I don't see any other download tools providing an option to normalize tbn. Is there a specific reason you can't write a bash script to loop over every item in a directory and run the ffmpeg command instead?