This tutorial records a playing file or a live source with MWriter. It uses the objects from
MPlatform 1: Playing a file and MPlatform 2: Capturing from a device.
Choose the container and the codecs
The writer describes its choices as configuration lists: "format" for the containers, then
"format::video" and "format::audio" for the codecs of the selected container.
ConfigSet selects one item, ConfigGetAll returns the whole configuration as one line.
private readonly MWriterClass m_writer = new MWriterClass();
private static void FillConfig(MWriterClass writer, string type, ComboBox box)
{
box.Items.Clear();
writer.ConfigGetCount(type, out int count);
for (int i = 0; i < count; i++)
{
writer.ConfigGetByIndex(type, i, out string name);
box.Items.Add(name);
}
}
private void FormatBox_SelectedIndexChanged(object sender, EventArgs e)
{
m_writer.ConfigSet("format", (string)formatBox.SelectedItem, out IMAttributes formatProps);
FillConfig(m_writer, "format::video", videoCodecBox);
FillConfig(m_writer, "format::audio", audioCodecBox);
}
private void VideoCodecBox_SelectedIndexChanged(object sender, EventArgs e)
{
m_writer.ConfigSet("format::video", (string)videoCodecBox.SelectedItem, out IMAttributes codecProps);
}
private void AudioCodecBox_SelectedIndexChanged(object sender, EventArgs e)
{
m_writer.ConfigSet("format::audio", (string)audioCodecBox.SelectedItem, out IMAttributes codecProps);
}
Start and stop the recording
WriterNameSet sets the file or the network URL and the configuration; ObjectStart starts the
writer on the source. The configuration line can come from ConfigGetAll or be written by hand:
private void StartRecording()
{
m_writer.ConfigGetAll(1, out string config); // for example "format='mp4' video::codec='mpeg4' audio::codec='aac'"
m_writer.WriterNameSet(@"C:\Video\capture.mp4", config);
m_writer.ObjectStart(m_live); // or m_file
}
private void StopRecording()
{
m_writer.ObjectClose(); // finishes and closes the file
}
The writer records what the source plays, in real time: a file must be playing (FilePlayStart), a live
source must be started (ObjectStart(null)). Calling WriterNameSet("", "") while the writer runs
switches to a new file with an automatic name and the same configuration. Close the writer before you close the source,
and release both objects when the form closes.
Next steps
- Container and codec options: Audio and video codecs and MWriter object properties.
- The SDK sample
Writer Samplerecords a live source to files and network streams, with pause and file switching.
Still have questions?
Tell us what didn’t work or what you’d like clarified.