Darkness4 / fc2-live-dl-go

Automatically download FC2 livestream. Fast and light.
MIT License
9 stars 0 forks source link

concat: Concatenating with a mp4 and ts into a mpeg/matroska container results in invalid data. #39

Closed Darkness4 closed 3 months ago

Darkness4 commented 3 months ago

Error happens on stream 1 (audio)

Logs:

4:07AM INF github.com/Darkness4/fc2-live-dl-go/video/concat/concat.go:88 > concat inputs=["Darkness4 Test/2024-06-09 Test.ts","Darkness4 Test/2024-06-09 Test.1.mp4"] options={} output="Darkness4 Test/2024-06-09 Test.combined.m4a"
Blacklisted stream #0 (video)
Input 0, mapping stream 1 (audio) to output stream 0
Created output stream (audio)
Blacklisted stream #0 (video)
Input 1, mapping stream 1 (audio) to output stream 0
input#1, stream #0 concatenation, last.dts=2351104, pkt.dts=0, new offset=2352128
[aac_adtstoasc @ 0x7f313c0e8200] Error parsing ADTS frame header!
[ipod @ 0x7f313c000bc0] Error applying bitstream filters to an output packet for stream #0: Invalid data found when processing input
Error writing packet to output file: Invalid data found when processing input
Error occurred: Invalid data found when processing input
4:07AM ERR github.com/Darkness4/fc2-live-dl-go/fc2/fc2.go:285 > ffmpeg concat finished with error error="Invalid data found when processing input\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" channelID=[REDACTED]

Test cases:

A concat B B.ts B.mp4 B.m4a
A.ts OK FAIL FAIL
A.mp4 OK OK OK
A.m4a OK OK OK

Probe:

Input #0, mpegts, from 'input.ts':
  Duration: 00:00:49.05, start: 1.913333, bitrate: 2076 kb/s
  Program 1 
    Metadata:
      service_name    : Service01
      service_provider: FFmpeg
  Stream #0:0[0x100]: Video: h264 (Main) ([27][0][0][0] / 0x001B), yuv420p(progressive), 852x480 [SAR 640:639 DAR 16:9], 30 fps, 30 tbr, 90k tbn
  Stream #0:1[0x101]: Audio: aac (LC) ([15][0][0][0] / 0x000F), 48000 Hz, stereo, fltp, 95 kb/s

Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'input.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf60.16.100
  Duration: 00:03:02.07, start: 0.000000, bitrate: 498 kb/s
  Stream #0:0[0x1](und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(progressive), 852x480 [SAR 640:639 DAR 16:9], 388 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]

Same behavior with ffmpeg

ffmpeg -f concat -i list.txt -c:v copy -c:a copy output.mp4
Darkness4 commented 3 months ago

Might be fixed with aac transcoding. This is working:

ffmpeg -y -f concat -i list.txt -c:v copy -c:a aac output.mp4

Doc: https://ffmpeg.org/doxygen/6.1/transcode_aac_8c-example.html

Or, it's something related with the in_codecpar.

This is also working:

 ffmpeg -y -f concat -i list.txt -c:v copy -c:a copy output.aac

Looks like the issue comes from the container. The matroska/mpeg forces the audio stream adts (Audio Data Transport Stream, equivalent to mpegts) to be converted into asc:

aac_adtstoasc Convert MPEG-2/4 AAC ADTS to an MPEG-4 Audio Specific Configuration

Darkness4 commented 3 months ago

Recommended fix is to use intermediates/fifo:

mkfifo temp1.ts temp2.ts
echo temp1.ts > list.txt
echo temp2.ts >> list.txt
ffmpeg -y -i input.mp4 -c copy temp1.ts 2> /dev/null &
ffmpeg -y -i input.ts -c copy temp2.ts 2> /dev/null &
ffmpeg -f concat -i list.txt -c copy output.mp4
Darkness4 commented 3 months ago

Closed with commit abc8639