lay295 / TwitchDownloader

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

metadata.txt is missing chapters under certain conditions #1005

Closed superbonaci closed 6 months ago

superbonaci commented 6 months ago

Checklist

Edition

Command Line Interface d02269d

Describe your issue here

This command generates this metadata.txt file:

TwitchDownloaderCLI.exe videodownload -q 360p -o clip.mp4 -u 2065608792
;FFMETADATA1
title=🐳 Finishing Bread & Fred with @limealicious | !newvid !supps (2065608792)
artist=bao
date=2024
comment=Originally aired: 2024-02-17 18:05:48Z\
Video id: 2065608792\
Views: 49015
[CHAPTER]
TIMEBASE=1/1000
START=0
END=2333000
title=Just Chatting
[CHAPTER]
TIMEBASE=1/1000
START=2333000
END=8125000
title=Bread & Fred
[CHAPTER]
TIMEBASE=1/1000
START=8125000
END=9114000
title=Just Chatting
[CHAPTER]
TIMEBASE=1/1000
START=9114000
END=24980000
title=Palworld

This other command generates this metadata.txt file:

TwitchDownloaderCLI.exe videodownload -q 360p -o clip.mp4 -u 2065608792 -b 8025 -e 9114
;FFMETADATA1
title=🐳 Finishing Bread & Fred with @limealicious | !newvid !supps (2065608792)
artist=bao
date=2024
comment=Originally aired: 2024-02-17 18:05:48Z\
Video id: 2065608792\
Views: 49015
[CHAPTER]
TIMEBASE=1/1000
START=-8025000
END=-5692000
title=Just Chatting
[CHAPTER]
TIMEBASE=1/1000
START=-5692000
END=100000
title=Bread & Fred
[CHAPTER]
TIMEBASE=1/1000
START=100000
END=1089000
title=Just Chatting

However there's a chapter missing, so should be like this:

;FFMETADATA1
title=🐳 Finishing Bread & Fred with @limealicious | !newvid !supps (2065608792)
artist=bao
date=2024
comment=Originally aired: 2024-02-17 18:05:48Z\
Video id: 2065608792\
Views: 49015
[CHAPTER]
TIMEBASE=1/1000
START=-8025000
END=-5692000
title=Just Chatting
[CHAPTER]
TIMEBASE=1/1000
START=-5692000
END=100000
title=Bread & Fred
[CHAPTER]
TIMEBASE=1/1000
START=100000
END=1089000
title=Just Chatting
[CHAPTER]
TIMEBASE=1/1000
START=1089000
END=16955000
title=Palworld

There must exist the same number of chapters keeping each one the same duration, just shift them in time according to -b.

Add any related files or extra information here

No response

ScrubN commented 6 months ago

This is literally what you asked for in #1003.

superbonaci commented 6 months ago

If #1003 were properly fixed it should be like this:

TwitchDownloaderCLI.exe videodownload -q 360p -o clip.mp4 -u 2065608792 -b 8025 -e 9114
;FFMETADATA1
title=🐳 Finishing Bread & Fred with @limealicious | !newvid !supps (2065608792)
artist=bao
date=2024
comment=Originally aired: 2024-02-17 18:05:48Z\
Video id: 2065608792\
Views: 48996
[CHAPTER]
TIMEBASE=1/1000
START=0
END=100000
title=Bread & Fred
[CHAPTER]
TIMEBASE=1/1000
START=100000
END=1089000
title=Just Chatting

you have 2 way to do it , leave metadata.txt like if no trim were made and adjust the ffmpeg command, or adjust metadata.txt and pass it directly to ffmpeg.

If you provide a chapter to ffmpeg after the audio and video end, and the chapter has no duration, it will add a single frame to add it, this is why this happens:

[CHAPTER]
TIMEBASE=1/1000
START=-8025000
END=16955000
title=Just Chatting
    Chapter #0:2: start 1089.000000, end 1089.000333
      Metadata:
        title           : Palworld

You are forcing ffmpeg to add a new frame of 0..000333 seconds because you don't provide accurate chapters.

ScrubN commented 6 months ago

You are forcing ffmpeg to add a new frame of 0..000333 seconds because you don't provide accurate chapters.

This is blatantly untrue and can be proven by cropping 1 second earlier or by modifying the metadata file.

FFmpeg applies chapters on hh:mm:ss but trims on hh:mm:ss+1. The only solution is to trim on frames instead of seconds, but this is not possible because of potentially dropped frames. This problem is probably why Untwitch only supports downloading whole segments.

superbonaci commented 6 months ago

Because if you trim 1 second earlier the last chapter is not there in the first place. Ffmpeg applies all chapters even after audio and video have ended.

ScrubN commented 6 months ago

Because if you trim 1 second earlier the last chapter is not there in the first place.

And would the chapter not be there because FFmpeg discards out of bounds chapters?

superbonaci commented 6 months ago

And would the chapter not be there because FFmpeg discards out of bounds chapters?

No it doesn't. Create the metadata.txt:

;FFMETADATA1
title=🐳 Finishing Bread & Fred with @limealicious | !newvid !supps (2065608792)
artist=bao
date=2024
comment=Originally aired: 2024-02-17 18:05:48Z\
Video id: 2065608792\
Views: 48996
[CHAPTER]
TIMEBASE=1/1000
START=0
END=2333000
title=Just Chatting
[CHAPTER]
TIMEBASE=1/1000
START=2333000
END=8125000
title=Bread & Fred
[CHAPTER]
TIMEBASE=1/1000
START=8125000
END=9114000
title=Just Chatting
[CHAPTER]
TIMEBASE=1/1000
START=9114000
END=24980000
title=Palworld

