How to create random filenames with ffmpeg?

Asked

Viewed 100 times

1

I’m compiling a custom version of ffmpeg, and I want it to generate random names but it doesn’t support that, so I need some function to do this with X characters(the quantity is not imported as long as it is on top of 10 and below 20), if possible, I would like to know how to do this only with numbers and with alphanumeric characters.

The goal of this, is to incorporate the generation of random file names, integrating this function to existing, hls->use_localtime/pattern_localtime_fmt representing the generation of names based on datetime.

Using %%Y-%%m-%%dT%%H.%%M.%%S.%%I.ts with -use_localtime 1 it is possible to obtain the following result 2016-10-30T15.12.32.03.ts.

Knowing this, how could I generate a random result as for example
2016-10-30T15.12.32.03-41u7aai73y.ts or 016-10-30T15.12.32.03-07931993501.ts?

It would be possible to change code below to implement the modifications proposed above?

The code hlsenc. c:

static int hls_write_header(AVFormatContext *s)
{
    HLSContext *hls = s->priv_data;
    int ret, i;
    char *p;
    const char *pattern = "%d.ts";
    const char *pattern_localtime_fmt = "-%s.ts";
    AVDictionary *options = NULL;
    int basename_size;

    hls->sequence       = hls->start_sequence;
    hls->recording_time = hls->time * AV_TIME_BASE;
    hls->start_pts      = AV_NOPTS_VALUE;

    if (hls->format_options_str) {
        ret = av_dict_parse_string(&hls->format_options, hls->format_options_str, "=", ":", 0);
        if (ret < 0) {
            av_log(s, AV_LOG_ERROR, "Could not parse format options list '%s'\n", hls->format_options_str);
            goto fail;
        }
    }

    for (i = 0; i < s->nb_streams; i++) {
        hls->has_video +=
            s->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO;
    }

    if (hls->has_video > 1)
        av_log(s, AV_LOG_WARNING,
               "More than a single video stream present, "
               "expect issues decoding it.\n");

    hls->oformat = av_guess_format("mpegts", NULL, NULL);

    if (!hls->oformat) {
        ret = AVERROR_MUXER_NOT_FOUND;
        goto fail;
    }

    if (hls->segment_filename) {
        hls->basename = av_strdup(hls->segment_filename);
        if (!hls->basename) {
            ret = AVERROR(ENOMEM);
            goto fail;
        }
    } else {
        if (hls->flags & HLS_SINGLE_FILE)
            pattern = ".ts";

        if (hls->use_localtime) {
            basename_size = strlen(s->filename) + strlen(pattern_localtime_fmt) + 1;
        } else {
            basename_size = strlen(s->filename) + strlen(pattern) + 1;
        }
        hls->basename = av_malloc(basename_size);
        if (!hls->basename) {
            ret = AVERROR(ENOMEM);
            goto fail;
        }

        av_strlcpy(hls->basename, s->filename, basename_size);

        p = strrchr(hls->basename, '.');
        if (p)
            *p = '\0';
        if (hls->use_localtime) {
            av_strlcat(hls->basename, pattern_localtime_fmt, basename_size);
        } else {
            av_strlcat(hls->basename, pattern, basename_size);
        }
    }

    if ((ret = hls_mux_init(s)) < 0)
        goto fail;

    if ((ret = hls_start(s)) < 0)
        goto fail;

    av_dict_copy(&options, hls->format_options, 0);
    ret = avformat_write_header(hls->avf, &options);
    if (av_dict_count(options)) {
        av_log(s, AV_LOG_ERROR, "Some of provided format options in '%s' are not recognized\n", hls->format_options_str);
        ret = AVERROR(EINVAL);
        goto fail;
    }
    av_assert0(s->nb_streams == hls->avf->nb_streams);
    for (i = 0; i < s->nb_streams; i++) {
        AVStream *inner_st  = hls->avf->streams[i];
        AVStream *outer_st = s->streams[i];
        avpriv_set_pts_info(outer_st, inner_st->pts_wrap_bits, inner_st->time_base.num, inner_st->time_base.den);
    }
fail:

    av_dict_free(&options);
    if (ret < 0) {
        av_freep(&hls->basename);
        if (hls->avf)
            avformat_free_context(hls->avf);
    }
    return ret;
}

This is just an excerpt from hlsenc. c.

1 answer

1

You can use the function tmpnam() to generate random filenames and then concatenate with the date/time you already have available.

For more information visit:

http://www.cplusplus.com/reference/cstdio/tmpnam/

Or type in your linux terminal:

man 3 tmpnam

Browser other questions tagged

You are not signed in. Login or sign up in order to post.