How to Edit Fragmented MP4 HLS Streams (M3U8) With the Video Editing SDK
By Olga Krovyakova - modified October 2, 2023, created April 1, 2019
With the newly released version 4.2, Solveig Multimedia's Video Editing SDK extends the suported formats.
Current version is generally focused on the online-based stream formats.
Video Editing SDK v.4.2 supports the following formats:
- Fragmented MP4 HLS streams
- HTTP hosted MP4 files
- YouTube online videos
In this article you'll find the demonstration of the Fragmented MP4 HLS streams support.
Fragmented MP4 HLS streams support
Video Editing SDK v.4.2 support the following features for Fragmented MP4 HLS streams:
Playback.
You can easily open the Fragmented MP4 HLS M3U8 stream and work with it just like with the local files. You can navigate through the file as well as play it back. Only parts necessary for preview of the stream will be downloaded to your PC.
To preview the Fragmented MP4 HLS stream, you can build the following graph:
The graph contains a SolveigMM HLS Source as a source filter and Elecard AVC Video Decoder as a video decoder filter. Both filters shipped with the SDK.
Use the SMM Trim C++ Sample Application to test the support of the Fragmented MP4 HLS streams playback and trimming.
The following video demonstrates what you can do with the Fragmented MP4 HLS streams by means of the Video Editing SDK v.4.2:
Editing.
Video Editing SDK is now capable of trimming of the Fragmented MP4 HLS streams. Video Editing Engine is the main component in charge. What you need to do is configure the Video Editing Engine, specify the XTL, describing your current task, then run it and wait for the completion.
Video Editing Engine is a COM component, exposing the ITrimmerObjControl, and you create it as follows:
hr = ::CoCreateInstance( CLSID_TrimmerObjControl, NULL, CLSCTX_INPROC, IID_ITrimmerObjControl, (void**)&m_pITrimmerObjControl ); |
To configure the Editing Engine you sould use the IModuleConfig interface:
hr = m_pITrimmerObjControl->QueryInterface(IID_IModuleConfig, (void **)&m_pModuleCfgEditObj); |
You must use the obtained interface to set an XTL string, containing your trimming task, and optionally specify the callback to track the operation progress:
VARIANT var1 = {0}, var2 = {0}; var1.vt = VT_BSTR; var1.bstrVal = ...; //task string in XTL format hr = m_pModuleCfgEditObj->SetValue( &SMAT_XTLString, &var1 ); var2.vt = VT_BYREF|VT_UI1; var2.pbVal = (BYTE*) &m_xCTrimmerObjControlCB; //pointer to the callback class hr = m_pModuleCfgEditObj->SetValue( &SMAT_Callback, &var2 ); VARIANT var_res = {0}; hr = m_pModuleCfgEditObj->CommitChanges(&var_res); |
You start the trimming process by invoking the ITrimmerObjControl::Start() method:
hr = m_pITrimmerObjControl->Start(); |
And during the trimming process you can ask the Editing Engine for a progress value:
VARIANT var = {0}; hr = m_pModuleCfgEditObj->GetValue( &SMAT_Progress, &var ); |
When the processing is done the Editing Engine invokes the ITrimmerObjControlCB::OnStop() from the callback.
Below you can see the XTL sting example, that contains a trimming task:
<timelines version="2"> |
The source file in this XTL is the Fragmented MP4 HLS file, hosted on http://smarteditingonline.solveigmm.com. Two intervals (represented by 'clip'-tags) will be saved from the source file to the output file:
- 160413589 - 229542097 UNITS (or 16.041 - 22.954 sec) and
- 469719350 - 572525849 UNITS (or 46.971 - 57.252 sec)
If the source file contains multiple streams (e.g. for different bitrate values), the source structure will be recreated.
The test file http://smarteditingonline.solveigmm.com/files/test_multibitrate.m3u8 contains 2 different streams with different bitrate values (see the picture below).
After trimming the output folder (C:\hls) will contain 2 subfolders with both streams and the M3U8 playlist file (output.m3u8) with the description of streams configurations. The following picture shows the structure of the trimmed HLS:
The structure of the output M3U8 file:
#EXTM3U #EXT-X-STREAM-INF:AVERAGE-BANDWIDTH=1007161,BANDWIDTH=1275068,VIDEO-RANGE=SDR,CODECS="mp4a.40.2, avc1.42c01f",RESOLUTION=1280x720,FRAME-RATE=24.000,CLOSED-CAPTIONS=NONE 500K/prog_index.m3u8 #EXT-X-STREAM-INF:AVERAGE-BANDWIDTH=1564155,BANDWIDTH=2042932,VIDEO-RANGE=SDR,CODECS="mp4a.40.2, avc1.42c01f",RESOLUTION=1280x720,FRAME-RATE=24.000,CLOSED-CAPTIONS=NONE 1000K/prog_index.m3u8 |
The trimmed file can be found by the following link:
http://smarteditingonline.solveigmm.com/sdkdemo/hls/output.m3u8
Please refer to the SMM Trim C++ Sample Application source codes for the detailed description on how to configure and run the Video Editing Engine to perform trimming operation.
Frequently Asked Questions
What is a fragmented MP4 HLS stream (M3U8)?
So, why the MP4 file must be fragmented? If you want to use HEVC/H.265 or AVC/H.264, you must use it according to Apple's video encoding standards in their HLS Authoring Specification. Fragmented MP4 files are also compatible with MPEG-DASH, an alternative to HLS, so you can utilize the same files; the only difference is the manifest file (playlist). This results in less encoding and storage requirements, which should save expenses.
Can I edit fragmented MP4 HLS streams with traditional video editing software?
In summary, editing fragmented MP4 HLS streams with traditional video editing software typically involves converting the segments into a complete video file before editing. This process simplifies compatibility, but may result in the loss of certain HLS-specific features.
Are other editing functions available for fragmented HLS MP4 streams besides trimming?
- Playback. The fragmented MP4 HLS M3U8 stream is precisely as easy to access and work with as local files. The file can be played back and browsed through. Your computer will only download the sections of the stream necessary to play the preview.
- Editing. Fragmented MP4 HLS streams can be edited with the use of a video editing software development kit. The core of the system is the video editing engine. After setting up the Video Editing Engine and providing the XTL that describes the present task, you can start the program and wait for it to finish.
For information on how to set up and run the Video Editing Engine for trimming, see the SMM Trim C++ Sample Application source codes.
What platforms are supported with the SDK?
Are there any specific guidelines for handling captions or subtitles in a fragmented MP4 HLS stream?
Can I monetize or insert ads into a fragmented MP4 HLS stream?
Olga Krovyakova is the Technical Support Manager in Solveig Multimedia since 2010.
She is the author of many text and video guidelines of company's products: Video Splitter, HyperCam, WMP Trimmer Plugin, AVI Trimmer+ and TriMP4.
She works with programs every day and therefore knows very well how they work. Сontact Olga via support@solveigmm.com if you have any questions. She will gladly assist you!