Create a 5 second video with the chapters:

ffmpeg -f lavfi -i color=c=blue:s=720x480:d=5,drawbox=x=100:y=60:w=120:h=90:color=red:t=fill -i metadata.txt -map_metadata 1 -c:v libx264 -pix_fmt yuv420p -y intro.mp4

The chapters are independent of content duration:

$ ffprobe -hide_banner -i intro.mp4 
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'intro.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    title           : 🐳 Finishing Bread & Fred with @limealicious | !newvid !supps (2065608792)
    artist          : bao
    date            : 2024
    encoder         : Lavf60.16.100
    comment         : Originally aired: 2024-02-17 18:05:48Z
                    : Video id: 2065608792
                    : Views: 48996
  Duration: 00:00:05.00, start: 0.000000, bitrate: 11 kb/s
  Chapters:
    Chapter #0:0: start 0.000000, end 2333.000000
      Metadata:
        title           : Just Chatting
    Chapter #0:1: start 2333.000000, end 8125.000000
      Metadata:
        title           : Bread & Fred
    Chapter #0:2: start 8125.000000, end 9114.000000
      Metadata:
        title           : Just Chatting
    Chapter #0:3: start 9114.000000, end 24980.000000
      Metadata:
        title           : Palworld
  Stream #0:0[0x1](und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(progressive), 720x480 [SAR 1:1 DAR 3:2], 6 kb/s, 25 fps, 25 tbr, 12800 tbn (default)
    Metadata:
      handler_name    : VideoHandler
      vendor_id       : [0][0][0][0]
      encoder         : Lavc60.31.102 libx264
  Stream #0:1[0x2](eng): Data: bin_data (text / 0x74786574)
    Metadata:
      handler_name    : SubtitleHandler
Unsupported codec with id 98314 for input stream 1

You have to modify the source code of TwitchDownloaderCLI to be fine tuned to work with ffmpeg and cover all bases. It's much more difficult that in looks like.

ScrubN commented 6 months ago
  1. I download 2099228826 with TD, which has 11 chapters

    ./TwitchDownloaderCLI.exe -u 2099228826 -o out.mp4 -e 3350 -q 360
  2. I check the metadata file during finalization to confirm it has all 11 chapters

Chapters

``` ;FFMETADATA1 title=Sh*t Show Saturday, random games 2day | !starforge (2099228826) artist=sodapoppin date=2024 comment=Originally aired: 2024-03-23 17:19:08Z\ Video id: 2099228826\ Views: 312122 [CHAPTER] TIMEBASE=1/1000 START=0 END=1977000 title=Just Chatting [CHAPTER] TIMEBASE=1/1000 START=1977000 END=6617000 title=An Ankou [CHAPTER] TIMEBASE=1/1000 START=6617000 END=8018000 title=HITMAN World of Assassination [CHAPTER] TIMEBASE=1/1000 START=8018000 END=9474000 title=Age of Water: The First Voyage [CHAPTER] TIMEBASE=1/1000 START=9474000 END=13658000 title=Midnight Ghost Hunt [CHAPTER] TIMEBASE=1/1000 START=13658000 END=19515000 title=Slice & Dice [CHAPTER] TIMEBASE=1/1000 START=19515000 END=19645000 title=Just Chatting [CHAPTER] TIMEBASE=1/1000 START=19645000 END=21091000 title=Pesticide Not Required [CHAPTER] TIMEBASE=1/1000 START=21091000 END=22836000 title=SCP: Descent [CHAPTER] TIMEBASE=1/1000 START=22836000 END=22850000 title=Just Chatting [CHAPTER] TIMEBASE=1/1000 START=22850000 END=32150000 title=Rumble Club ```

  1. I run FFprobe on 2099228826

    ffprobe -hide_banner -i out.mp4
    Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'out.mp4':
    Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    title           : Sh*t Show Saturday, random games 2day   | !starforge (2099228826)
    artist          : sodapoppin
    date            : 2024
    encoder         : Lavf58.51.101
    comment         : Originally aired: 2024-03-23 17:19:08Z
                    : Video id: 2099228826
                    : Views: 312122
    Duration: 00:55:50.02, start: 0.000000, bitrate: 673 kb/s
    Chapters:
    Chapter #0:0: start 0.000000, end 1977.000000
      Metadata:
        title           : Just Chatting
    Chapter #0:1: start 1977.000000, end 3350.000000
      Metadata:
        title           : An Ankou
    Stream #0:0[0x1](und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, bt709, progressive), 640x360 [SAR 1:1 DAR 16:9], 564 kb/s, 30 fps, 30 tbr, 90k tbn (default)
    Metadata:
      handler_name    : VideoHandler
      vendor_id       : [0][0][0][0]
    Stream #0:1[0x2](und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 101 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
      vendor_id       : [0][0][0][0]
    Stream #0:2[0x3](eng): Data: bin_data (text / 0x74786574)
    Metadata:
      handler_name    : SubtitleHandler
    Unsupported codec with id 98314 for input stream 2
  2. I observe that of the 11 chapters, the 9 chapters that occur after the video ends are not present.

It seems to me that FFmpeg does remove out of bounds chapters, and thus this is not an issue.

superbonaci commented 6 months ago

Yes, it works now thanks.