With this feature that is also called "transwrapping", you can trim a local file, capture a network stream to a local file, or transcode a list of files in a single file of the same or different container. Note, that for a playlist all the files should be of the same codecs and format (resolution, frame rate, etc).
The feature is based on an approach where data packets from a source (a network stream or a file) are extracted and the same packets are used to create new frames in the output file. The packets are not changed in this process. And as a result, you'll get a direct copy of your source file.
It is similar to the FFmpeg "copy" feature, but it works differently within SDKs.
Closed Captions (CC) can also be transwrapped: add embed_cc=true to the configuration string. This works for MPEG-2, MPEG-4 Part 2 and H.264 video. HEVC copied this way gets no captions; see Embedding, replacing and removing captions in files and streams.
Required settings
Transcoding works within a single process - in this mode, the packets are processed within a single thread so they are not changed. Below you can find additional settings you need to set.
MFormats
m_objMFReader.PropsSet("experimental.mfcodecs", "true");
m_objMFReader.PropsSet("experimental.out_video_packets", "true");
m_objMFReader.PropsSet("experimental.out_audio_packets", "true");
m_objMFReader.PropsSet("external_process", "false");
m_objMFWriter.PropsSet("external_process", "false");MPlatform
For MPlaylist, you should set the properties directly the moment you add a file to the playlist:
m_objPlaylist.PlaylistAdd(null, openMediaFile.FileNames[i], "external_process=false experimental.mfcodecs=true experimental.out_video_packets=true experimental.out_audio_packets=true", ref nIndex, out pFile);For MFile, the properties should be set within a FileNameSet method:
myFile.FileNameSet(pathToFile, "external_process=false experimental.mfcodecs=true experimental.out_video_packets=true experimental.out_audio_packets=true");And for MWriter you should disable external processing:
m_objWriter.PropsSet("external_process", "false");Important! This feature works for original video sources only. Don't use any conversion, i.e. FormatVideoSet(eMFT_Convert, ...) or SourceFrameConvertedGet methods.
If you set the "experimental.out_video_packets" to "true" or "1", the video is still decoded so you can use it for preview. If you don't need the preview and you'd like to increase the overall speed of the encoding, consider setting the following:
m_objMFReader.PropsSet("experimental.out_video_packets", "2");
m_objMFReader.PropsSet("experimental.out_audio_packets", "2");In this case, the video and audio data is not decoded.
Encoding configuration
You should use a specific video and audio codecs for transcoding - "packets" as a video codec and audio codec. For example, for MP4 container, the encoding configuration looks like:
format='mp4' video::codec='packets' audio::codec='packets'DVB Subtitles transwrapping
DVB subtitles can be copied too. For example, to copy the video, audio and two subtitle tracks of a source into an MPEG-TS file:
-
On the source (MFReader or MFile), turn off
external_processas usual, select the subtitle tracks withsubtitle_trackand setexperimental.out_subtitle_packets:myFile.FileNameSet(pathToFile, "external_process=false experimental.mfcodecs=true experimental.out_video_packets=true " + "experimental.out_audio_packets=true experimental.out_subtitle_packets=true subtitle_track=0,1"); -
In the writer configuration, set
packetsas the subtitle codec. All selected tracks are copied:format='mpegts' video::codec='packets' audio::codec='packets' subtitle::codec='packets' -
Optionally set the language and the output PID of each track;
subtitle.1::is the second track:subtitle::metadata::language='eng' subtitle.1::metadata::language='ger' subtitle::pid='1200' subtitle.1::pid='1201' Start the source and the writer.
experimental.out_subtitle_packets=true (1) copies the subtitle packets and also decodes the subtitles, for example for a preview (see Display DVB subtitles and DVB Teletext from MPEG-TS). With 2 they are only copied, which is faster.
With MPlaylist as the source, the writer starts even when the first item has no subtitles, because subtitle.init_without_data is true by default. The subtitle PID, however, exists only if the writer knows the subtitle stream when it starts; to have it from the start, reserve it as described below. If MWriter starts before MPlaylist, also turn off the playlist background:
m_objPlaylist.PlaylistBackgroundSet(null, null, null, out _);
Reserving a DVB subtitle PID before subtitles arrive
The writer creates the streams of its output when it starts and cannot add one later without breaking the output. If the source has no DVB subtitles at that moment, for example a playlist whose first item has none, the output gets no subtitle PID, and subtitles that come later are not written.
To avoid this, describe the subtitle stream in the writer configuration. The writer then announces the PID in the PMT from the start and puts the later subtitle packets into it, without a restart. Until then the PID carries no packets. This works in MFWriter and in the FFmpeg backend of MWriter, for several subtitle tracks and for second outputs (second_encoder and outputs with path).
Add this to the audio and video configuration of the writer:
subtitle::codec='packets'
subtitle::pid='1200'
subtitle::metadata::language='eng'
subtitle::composition_page_id='1'
subtitle::ancillary_page_id='1'
subtitle::subtitling_type='16'
MFormats SDK: set the property before WriterSet, then pass the frames of the source to the writer with ReceiverFramePut in your frame loop. Here writerConfig is the complete configuration:
mfWriter.PropsSet("subtitle.init_without_data", "true"); // the default
mfWriter.WriterSet(outputPath, 1, writerConfig);
MPlatform SDK: use the FFmpeg backend of MWriter and set the property after WriterNameSet:
mWriter.WriterNameSet(outputPath, writerConfig);
mWriter.PropsSet("encoder::subtitle.init_without_data", "true"); // the default
mWriter.ObjectStart(source);
Rules:
- Set the three DVB fields together, as decimal numbers. They describe the packets that will be copied, so use the page IDs of your subtitle source. They work only with
subtitle::codec='packets'. - Later packets must match these page IDs and the mapping of input tracks. The writer does not renumber pages or remap tracks.
- Give the complete configuration in the first
WriterSetorWriterNameSetcall. Later partial calls can drop the subtitle settings. - An invalid configuration is rejected with
E_INVALIDARG. If an explicitly configured subtitle stream cannot be created, the writer does not start; a stream found automatically on the first frame does not stop it. - Instead of the three fields you can pass a complete description in
subtitle.stream_info. It takes precedence.
| Parameter | Use |
|---|---|
subtitle::composition_page_id |
Composition page ID of the copied stream, 0 to 65535. |
subtitle::ancillary_page_id |
Ancillary page ID of the copied stream, 0 to 65535. |
subtitle::subtitling_type |
DVB subtitling type, 0 to 255. The 16 in the example is a value, not a default. |
subtitle::pid |
Output TS PID, for example 1200. Choose an unused PID; it is not related to the page IDs. |
subtitle::metadata::language |
Language of the track, for example eng. |
subtitle::stream_id |
Index of the input subtitle stream that the description belongs to. It is not the output PID. |
subtitle.stream_info |
Complete description of the input stream, for example subtitle.stream_info="codec='dvbsub' time_base='1/90000' codec_data='0001000110'". A non-empty value takes precedence over the three fields and over the description on the first frame. An empty value lets the writer take the description from the first frame. |
subtitle.init_without_data (writer property; MPlatform: encoder::) |
Lets audio and video start without subtitle data. True by default. Does not reserve a PID by itself. |
For more tracks, use subtitle.1::, subtitle.2:: and so on with the same fields, and subtitle.stream_info.1, subtitle.stream_info.2 for explicit descriptions.
Trimming between key frames
Compressed video stores full pictures only in key frames (I-frames). The frames after a key frame, up to the next one, are coded as changes against it; together they form a group of pictures (GOP). A copy without re-encoding can therefore start only at a key frame.
When the in point of a trim falls inside a GOP, the reader by default continues from the next key frame, and the frames between the in point and that key frame are lost. For example, with a GOP of 12 frames at 25 fps and the in point at 15.0 s, the output starts at 15.36 s.
To keep these frames, enable packets.seek_from_keyframe on the reader. A positioning, such as an in point or a seek, then starts at the key frame before the requested time, the start of its GOP. The frames of that GOP before the in point are marked to be discarded, and the result depends on the container:
- MXF: the frames are written as pre-charge, and the track origin counts them. Players that honor the origin start at the in point.
- MPEG-TS and MOV: the frames are shown, so the file starts slightly before the in point, at the key frame.
| Property | Applied to | Default | Description |
|---|---|---|---|
packets.seek_from_keyframe |
MFReader / MFile (file::) |
false | Starts a positioning from the key frame before the requested time. Works only when the reader outputs packets without decoding: experimental.out_video_packets=2. |
MPlatform SDK, trimming a file from 15 s to 45 s:
myWriter.PropsSet("external_process", "false");
myWriter.WriterNameSet(pathToOutput, "format='mxf' video::codec='packets' audio::codec='packets'");
myWriter.ObjectStart(myFile);
myFile.PropsSet("file::packets.seek_from_keyframe", "true");
myFile.FileNameSet(pathToFile, "external_process=false experimental.mfcodecs=true " +
"experimental.out_video_packets=2 experimental.out_audio_packets=2");
myFile.FileInOutSet(15.0, 45.0); // trim from 15 s to 45 s
myFile.FilePlayStart();MFormats SDK:
myReader.ReaderOpen(pathToFile, "external_process=false experimental.mfcodecs=true " +
"experimental.out_video_packets=2 experimental.out_audio_packets=2 packets.seek_from_keyframe=true");
myWriter.PropsSet("external_process", "false");
myWriter.WriterSet(pathToOutput, 1, "format='mxf' video::codec='packets' audio::codec='packets'");
// The request by time returns the key frame of the GOP first
myReader.SourceFrameGetByTime(15.0, -1, out MFFrame frame, "");
while (frame != null)
{
frame.MFTimeGet(out M_TIME time);
if (time.rtStartTime >= 45 * 10000000L) // the out point, in 100 ns units
{
Marshal.ReleaseComObject(frame);
break;
}
myWriter.ReceiverFramePut(frame, -1, "");
Marshal.ReleaseComObject(frame);
myReader.SourceFrameGet(-1, out frame, "");
}
myWriter.WriterClose(0);Things to know:
- The property is off by default because a reader with it returns frames from before the requested time.
- A second positioning inside the same GOP repeats the frames of that GOP.
- The out point is not changed and can include one more frame.
- MOV shows the frames before the in point; no edit list hides them.
- To play such an MXF in the SDK from the in point, open it with
mxf.use_track_origin=true, see below.
Preserving MXF pre-charge and track origin
Some MXF files contain leading frames that are required for decoding but should not be presented during normal playback. These frames are called pre-charge or pre-roll. The MXF track origin identifies the intended playback start after these frames.
When transwrapping such files to MXF, the SDK can preserve this information so that playback starts at the intended frame instead of displaying the leading pre-charge frames.
| Parameter | Applied to | Default | Description |
|---|---|---|---|
mxf.use_track_origin |
MFReader / MFile | false |
Makes the Reader honor the source MXF track origin and identify pre-charge packets. Enable before opening the source. |
track_origin |
MFWriter / MWriter configuration |
0 — automatic |
Sets the output MXF track origin in edit units, normally video frames. A positive value overrides automatic selection. |
For automatic preservation, enable mxf.use_track_origin=true on the source and leave track_origin unspecified in the Writer configuration. The MXF muxer can derive the origin from the leading video packets marked as pre-charge.
MFormats SDK:
myReader.ReaderOpen(pathToFile, "external_process=false experimental.mfcodecs=true experimental.out_video_packets=1 experimental.out_audio_packets=1 mxf.use_track_origin=true");
myWriter.PropsSet("external_process", "false");
myWriter.WriterSet(pathToOutput, 1, "format='mxf' video::codec='packets' audio::codec='packets'");Pass the original frames from the Reader to the Writer without format conversion, as described above.
MPlatform SDK:
myFile.FileNameSet(pathToFile, "external_process=false experimental.mfcodecs=true experimental.out_video_packets=true experimental.out_audio_packets=true mxf.use_track_origin=true");
myWriter.PropsSet("external_process", "false");
myWriter.WriterNameSet(pathToOutput, "format='mxf' video::codec='packets' audio::codec='packets'");If an explicit origin is required, add track_origin to the Writer configuration:
format='mxf' track_origin='5' video::codec='packets' audio::codec='packets'This declares five edit units of pre-charge before the intended playback start. It changes the MXF origin metadata; it does not physically remove or add encoded frames.
Note: Track origin is separate from the starting timecode. To play or verify the resulting MXF in the SDK while honoring its origin, open it with mxf.use_track_origin=true as well.
Still have questions?
Tell us what didn’t work or what you’d like clarified.