Video cant be opend after saved by filesink

Hi, NVs,

My env is: jetpack4.6, deepstream6.0, python version

I save the mp4 file by filesink in my pipeline and want to dynamic change the video name in pipeline runing. But I find that I cant open the video from second video, though the size of video is normal. I guess it is caused by my operation and lost some important config.

Can you help me check it? Thanks.

            if not on_recording[frame_meta.pad_index]: 
                # pdb.set_trace()
                print("------------------> stream {} start recording".format(frame_meta.pad_index))
                on_recording[frame_meta.pad_index] = True
                filesink[frame_meta.pad_index].set_state(Gst.State.NULL)
                dt = datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S')
                videoname[frame_meta.pad_index] = "stream_{}_{}.mp4".format(frame_meta.pad_index, dt)
                filesink[frame_meta.pad_index].set_property("location", videoname[frame_meta.pad_index])
                filesink[frame_meta.pad_index].set_property("sync", 0)
                filesink[frame_meta.pad_index].set_property("async", 0)
                frame_num[frame_meta.pad_index] = 0
                filesink[frame_meta.pad_index].set_state(Gst.State.PLAYING)
            elif(frame_num[frame_meta.pad_index] >= RECOEDING_FRAME_NUM):
                print("------------------> stream {} stop recording".format(frame_meta.pad_index))
                on_recording[frame_meta.pad_index] = False
                os.system("mv {} ./result/".format(videoname[frame_meta.pad_index]))
            else:
                pass

Try to use gstreamer block probe to block this video file branch in your pipeline and then send the eos event to all blocked element in this branch. Now you can change the property of your filesink and the video could be saved normally. I have tried this and it could work for me

To resume the pipeline and recording the second video, just set the elements state to playing and remove the block probe function.

when you quitting your program, try to sent the eos event to all elements in your pipeline, and the filesink will write down the ‘end character’ to your recording video file, and you can use some simple media program to open it.

Hi, @936214531 can you show me how block the video file branch? It’s great if you can give me some examples. Thanks!

Your code should be something like this, Here is a just example for you:

bool restart_record()
{
    //destroy_pipeline (appCtx[0]);

    GstPad *blockpad;

    blockpad =  gst_element_get_static_pad(appCtx[0]->pipeline.common_elements.primary_gie_bin.tee,"sink");

    gst_pad_add_probe (blockpad, GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM,
                       restart_record_pad_probe_cb, NULL, NULL);


    return TRUE;
}


static GstPadProbeReturn
restart_record_pad_probe_cb (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
{



    GST_DEBUG_OBJECT (pad, "pad is blocked now");

    /* remove the probe first */
    gst_pad_remove_probe (pad, GST_PAD_PROBE_INFO_ID (info));


    //NvDsSinkBinSubBin bin=appCtx[0]->pipeline.instance_bins[0].sink_bin.sub_bins[1];
    NvDsPrimaryGieBin bin = appCtx[0]->pipeline.common_elements.primary_gie_bin;
    //changed_element = bin.bin;

    gst_element_send_event (bin.queue2, gst_event_new_eos());
    gst_element_send_event (bin.transform, gst_event_new_eos());
    gst_element_send_event (bin.cap_filter, gst_event_new_eos());
    gst_element_send_event (bin.encoder, gst_event_new_eos());
    gst_element_send_event (bin.codecparse, gst_event_new_eos());
    gst_element_send_event (bin.mux, gst_event_new_eos());
    gst_element_send_event (bin.sink, gst_event_new_eos());

    gst_element_set_state (bin.queue2, GST_STATE_NULL);
    gst_element_set_state (bin.transform, GST_STATE_NULL);
    gst_element_set_state (bin.cap_filter, GST_STATE_NULL);
    gst_element_set_state (bin.encoder, GST_STATE_NULL);
    gst_element_set_state (bin.codecparse, GST_STATE_NULL);
    gst_element_set_state (bin.mux, GST_STATE_NULL);
    gst_element_set_state (bin.sink, GST_STATE_NULL);


    time_t tt;
    time(&tt);
    tt = tt+8*3600;
    struct tm* t = gmtime(&tt);

    gchar dir_path_record[64];
    gchar file_path_record[128];

    snprintf(dir_path_record,64,"/home/js/disk_data/record_file/%d_%02d_%02d",
             t->tm_year +1900,
             t->tm_mon + 1,
             t->tm_mday);

    if(NULL == opendir(dir_path_record))
        mkdir(dir_path_record,0775);


    snprintf(file_path_record,128,"/home/js/disk_data/record_file/%d_%02d_%02d/%02d_%02d_%02d.mp4",
             t->tm_year +1900,
             t->tm_mon + 1,
             t->tm_mday,
             t->tm_hour,
             t->tm_min,
             t->tm_sec
             );

    g_object_set (G_OBJECT (bin.sink), "location", file_path_record, NULL);


    gst_element_set_state (bin.queue2, GST_STATE_PLAYING);
    gst_element_set_state (bin.transform, GST_STATE_PLAYING);
    gst_element_set_state (bin.cap_filter, GST_STATE_PLAYING);
    gst_element_set_state (bin.encoder, GST_STATE_PLAYING);
    gst_element_set_state (bin.codecparse, GST_STATE_PLAYING);
    gst_element_set_state (bin.mux, GST_STATE_PLAYING);
    gst_element_set_state (bin.sink, GST_STATE_PLAYING);


    gst_object_unref (pad);

    RECORD_STATE = RECORDING;
    return GST_PAD_PROBE_DROP;
}

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.