Skip to content

Instantly share code, notes, and snippets.

@gtgt
Last active April 2, 2023 10:05
Show Gist options
  • Save gtgt/ccc6c97da122092bc15a to your computer and use it in GitHub Desktop.
Save gtgt/ccc6c97da122092bc15a to your computer and use it in GitHub Desktop.
ffmpeg - random filename segment patch (this way you only need to protect the playlist, not the segments)
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";
+ for (int n = 0; n < length - 1; n++) {
+ int key = rand() % (int)(sizeof(charset) - 1);
+ target[n] = charset[key];
+ }
+ if (length) target[length - 1] = '\0';
+ return target;
+}
+
+/**
+ * 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;
@@ -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;
+ len = sizeof(buf1);
+ if (len > nd + 1) len = nd + 1;
+ randstring(buf1, len);
+ if ((q - buf + len) > buf_size - 1)
+ goto fail;
+ memcpy(q, buf1, len);
+ q += len;
+ goto addchar;
default:
goto fail;
}
#!/bin/bash
DIR=$(dirname $0)
mkdir -p $DIR/build
PREFIX=$DIR/build
./configure --extra-version=static --prefix=$PREFIX --libdir=$PREFIX/lib --shlibdir=$PREFIX/lib --mandir=$PREFIX/share/man --disable-shared --enable-static \
--optflags='-O2 -march=native -pipe -static' --extra-cflags='-O2 -march=native -pipe -static' --extra-cxxflags='-O2 -march=native -pipe -static' \
--enable-gpl --enable-postproc --enable-avfilter --enable-avresample --disable-stripping --enable-version3 --enable-nonfree --enable-indev=v4l2 --enable-outdev=v4l2 --enable-indev=alsa --disable-indev=oss \
--disable-indev=jack --enable-outdev=alsa --disable-outdev=oss --disable-outdev=sdl --enable-version3 --enable-bzlib --disable-runtime-cpudetect --disable-debug --disable-doc --disable-gnutls --enable-hardcoded-tables \
--enable-iconv --enable-network --disable-openssl --enable-ffplay --enable-ffserver --enable-vaapi --enable-vdpau --enable-zlib --enable-libvo-aacenc --disable-libvo-amrwbenc --enable-libmp3lame --disable-libaacplus --enable-libfaac \
--enable-libtheora --disable-libtwolame --disable-libwavpack --disable-libwebp --enable-libx264 --disable-libx265 --enable-libxvid --disable-libcdio --disable-libiec61883 --disable-libdc1394 --disable-libcaca --enable-openal \
--enable-opengl --enable-libv4l2 --disable-libpulse --enable-x11grab --disable-libflite --disable-frei0r --disable-fontconfig --disable-ladspa --disable-libass --disable-libfreetype --disable-libsoxr --enable-pthreads \
--enable-libopencore-amrwb --enable-libopencore-amrnb --disable-libfdk-aac --disable-libopenjpeg --disable-libbluray --disable-libcelt --disable-libgme --disable-libgsm --disable-libmodplug --enable-libopus --disable-libquvi \
--enable-librtmp --disable-libssh --disable-libschroedinger --enable-libspeex --enable-libvorbis --enable-libvpx --disable-libzvbi --disable-armv5te --disable-armv6 --disable-armv6t2 --disable-neon --disable-vfp --disable-mips32r2 \
--disable-mipsdspr1 --disable-mipsdspr2 --disable-mipsfpu --disable-altivec --disable-avx --disable-avx2 --enable-pic --disable-asm --cpu=host
#!/bin/bash
#/bin/bash
[[ $1 && $2 ]] || exit 1
DIR=$(dirname $0)
$DIR/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"
@MythodeaLoL
Copy link

Can u fix it for ffmpeg n2.7.7? I'm running, but this code is not optimized, because this, i'm rething problems.
See
http://codereview.stackexchange.com/questions/29198/random-string-generator-in-c
http://pt.stackoverflow.com/q/166819/8984
Can u help me @gtgt ?

@gtgt
Copy link
Author

gtgt commented Sep 11, 2017

Updated the patch according to your question on stackoverflow.

@dogenius01
Copy link

dogenius01 commented Aug 19, 2019

Hi,
This is what i exactly wanted from ffmpeg hls(-hls_segment_filename).
Will you guide how to apply this patch?

Copy link

ghost commented Apr 2, 2023

Would also intresst me, can you please give out a patch for FFmpeg 6.0 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment