How to encrypt the ffmpeg output when generating video chunks

encryptionffmpegfilesopensslpipe

I can openssl encrypt a ffmpeg video stream with

 ffmpeg -i  video1.mp4-video1.mp4.mp4 -f ogg -  | 
 openssl enc -des3 > outptu.ogg.des3

Which strategy would you use to encrypt the ffmpeg output when generating chunks (say of given duration) given by the command:

ffmpeg -f video4linux2 -s vga -i /dev/video0 -f segment -segment_time 1\
-strftime 1 '%Y-%m-%d_%H-%M-%S.ts'

I need to have %Y-%m-%d_%H-%M-%S.ts.des3 instead of %Y-%m-%d_%H-%M-%S.ts

I would love using a name pipe, but it means I have to detect file headers and footers of each chunks generated by ffmpeg.

I guess the straightest solution is to run a background script that encrypt automatically new detected files.

Best Answer

You are using the wrong tool for the job. Instead of piping the output into OpenSSL, write the output to a file which is located on an encrypted filesystem. Encrypted filesystems are designed for random access, file encryption tools such as openssl enc rarely are.

Furthermore openssl enc is bad crypto and should never be used. You're using DES3, which while still legally admissible by some standards is strongly deprecated in favor of AES. And the worst bit is that the way openssl derives a key from a password is ridiculously bad — it doesn't use a proper key stretching function, so brute-forcing through passwords is easy. Using the openssl command line tool for encryption is hard to do right and is never the best tool for the job. Just forget that openssl exists and use proper tools for the job, e.g. LUKS for full disk encryption, Ecryptfs for home directory encryption, gpg or 7z for per-file encryption. Even EncFS to mount an encrypted directory, while flawed, is a lot less broken than openssl.

Related Question