Kethsar / ytarchive

Garbage Youtube livestream downloader
MIT License
1.13k stars 91 forks source link

FFMPEG fails while trying to mux a large audio with video -- With workaround! #116

Open mikepiman opened 1 year ago

mikepiman commented 1 year ago

FFMPEG fails while trying to mux an audio file (AAC format at least) of around 14 GB or more, with a video file that may be larger. The error is: I observe FFMPEG allocating more and more RAM, and finally always failing as it approaches 4 GB, reporting "Cannot allocate enough memory" right before it exits.

After much tinkering, I found a way around this, that produced perfect, seamless results from a 160 GB, 933 hour livestream:

Basically, I found that MKVMERGE will mux them.

However, it cannot take them as-is (the video coming out with an incorrect resolution, and possibly other problems), so first FFMPEG must correct them.

However, since FFMPEG cannot take files that cause it to allocate more than 4 GB memory, the files must be split. I used a file splitter to split my video file, which I renamed "v.h264", into chunks of no more than about 30 GB, and did the same for the audio file, "a.aac".

To make sure the pieces can be recombined seamlessly, I opened all the video pieces at once in a hex editor(*). I observed that the first piece starts with a sequence, "00 00 00 1C" followed by "ftypdash", that is repeated at very regular but slightly different intervals throughout the whole file -- and throughout every piece. Therefore, I cut-and-pasted such that every file piece starts with this sequence. Then I did the same for the audio file.

As mentioned, now FFMPEG will accept them and "fix" them into playable audio & video files, by running:

ffmpeg -hidebanner -nostdin -loglevel fatal -stats -i a1.aac -c copy a1.aac ffmpeg -hidebanner -nostdin -loglevel fatal -stats -i a2.aac -c copy a2.aac ffmpeg -hidebanner -nostdin -loglevel fatal -stats -i a3.aac -c copy a3.aac ... ffmpeg -hidebanner -nostdin -loglevel fatal -stats -i v1.h264 -c copy v1.h264 ffmpeg -hidebanner -nostdin -loglevel fatal -stats -i v2.h264 -c copy v2.h264 ffmpeg -hidebanner -nostdin -loglevel fatal -stats -i v3.h264 -c copy v3.h264 ...

Then I used "MKVToolNix GUI" to combine the video, and then combine the audio, and then mux the two. (This program simply ran MKVMERGE.EXE each time).

VoilΓ ! My 160 GB MKV video file plays and seeks quickly and perfectly, with no seams or skips. I hope this helps someone -- esp the developer.


(*) HxD as a hex editor was intuitive and extremely fast and accurate.
t94xr commented 7 months ago

I would love if we could use ytarchive --mkvmerge or something to swap from ffmpeg to mkvmerge ...

Im experiencing this same problem with ffmpeg but I can't seem to fix it.

t94xr@thinkserver:~/copper$ ./monitor
ytarchive 0.4.0
2024/02/16 13:12:43 Channel: CopperCab
2024/02/16 13:12:43 Video Title: MORE KIDS = CLIMATE CHANGE? 😲🌎πŸ”₯
2024/02/16 13:12:43 DEBUG: Retrieving URLs from web DASH manifest
2024/02/16 13:12:44 DEBUG: Retrieving URLs from web adaptive formats
2024/02/16 13:12:44 Selected quality: 720p60 (h264)
2024/02/16 13:12:44 INFO: Starting download to /home/t94xr/copper/P9YCjDXJbVk__4129179490/MORE KIDS = CLIMATE CHANGE_ 😲🌎πŸ”₯-P9YCjDXJbVk.f140.ts
2024/02/16 13:12:44 INFO: Starting download to /home/t94xr/copper/P9YCjDXJbVk__4129179490/MORE KIDS = CLIMATE CHANGE_ 😲🌎πŸ”₯-P9YCjDXJbVk.f298.ts
2024/02/16 14:12:48 DEBUG: Retrieving URLs from web DASH manifest
2024/02/16 14:12:49 DEBUG: Retrieving URLs from web adaptive formats
2024/02/16 15:12:53 DEBUG: Retrieving URLs from web DASH manifest
2024/02/16 15:12:54 DEBUG: Retrieving URLs from web adaptive formats
2024/02/16 15:21:16 DEBUG: audio1: HTTP Error for fragment 1652: 404 Not Found
2024/02/16 15:21:17 DEBUG: video1: HTTP Error for fragment 1652: 404 Not Found
2024/02/16 15:21:27 DEBUG: video1: HTTP Error for fragment 1652: 404 Not Found
2024/02/16 15:21:38 DEBUG: video1: HTTP Error for fragment 1652: 404 Not Found
2024/02/16 15:21:49 DEBUG: video1: HTTP Error for fragment 1652: 404 Not Found
2024/02/16 15:22:00 DEBUG: video1: HTTP Error for fragment 1652: 404 Not Found
2024/02/16 15:22:09 DEBUG: audio1: Fragment 1652: 10/10 retries
2024/02/16 15:22:11 DEBUG: video1: HTTP Error for fragment 1652: 404 Not Found
2024/02/16 15:22:11 DEBUG: Retrieving URLs from web DASH manifest
2024/02/16 15:22:13 DEBUG: Retrieving URLs from web adaptive formats
2024/02/16 15:22:13 DEBUG: audio1: Fragment 1652: Stream still live, continuing download attempt
2024/02/16 15:22:19 DEBUG: video1: HTTP Error for fragment 1652: 404 Not Found
2024/02/16 15:22:25 DEBUG: video1: HTTP Error for fragment 1652: 404 Not Found
2024/02/16 15:22:31 DEBUG: video1: HTTP Error for fragment 1652: 404 Not Found
2024/02/16 15:22:32 DEBUG: audio1: HTTP Error for fragment 1652: 404 Not Found
2024/02/16 15:22:37 DEBUG: video1: HTTP Error for fragment 1652: 404 Not Found
2024/02/16 15:22:37 DEBUG: video1: Fragment 1652: 10/10 retries
2024/02/16 15:22:38 DEBUG: audio1: HTTP Error for fragment 1652: 404 Not Found
2024/02/16 15:22:40 DEBUG: Retrieving URLs from web DASH manifest
2024/02/16 15:22:40 DEBUG: Retrieving URLs from web adaptive formats
2024/02/16 15:22:40 DEBUG: audio1: Stream has ended and fragment within the last two not found, probably not actually created
2024/02/16 15:22:40 DEBUG: audio1: exiting
2024/02/16 15:22:40 DEBUG: video1: exiting
2024/02/16 15:22:40 DEBUG: video-download thread closing
2024/02/16 15:22:40 DEBUG: audio-download thread closing
2024/02/16 15:22:40 INFO: Deleting file /home/t94xr/copper/P9YCjDXJbVk.f140.state
2024/02/16 15:22:40 INFO: Deleting file /home/t94xr/copper/P9YCjDXJbVk.f298.state

