This is a project I did a while a go, but I never got a round to write a post about it ๐Ÿ˜ฉ So here it is. It was a โ˜”๏ธ wet and cold spring in Norway (like most of them are). We were going on a ๐Ÿš™ trip for the weekend, and I wanted to bring some ๐Ÿ“บ TV shows my son could watch on the way.

Summer marked trails in Norway

Summer marked trails in Norway by Hans Kristian Flaatten. Licensed under CC BY-SA 4.0.

Here in Norway we have a public broadcasting company - the Norwegian Broadcasting Corporation (NRK). I have had the opportunity to work with them on UT.no - Norwayโ€™s Trip Planner - but more on that later. NRK makes available all of their shows through their online media player tv.nrk.no for a period of 6 to 12 months before they are taken offline.

All this is well and good, except for the disability to save shows for offline viewing. Since we have so spotty cellular connection because of all the high mountains and deep fjords, and streaming 200 episodes of Sesame Street is not an economically viable option anyways, I opted for making my own offline copies. ffmpeg ๐ŸŽฅ to the rescue!

m3u-to-mp4

Getting the m3u-steam was just a matter of right clicking on it in Safari and saving to disk. Once I had the m3u playlist file it was time to fire up ffmpeg, and by the way, thereโ€™s a Docker Image for that ๐Ÿ˜…

m3u is not the actual video stream itself. It is just a playlist composed of links to several small videos that are downloaded and played in rapid succession. ffmpeg has built in support for downloading and concatenate into one video file.

ffmpeg -i playlist.m3u -c copy -bsf:a aac_adtstoasc recording.mp4

Here is a rundown of what the different cli flags do:

  • -i playlist.m3u is location of the playlist file we want to record.
  • -c copy do not re-encoding the video stream since it is already the desired format of H.264.
  • -bsf:a aac_adtstoasc converts the audio such that it is compatible with the mp4 container.
  • recording.mp4 is the location of the outputted recording.

๐Ÿณ Docker All Things

Since I do not install program directly on my computer any more I of course had to run this too in a Docker container. You can check out the project on github.com/Starefossen/m3u-to-mp4 ๐Ÿš€

docker run --rm
  -v $(pwd)/playlists:/playlists
  -v $(pwd)/recordings:/recordings
  nachochip/ffmpeg
    -i /playlists/episode1.m3u
    -c copy
    -bsf:a aac_adtstoasc
    /recordings/episode1.mp4

Comments

If you have any questions or other feedback to the article, please post them in the comment section bellow and I promise to read them and respond to you on a regular basis.