Opened 7 months ago

Closed 7 months ago

Last modified 7 months ago

#10629 closed defect (invalid)

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

Reported by: superbonaci Owned by:
Priority: minor Component: undetermined
Version: 6.0 Keywords:
Cc: Blocked By:
Blocking: Reproduced by developer: no
Analyzed by developer: no

Description

Summary of the bug:
How to reproduce:

% ffmpeg -h
ffmpeg version 6.0 Copyright (c) 2000-2023 the FFmpeg developers
  built with Apple clang version 14.0.3 (clang-1403.0.22.14.1)
  configuration: --prefix=/opt/homebrew/Cellar/ffmpeg/6.0_1 --enable-shared --enable-pthreads --enable-version3 --cc=clang --host-cflags= --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libaribb24 --enable-libbluray --enable-libdav1d --enable-libmp3lame --enable-libopus --enable-librav1e --enable-librist --enable-librubberband --enable-libsnappy --enable-libsrt --enable-libsvtav1 --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvmaf --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libspeex --enable-libsoxr --enable-libzmq --enable-libzimg --disable-libjack --disable-indev=jack --enable-videotoolbox --enable-audiotoolbox --enable-neon
  libavutil      58.  2.100 / 58.  2.100
  libavcodec     60.  3.100 / 60.  3.100
  libavformat    60.  3.100 / 60.  3.100
  libavdevice    60.  1.100 / 60.  1.100
  libavfilter     9.  3.100 /  9.  3.100
  libswscale      7.  1.100 /  7.  1.100
  libswresample   4. 10.100 /  4. 10.100
  libpostproc    57.  1.100 / 57.  1.100
Hyper fast Audio and Video encoder
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...

How to reproduce:
Use the concat demuxer or protocol to combine/merge/concatenate .ts files which are part of whole stream.

The stream can be any https://www.twitch.tv/ stream.
The stream is made of several .ts parts which can be combined:
index-0000000000.ts
...
index-0000000945.ts

into output.ts

Parts can be downloaded with:

% twitch-dl download https://www.twitch.tv/videos/1462570126 -k -q source --no-join

To get the properly formatted combined file, use Video Download Helper and set it to download .ts files, so can save them as .m2ts

ffmpeg does not concatenate the parts like DownloadHelper or TwitchDownloader do (these produce the same exact file, same sha1). Apparently the audio and video streams merged by ffmpeg are in good condition but several metadata is destroyed. The other 2 programs keep everything as is.

The method that concatenates properly all the parts is available here (.NET Framework):
https://github.com/lay295/TwitchDownloader/blob/fefd3d18f84185977b9959032dd423b7e1fed405/TwitchDownloaderCore/VideoDownloader.cs#L663-L698

Change History (8)

comment:1 by superbonaci, 7 months ago

All the ts parts, scripts, and correct and incorrect output.ts are inside this zip file:

https://transfer.sh/get/iWxnbTEisj/ffmpeg_incorrect_ts_merge.zip

mirror:
https://mega.nz/file/VfhRAQaD#_jCjCsK5J2O7EK8XyNy5kr0bW3X7AJd6o-Qlj8X-ZE4

sha1:

d2b26c5816ba18a93b8b563005cee00ced16574a  ffmpeg_incorrect_ts_merge.zip

comment:2 by mkver, 7 months ago

If these files are part of the same stream, then why do you use the concat demuxer and not the concat protocol?

comment:3 by superbonaci, 7 months ago

Yes, of course all the parts are part of the same stream and are all of them and in consecutive order. I've tried both demuxer and protocol just to test, but none of them handle the metadata properly.

comment:4 by superbonaci, 7 months ago

The file muxed by ffmpeg using the concat demuxer should match in hash (sha1) the one downloaded by "Video Download Helper" or "TwitchDownloaderCLI", because I think there's only one way to properly mux these parts into a single file so it's not noticeable any difference (correct me if I am wrong please).

in reply to:  4 comment:5 by mkver, 7 months ago

Resolution: invalid
Status: newclosed

Replying to superbonaci:

The file muxed by ffmpeg using the concat demuxer should match in hash (sha1) the one downloaded by "Video Download Helper" or "TwitchDownloaderCLI", because I think there's only one way to properly mux these parts into a single file so it's not noticeable any difference (correct me if I am wrong please).

That is absolutely wrong: If you simply want to combine all these transport streams, use cat; instead of simply combining these files, FFmpeg demuxes the input files and remuxes them. (You could use the concat protocol together with the data demuxer ("-f data" as input option) and the data muxer to do what concat does.)

comment:6 by superbonaci, 7 months ago

Cat can't be used to concatenate Transport Stream files because must not be binary concatenated, streams will break.
This is wrong:

% cat index-*.ts > output_bad.ts

The command you specified does not produce any output:

% ffmpeg -i "concat:tsfiles/index-0000000000.ts|tsfiles/index-0000000001.ts|tsfiles/index-0000000002.ts|tsfiles/index-0000000003.ts|tsfiles/index-0000000004.ts" -f data -c copy tsfiles/output_concat_protocol.ts
ffmpeg version 6.0 Copyright (c) 2000-2023 the FFmpeg developers
  built with Apple clang version 14.0.3 (clang-1403.0.22.14.1)
  configuration: --prefix=/opt/homebrew/Cellar/ffmpeg/6.0_1 --enable-shared --enable-pthreads --enable-version3 --cc=clang --host-cflags= --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libaribb24 --enable-libbluray --enable-libdav1d --enable-libmp3lame --enable-libopus --enable-librav1e --enable-librist --enable-librubberband --enable-libsnappy --enable-libsrt --enable-libsvtav1 --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvmaf --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libspeex --enable-libsoxr --enable-libzmq --enable-libzimg --disable-libjack --disable-indev=jack --enable-videotoolbox --enable-audiotoolbox --enable-neon
  libavutil      58.  2.100 / 58.  2.100
  libavcodec     60.  3.100 / 60.  3.100
  libavformat    60.  3.100 / 60.  3.100
  libavdevice    60.  1.100 / 60.  1.100
  libavfilter     9.  3.100 /  9.  3.100
  libswscale      7.  1.100 /  7.  1.100
  libswresample   4. 10.100 /  4. 10.100
  libpostproc    57.  1.100 / 57.  1.100
Input #0, mpegts, from 'concat:tsfiles/index-0000000000.ts|tsfiles/index-0000000001.ts|tsfiles/index-0000000002.ts|tsfiles/index-0000000003.ts|tsfiles/index-0000000004.ts':
  Duration: 00:00:18.43, start: 60.000000, bitrate: 2694 kb/s
  Program 1
  Stream #0:0[0x100]: Audio: aac (LC) ([15][0][0][0] / 0x000F), 48000 Hz, stereo, fltp, 131 kb/s
  Stream #0:1[0x101]: Video: h264 (Main) ([27][0][0][0] / 0x001B), yuv420p(progressive), 1280x720, 30 fps, 30 tbr, 90k tbn
  Stream #0:2[0x102]: Data: timed_id3 (ID3  / 0x20334449)
Output #0, data, to 'tsfiles/output_concat_protocol.ts':
[out#0/data @ 0x6000015503c0] Output file does not contain any stream

comment:7 by superbonaci, 7 months ago

For those wanting to properly concatenate ts files, use the tsmerge mode in TwitchDownloaderCLI. It uses native dotnet functions to achieve it, it's the best so far.

comment:8 by superbonaci, 7 months ago

Running TwitchDownloaderCLI tsmerge saves the day.

Note: See TracTickets for help on using tickets.