2024/02/16 15:22:40 Download Finished
2024/02/16 15:22:40 INFO: Moving file /home/t94xr/copper/P9YCjDXJbVk__4129179490/MORE KIDS = CLIMATE CHANGE_ 😲🌎πŸ”₯-P9YCjDXJbVk.f140.ts to /home/t94xr/copper/MORE KIDS = CLIMATE CHANGE_ 😲🌎πŸ”₯-P9YCjDXJbVk.f140.ts
2024/02/16 15:22:40 INFO: Moving file /home/t94xr/copper/P9YCjDXJbVk__4129179490/MORE KIDS = CLIMATE CHANGE_ 😲🌎πŸ”₯-P9YCjDXJbVk.f298.ts to /home/t94xr/copper/MORE KIDS = CLIMATE CHANGE_ 😲🌎πŸ”₯-P9YCjDXJbVk.f298.ts
2024/02/16 15:22:40 INFO: Moving file /home/t94xr/copper/P9YCjDXJbVk__4129179490/MORE KIDS = CLIMATE CHANGE_ 😲🌎πŸ”₯-P9YCjDXJbVk.ffmpeg.txt to /home/t94xr/copper/MORE KIDS = CLIMATE CHANGE_ 😲🌎πŸ”₯-P9YCjDXJbVk.ffmpeg.txt
2024/02/16 15:22:40 Muxing final file...
2024/02/16 15:22:40 DEBUG: Executing command: /usr/bin/ffmpeg /usr/bin/ffmpeg -hide_banner -nostdin -loglevel fatal -stats -i '/home/t94xr/copper/MORE KIDS = CLIMATE CHANGE_ 😲🌎πŸ”₯-P9YCjDXJbVk.f140.ts' -i '/home/t94xr/copper/MORE KIDS = CLIMATE CHANGE_ 😲🌎πŸ”₯-P9YCjDXJbVk.f298.ts' -c copy '/home/t94xr/copper/MORE KIDS = CLIMATE CHANGE_ 😲🌎πŸ”₯-P9YCjDXJbVk.mkv'
2024/02/16 15:22:40 ERROR: Execute returned code 1. Something must have gone wrong with ffmpeg.
2024/02/16 15:22:40 ERROR: The .ts files will not be deleted in case the final file is broken.
2024/02/16 15:22:40 ERROR: Finally, the ffmpeg command was either written to a file or output above.
2024/02/16 15:22:42 You have opted to wait for a livestream to be scheduled. Retrying every 30 seconds.
Retries: 2 (Last retry: 2024/02/16 15:23:43), Total time waited: 60 seconds^C
t94xr@thinkserver:~/copper$ ls
Kethsar commented 7 months ago

@t94xr 1652 fragments is fairly small even for a normal latency stream. Are you sure your case isn't more likely to do with the specific ffmpeg version being used? There were issues with 6.0 and 6.0.1 if I am not mistaken.

You could try running the mux command manually with the -loglevel fatal part removed to see what error it was actually giving.

t94xr commented 7 months ago
ffmpeg version 5.1.4-0+deb12u1 Copyright (c) 2000-2023 the FFmpeg developers
  built with gcc 12 (Debian 12.2.0-14)

hmm I'll update, im on Deb 12

Kethsar commented 7 months ago

It would probably be better to run the command and see what the error actually is first, as updating has caused more breakage than not. I don't think 5.1.4 had issues, but I cannot remember since I don't think I ended up on that version. I went from 4.X to 6.0.1 or something.

t94xr commented 7 months ago
ffmpeg version 6.0.1 Copyright (c) 2000-2023 the FFmpeg developers
  built with gcc 12 (Debian 12.2.0-14)

just updated to this... added the fmpeg repos, which seems to be the latest available to me.

Kethsar commented 7 months ago

Well I am pretty sure 6.0.1 is a known broken version, it has issues with the audio streams when muxing if I recall.

t94xr commented 7 months ago

well if i force fmpeg 6.1.1 deb onto my system, it borks ffmeg outright, so I'll go fingers crossed, I have a second method as a backup, now with mkvmerge I can do it manually but it would be great if it.

t94xr commented 7 months ago

I've tested everything on Windows using ffmpeg 6.1.1 and it works flawlessly. fml