John's Corner
Download MPEG-DASH video
This tip covers how to download an MPEG-DASH video stream and save it to a standalone MP4 or other video file.
MPEG-DASH (Dynamic Adaptive Streaming over HTTP) streams are a relatively new way to supply video to users. In simple terms, the video and audio is divided up into short segments (eg. 10 secs) and streamed to the web browser as required. The video and audio can be streamed as a single combined segment or as two separate segments.
This means the user only gets sent data when the video is actually playing which also acts to reduce load on servers. Best of all, it ensures video and audio remain in sync. There is usually provision to increase or decrease the quality on the fly, depending on the speed of the connection to give the user the best quality while minimising dropouts. Downloading to an offline file allows the best quality to be saved and then played back locally without dropouts. Information about the available resolutions and segments is contained in an initial master file, typically in JSON, XML, MPD or M3U8 format.
The video is not downloadable as a single file, so the video and audio data segments must be individually downloaded and then assembled into a standalone file. The process described below uses the Windows Command Prompt console and requires Youtube DL to download the segments and FFmpeg to create the standalone file. You will need to play the video in your browser and use the DevTools or Debug console in your browser to get information about the video and audio streams. Sometimes the browser will not play the video when the debug console is active. In this case, you will need to use a network traffic monitoring extension. The Traffic Analyser extension for Google Chrome is recommended.
The instructions below should work for a wide variety of files and websites but read carefully. Some of the commands will need modification to suit the particular situation.
- Initial Setup
This step sets up the folders and tools you will need.
- Start by creating a download folder in a convenient location.
- Copy youtube-dl.exe and ffmpeg.exe to this folder. You should also install a network traffic monitoring extension in your browser, eg. Traffic Analyser.
- Create a subfolder called video. This will be the working folder for your video segments. You may also need to create an audio subfolder in the case where there are separate video and audio streams.
- Get download URLs
This step gets information about the video and audio streams and where to download them from.
- Open web page in browser and use F12 to open the DevTools window. Filter by XHR. If the playback stops, use the network traffic monitoring extension.
- Start the video, let it play for a few seconds then stop.
- Check in DevTools for a master file followed by audio and video segment files
The master file might be called master or index or playlist and will have either .json or .mpd or .xml or .m3u8 as the extension. The video and audio segments might be called segment-1 or video-1 or audio-1 or index-1 with .m4s or .mp4 or .m4a or .ts as the extension. Getting the right files might take some trial and error. You can use the search function in the Traffic Analyser to search for each extension in turn.
- Right click on the master file and click "Open in New Tab".
A .json or .xml should display in the web page. A .mpd or .m3u8 will download. This can be opened in Notepad.
- Look in the master file for the desired resolution (typically 1920 x 1080) and if present, get the baseURL eg. 96dec4e9/chop
- Check the segment lists and note the highest numbered segment (eg. 288 in commands below)
- Right click the audio segment file (if present) to get the audio URL and paste to a temporary text file in Notepad.
- Edit the baseURL if required to match the desired resolution.
- Repeat for the video segment.
- Use these URLs in the commands below to download the segments into the subfolder(s).
Note: These URLs may be time limited so best to download immediately.
- Prepare the download commands
This step sets up the commands needed to download all the video and audio data segments.
- Copy and paste for /L %A in (0,1,nnn) do (youtube-dl -o video\%(title^)s "") into your Notepad temporary text file.
- Cut and paste your video URL between the double quotes.
- Change nnn to the highest segment number from the master file and make sure you have the correct baseURL for the resolution you want.
- Finally, change the number in the segment file name from 1 to %A. This might be required in more than one place in the filename.
- If present, repeat the above steps for the audio file using for /L %A in (0,1,nnn) do (youtube-dl -o audio\%(title^)s "").
An example of a completed video command is shown below:
for /L %A in (0,1,288) do (youtube-dl -o video\%(title^)s "https://200vod-adaptive.akamaized.net/exp=1585613778~acl=%2F01cf90c8-e4ad-4e58-b547-2dd8a8728aa7%2F%2A~hmac=fffda9518507e194075586e847af3bdea325d1dacdac11d3fbb0bcbabd28a333/01cf90c8-e4ad-4e58-b547-2dd8a8728aa7/sep/video/96dec4e9/chop/segment-%A.m4s")
These video and audio commands will be used to download all the associated segments in a single step. Note that the -o video\%(title^)s following youtube-dl instructs Youtube downloader to store the downloaded file in the video subfolder - similarly for the audio subfolder. The ^ character is an "escape" character which essentially tells the windows command interpreter to ignore the following ) character and pass it on to the youtube-dl command. Without it, the overall command will fail.
- Download video and audio segments
This step downloads all the segments for each stream into their respective subfolders.
- Open a command prompt in your download folder created above - hold down the Shift key and right click the folder icon then select Open command window here from the menu.
- Cut and paste your video download command above and press Enter to execute. This may take many minutes to complete.
- If required, cut and paste your audio download command above and press Enter to execute. This will usually be quicker to complete.
Note: segment-0 has the header information to create the playable video or audio file.
- Join segments
This step joins the segments to create the video and audio data files. This excludes the first header segment.
- Enter dir video to view the contents of the video subfolder.
- Examine the filenames of the downloaded segments. You need to match the filename structure in the command
- Copy and paste this command:
for /L %A in (1,1,nnn) do (type video\segment-%A-segment-%A.m4s >> video.m4s)
to create the video file. Edit the nnn value and filename structure as required then press Enter to execute.
- Copy the video header segment from the video subfolder using this command:
copy video\segment-0-segment-0.m4s video-header.m4s
Edit the segment-0 filename as required then press Enter
- Do the same with the audio segments, editing the filenames as previously, using the following commands:
for /L %A in (1,1,nnn) do (type audio\segment-%A-segment-%A.m4s >> audio.m4s)
copy audio\segment-0-segment-0.m4s audio-header.m4s
- Create playable video and audio files
You have four key files ready, the video and audio header files and the video and audio data files. You now need to join the headers to their respective data files to create the playable video and audio stream files using ffmpeg as follows:
ffmpeg -i "concat:video-header.m4s|video.m4s" -c copy video.mp4
ffmpeg -i "concat:audio-header.m4s|audio.m4s" -c copy audio.aac
Note: if audio.aac causes an error, try audio.mp3
- Create the final mp4 file
Use the following command to combine the video and audio stream files into the final container. This could be mp4 or mkv
ffmpeg -i video.mp4 -i audio.aac -c copy movie.mp4
Test movie.mp4 in your favourite player (or drop onto a web browser page) to ensure it is playable in the desired resolution with audio.
- Cleanup
This step removes all intermediate files to reclaim space and prepare for next use.
- Delete the audio and video folders containing the segment files.
- Delete the .m4s (or equivalent) files in the main folder.
- Delete video.mp4 and audio.aac/audio.mp3 in the main folder.
| Please Note: |
Where possible, try to use aac for the audio stream. You can use Xmedia Recode to transcode the audio file before you create the final mp4 file. This will produce an mp4 file that is playable on the widest range of devices. Some Apple devices do not support mp3 audio in an mp4 video file. |
|
Return to Tips and Tricks
John's Corner