FFMPEG:segment fromat add timestamp into filename option

原本只能輸出成 %4d.mp4 ==> 0000.mp4, 0001.mp4
要改成可以像行車紀錄器般的檔案格式 201405020248.mp4

增加 %t 的參數
ffmpeg -i input -f segment -segment_time 60 -reset_timestamps 1 %t.mp4

這邊將 %t.mp4 定義成 Webcam年日月/E年日月時分秒.mp4
strftime(buf1, sizeof(buf1), “WebCam%Y%m%d/E%Y%m%d%H%M%S”, localtime(&tv.tv_sec));

修改方式如下:

修改 ffmpeg source / libavformat / utils.c 檔案
增加引用 #include <sys/time.h>

尋找 av_get_frame_filename function
替換成下面這一個

int av_get_frame_filename(char *buf, int buf_size, const char *path, int number)
{
    const char *p;
    char *q, buf1[50], c;
    int nd, len, percentd_found, percentt_found;
    struct timeval tv;

    q = buf;
    p = path;
    percentd_found = 0;
    percentt_found = 0;
    for (;;) {
        c = *p++;
        if (c == '')
            break;
        if (c == '%') {
            do {
                nd = 0;
                while (av_isdigit(*p))
                    nd = nd * 10 + *p++ - '0';
                c = *p++;
            } while (av_isdigit(c));

            switch (c) {
            case '%':
                goto addchar;
            case 'd':
                if (percentd_found)
                    goto fail;
                percentd_found = 1;
                snprintf(buf1, sizeof(buf1), "%0*d", nd, number);
                len = strlen(buf1);
                if ((q - buf + len) > buf_size - 1)
                    goto fail;
                memcpy(q, buf1, len);
                q += len;
                break;
            case 't':
                if (percentt_found)
                    goto fail;
                percentt_found = 1;

                gettimeofday(&tv, NULL);
                //strftime(buf1, sizeof(buf1), "%Y-%m-%d_%H-%M-%S", localtime(&tv.tv_sec));
                //
                strftime(buf1, sizeof(buf1), "WebCam%Y%m%d/E%Y%m%d%H%M%S", localtime(&tv.tv_sec));
                len = strlen(buf1);
                if ((q - buf + len) > buf_size - 1)
                    goto fail;
                memcpy(q, buf1, len);
                q += len;
                break;
            default:
                goto fail;
            }
        } else {
        addchar:
            if ((q - buf) < buf_size - 1)
                *q++ = c;
        }
    }
    if (!percentd_found && !percentt_found)
        goto fail;
    *q = '';
    return 0;
fail:
    *q = '';
    return -1;
}
This entry was posted in FFMpeg. Bookmark the permalink.