DEV Community

Saifur Rahman Mohsin
Saifur Rahman Mohsin

Posted on

2

Fixing a broken youtube live video download

I usually download videos using Youtube-dl. In fact, it's one of my default programs that's install on any new machine.

So earlier today, I was downloading a "live" youtube video which was to be live the whole day. I needed a specific part i.e. a single event so I ran youtube-dl on it's live URL at the beginning of the event, ran caffeinate -dismut 65535, set my mac brightness to 0 and left for my daily routine.

I came back after an hour when the event ended, and hit Ctrl+C hoping to get the final video but then I got this in my log:

^C[ffmpeg] Interrupted by user
[ffmpeg] Downloaded 370933808 bytes
[download] 100% of 353.75MiB in 01:02:40
Mohsin@Mozs-MacBook-Pro Desktop % [https @ 0x12800b800] Opening 'http://SomeLongURL/playlist/index.m3u8' for reading
Error writing trailer of file:SomeEvent.mp4: Immediate exit requested
frame=112801 fps= 21 q=-1.0 Lsize=  362240kB time=01:02:40.08 bitrate= 789.2kbits/s speed=0.717x
video:303393kB audio:59858kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown
Error closing file file:SomeEvent.mp4: Immediate exit requested
Exiting normally, received signal 2.
Enter fullscreen mode Exit fullscreen mode

Now this Error writing trailer of file:SomeEvent.mp4 was not ideal. This meant that the mp4 file that the mp4 container of the video file that was saved did not have a proper terminating trailer. Videos have a header/trailer and it's specification on how that is, depends on it's format. And this meant that when I opened the video on VLC, it threw an error than playing. I needed to fix this in order to watch the video.

And that's when I found untrunc but that's originally written to work on Linux machines. I run MacOS and that too ARM (M1), so I would have to compile it from source. Fortunately, unlike the initial days since it's release, Docker works great on Apple Silicon machines and I can install this tool inside and use it.

The tool needs a working "reference" to fix the video so I ran youtube-dl again on the live video and took a small clip. This time, it terminated correctly and so I put them both in a videos folder with SomeEvent.mp4 and reference.mp4.

Then I cloned the untrunc repo, and ran the docker build using:

git clone git@github.com:ponchio/untrunc.git
docker build -t untrunc .
Enter fullscreen mode Exit fullscreen mode

And once the docker build was done, I ran the final command to fix the video:

docker run -v ~/Desktop/videos:/files untrunc /files/reference.mp4 /files/SomeEvent.mp4
Enter fullscreen mode Exit fullscreen mode

It gave an output:

Reading: /files/reference.mp4
[mov,mp4,m4a,3gp,3g2,mj2 @ 0xaaaacabcda80] multiple edit list entries, a/v desync might occur, patch welcome
Repair: /files/SomeEvent.mp4
Mdat not found!
Processed: 1%
...
...
Processed: 99%
Found 274677 packets.
Found 112779 chunks for avc1
Found 161898 chunks for mp4a
Saving to: /files/SomeEvent_fixed.mp4
Enter fullscreen mode Exit fullscreen mode

And that was how I was able to recover the broken mp4 file. Hope this article helps anyone facing the same issue. Feel free to drop a comment if it does.

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay