0

I am trying to create a simple rolling recorder with gstreamer using splitmuxsink.

std::string getTime() {
 std::time_t t = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
 std::tm ltime;
 localtime_r(&t, &ltime);
 std::ostringstream s;
 s << std::put_time(&ltime, "%H-%M-%S");
 return s.str();
}
gchararray cb_splitmuxsink_format_location(GstElement*, guint fragmentId, gpointer) {
 std::string name = std::string("segment-") + getTime() + ".mkv";
 LOG4CPLUS_INFO(log4cplus::Logger::getInstance(""), "Creating file " << name);
 return g_strdup(name.c_str());
}
int main(int argc, char** argv) {
 gst_init(&argc, &argv);
 log4cplus::initialize();
 log4cplus::BasicConfigurator::doConfigure();
 auto logger = log4cplus::Logger::getInstance("main");
 auto* pipeline = gst_parse_launch("rtspsrc location=rtsp://127.0.0.1:8554/test latency=0 ! \
 rtph264depay ! h264parse ! queue ! splitmuxsink name=\"splitmuxsink\" location=./segment%02d.mkv \
 max-size-bytes=6000000 muxer=matroskamux \
 muxer-properties=\"properties, streamable=true\"", NULL);
 if (!pipeline) {
 LOG4CPLUS_FATAL(logger, "Failed to create elements");
 return 1;
 }
 GstElement* splitmuxsink = gst_bin_get_by_name(GST_BIN(pipeline), "splitmuxsink");
 g_signal_connect(splitmuxsink, "format-location", G_CALLBACK(cb_splitmuxsink_format_location), NULL);
 GstStateChangeReturn ret = gst_element_set_state(pipeline, GST_STATE_PLAYING);
 if (ret == GST_STATE_CHANGE_FAILURE) {
 LOG4CPLUS_FATAL(logger, "Failed to start pipeline");
 return 1;
 }
 signal(SIGINT, intHandler);
 while (running) {
 }
 gst_element_set_state(pipeline, GST_STATE_NULL);
 gst_deinit();
 return 0;
}

I have an incoming stream with the time written on the video.
This will first create a segment-00-47-17.mkv which starts at the right time. But then I get a segment-00-47-47 which actually starts at 00-47-37, and every following segment is marked 10 seconds later.
I would point that the creation time of those files is also the one in their names and not the one in the video. Also increasing the segment durations with max-size-time still causes the same 10 seconds delay after the first split.

Does anyone know about this behaviour and on how to fix it?

3CEZVQ
43.7k11 gold badges92 silver badges101 bronze badges
asked Dec 9, 2025 at 0:00
1
  • I'd guess some of the video is cached in memory before creating the file, I doubt there's much you can do about that Commented Dec 9, 2025 at 7:08

1 Answer 1

1

You are creating these timestamps yourself. You are just using the current system time. That does not take into account initial buffer times from starting the streams start playing. You are timestamping the files when the files are opened and not take any considerations of the stream. You should probably determine the stream time from the current running pipeline or timestamp of the last sample that has reached the sink.

answered Dec 9, 2025 at 11:41
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.