I applied the following patch below, it works as expected, but occasionally transcoding to and in other cases ( rare cases depending on the version of compiled ffmpeg ) continues to run but instead of creating multiple segments with random names, it gets all of them stored in a single thread file.
That is, looking at the patch, is there any inconsistency in it? If so, how could I resolve?
diff --git a/libavformat/utils.c b/libavformat/utils.c
index 17ae300..7ff8c22 100644
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -3809,6 +3809,36 @@ uint64_t ff_ntp_time(void)
return (av_gettime() / 1000) * 1000 + NTP_OFFSET_US;
}
+static char *randstring(size_t length) {
+
+ static char charset[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
+ char *randomString;
+
+ if (length) {
+ randomString = malloc(sizeof(char) * (length +1));
+
+ if (randomString) {
+ for (int n = 0;n < length;n++) {
+ int key = rand() % (int)(sizeof(charset) -1);
+ randomString[n] = charset[key];
+ }
+
+ randomString[length] = 'diff --git a/libavformat/utils.c b/libavformat/utils.c
index 17ae300..7ff8c22 100644
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -3809,6 +3809,36 @@ uint64_t ff_ntp_time(void)
return (av_gettime() / 1000) * 1000 + NTP_OFFSET_US;
}
+static char *randstring(size_t length) {
+
+ static char charset[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
+ char *randomString;
+
+ if (length) {
+ randomString = malloc(sizeof(char) * (length +1));
+
+ if (randomString) {
+ for (int n = 0;n < length;n++) {
+ int key = rand() % (int)(sizeof(charset) -1);
+ randomString[n] = charset[key];
+ }
+
+ randomString[length] = '%pre%';
+ }
+ }
+
+ return randomString;
+}
+
+/**
+ * Random filename usage
+ *
+ * use "%<size>r" at -segment_format. <size> is the length of random string generated, for example: "%20r"
+ *
+ * command example:
+ * ffmpeg -i $1 -r 25 -c:a libfaac -ab:a 128k -ac:a 2 -c:v mpeg2video -s:v 640x360 -aspect:v 16:9 -map 0 -f segment -segment_time 120 -segment_list $2.m3u8 -segment_format mpegts "$2-%20r.ts"
+ */
+
int av_get_frame_filename(char *buf, int buf_size, const char *path, int number)
{
const char *p;
@@ -3829,7 +3859,6 @@ int av_get_frame_filename(char *buf, int buf_size, const char *path, int number)
nd = nd * 10 + *p++ - '0';
c = *p++;
} while (av_isdigit(c));
-
switch (c) {
case '%':
goto addchar;
@@ -3846,6 +3875,17 @@ int av_get_frame_filename(char *buf, int buf_size, const char *path, int number)
memcpy(q, buf1, len);
q += len;
break;
+ case 'r':
+ percentd_found = 1;
+ if (number < 0)
+ nd += 1;
+ snprintf(buf1, sizeof(buf1), "%s", randstring(nd));
+ len = strlen(buf1);
+ if ((q - buf + len) > buf_size - 1)
+ goto fail;
+ memcpy(q, buf1, len);
+ q += len;
+ goto addchar;
default:
goto fail;
}
';
+ }
+ }
+
+ return randomString;
+}
+
+/**
+ * Random filename usage
+ *
+ * use "%<size>r" at -segment_format. <size> is the length of random string generated, for example: "%20r"
+ *
+ * command example:
+ * ffmpeg -i $1 -r 25 -c:a libfaac -ab:a 128k -ac:a 2 -c:v mpeg2video -s:v 640x360 -aspect:v 16:9 -map 0 -f segment -segment_time 120 -segment_list $2.m3u8 -segment_format mpegts "$2-%20r.ts"
+ */
+
int av_get_frame_filename(char *buf, int buf_size, const char *path, int number)
{
const char *p;
@@ -3829,7 +3859,6 @@ int av_get_frame_filename(char *buf, int buf_size, const char *path, int number)
nd = nd * 10 + *p++ - '0';
c = *p++;
} while (av_isdigit(c));
-
switch (c) {
case '%':
goto addchar;
@@ -3846,6 +3875,17 @@ int av_get_frame_filename(char *buf, int buf_size, const char *path, int number)
memcpy(q, buf1, len);
q += len;
break;
+ case 'r':
+ percentd_found = 1;
+ if (number < 0)
+ nd += 1;
+ snprintf(buf1, sizeof(buf1), "%s", randstring(nd));
+ len = strlen(buf1);
+ if ((q - buf + len) > buf_size - 1)
+ goto fail;
+ memcpy(q, buf1, len);
+ q += len;
+ goto addchar;
default:
goto fail;
}