Does FFMPEG supports AES encryption - encryption

Want to know if the ffmpeg has in-built encryption . I have grabbed the frames from the camera and now I encode the video with this frame using ffmpeg.
But is it possible to encrypt the frames (AES) just like we mention the encode format.

Yes, ffmpeg supports AES.
You can create encrypted HLS segments for example using:
ffmpeg -i <input> -hls_time 10 -hls_key_info_file key_info playlist.m3u8
The same lib is also used for SRTP and possibly other formats.
If you want to encrypt just the I-frames you'll most likely need to write a custom program using the ffmpeg libs.

Yes Support AES Encryption
#!/bin/bash
mkdir -p /opt/FFMPEG/rawContent/
cd /opt/FFMPEG/rawContent/
mkdir -p /opt/FFMPEG/$1/processed/
mkdir -p /opt/FFMPEG/$1/encrypted/
var=`ls | grep -i 'mp4'`
for z in ${var}
do
cd /opt/FFMPEG/$1/encrypted/
fname=`echo ${z} | awk -F "." '{print $1}'`
BASE_URL=" ${fname}.key"
openssl rand 16 > ${fname}.key
echo $BASE_URL > ${fname}.keyinfo
echo ${fname}.key >> ${fname}.keyinfo
echo $(openssl rand -hex 16) >> ${fname}.keyinfo
fname=`echo ${z} | awk -F "." '{print $1}'`
sleep 1
ffmpeg -i /opt/FFMPEG/rawContent/${fname}.mp4 -profile:v baseline -level 4.0 -start_number 0 -hls_time 10 -hls_list_size 0 -hls_key_info_file ${fname}.keyinfo* -f hls ${fname}.m3u8
mv /opt/FFMPEG/rawContent/${fname}.mp4 /opt/FFMPEG/$1/processed/
done
cd -
Run above Shell as
./shellname.sh NameOfTheFolder
(./test.sh TestOne)

Related

See HLS viewer number in Nginx + RTMP module

I am trying to fetch the actual number of HLS viewers in an Nginx with the RTMP module.
I have tried this solution but it is not giving me the real number, I think maybe because it's not counting the viewers who are pulling the stream in HLS format.
Is there any 'good' way to achieve that?
Thanks.
There is a way by counting the number of IPs that requested HLS/DASH fragments in the last 10 to 20 minutes:
#!/bin/bash
date_prefix=$(date +'%d/%b/%Y:%H')
cur_min=$(date +'%M')
cur_min_decimal=${cur_min:0:1}
if [[ $cur_min_decimal == '0' ]]
then
prev_min_decimal=5
else
prev_min_decimal=$(($cur_min_decimal - 1))
fi
cur_minuted_date="${date_prefix}:${cur_min_decimal}"
prev_minuted_date="${date_prefix}:${prev_min_decimal}"
tail -n 10000 /var/log/nginx/access.log \
| grep -E 'GET /(hls|dash)/key-' \
| grep -E "${cur_minuted_date}|${prev_minuted_date}" \
| awk '{print $1}' \
| sort \
| uniq \
| wc -l
If you want to show it on the website, you can CRON that script every minute and output it in the /var/www/html folder

To convert the extension of a file to the different directory by a ffmpeg command

The command below is to convert from mp4 file to jpg file with the same file name in the same directory, .../httpdocs/save/.
[root#server-xxxxx-x ~]# for i in `find /var/www/vhosts/xxxxxx.com/httpdocs/save/ -type f -name "*.mp4"`; do ffmpeg -i $i `echo $i | sed -En "s/.mp4$/.jpg/p"`; done
Now, I need to convert from, .../httpdocs/save/ to the different directory, .../httpdocs/file/, how should I change the command above? I'd appreciate if anyone could help me out.
ffmpeg version 2.2.2
Simple method is to execute the command from the directory containing the input files:
cd "/path/to/inputs" && for i in *.mp4; do ffmpeg -i "$i" -frames:v 1 "/path/to/outputs/${i%.*}.jpg"; done
Or you could use basename if you want to put the full path in the for loop:
for i in /path/to/inputs/*.mp4; do ffmpeg -i "$i" -frames:v 1 "/path/to/outputs/$(basename "$i" .mp4).jpg"; done
...but it is less efficient than only using parameter expansion:
for i in /path/to/inputs/*.mp4; do filepath="${i##*/}"; ffmpeg -i "$i" -frames:v 1 "/path/to/outputs/${filepath%.*}.jpg"; done
Other relevant questions:
How do you convert an entire directory with ffmpeg?
Use ffmpeg to get middle frame of a video?
How can I extract a good quality JPEG image from an H264 video file with ffmpeg?

UNIX for loop with some options

I have for loop:
for mnt `cat $file.txt`
do
grep -h -i -A 3 -B 4 *log | grep -v "10001" >> extrafile.txt
done
What does -A 3 and -B 4 means?
After and Before followed by number of lines
After en Before. After and before what?
No wonder the grep is confusing: You don't mention the "${mnt}" you are searching for. When I improve your script (moving input and output to the end, outside the loop, and using ${mnt}), the script looks like
while read -r mnt; do
grep -h -i -A 3 -B 4 "${mnt}" *log | grep -v "10001"
done < "${file.txt}" >> extrafile.txt
You get the context of every hit from $file.txt and delete all lines with 10001.

FFMPEG Encryption

I am doing a project with encrypting
video and I have a few questions for the procedure.
I used a command to transcode mp4 to HLS with a ts segment duration of ~10 seconds.
First, I need to encrypt those videos with a key from database. However,
I have no idea for the encryption whether working with ffmpeg or not.
Second, if the encryption can work without ffmpeg, so what should I do? I have searched in google which includes something like openssl / aes but
there is no a detailed step for me to follow, even the ffmpeg link:
http://www.ffmpeg.org/ffmpeg-all.html#srtp
Could anyone give me a hand, teaching me how to encrypt a video? Thanks to you.
Yes, you can do it with ffmpeg. You need to write the key from the database to a file, let's say video.key.
You need a second file, let's name it key_info which is the key info file. It has the following format:
key URI
key file path
IV (optional)
Eg:
http://example.com/video.key
video.key
You tell ffmpeg to use it to encrypt your segments with the hls_key_info argument:
ffmpeg -i input.mp4 -c copy -bsf:v h264_mp4toannexb -hls_time 10 -hls_key_info_file key_info playlist.m3u8
This will encrypt your segments with AES-128 in CBC mode and add the relevant tags to your playlist:
#EXT-X-KEY:METHOD=AES-128,URI="http://example.com/video.key"
You can also manually encrypt the segments if you want with openssl. Here's an example script, where each IV is equal to the segment index:
#!/bin/bash
ts_dir=/path/to/ts/
key_file=video.key
openssl rand 16 > $key_file
enc_key=$(hexdump -v -e '16/1 "%02x"' $key_file)
pushd $ts_dir
ts_cnt=$(ls *.ts | wc -l)
((ts_cnt--))
i=0
for i in $(seq -f "%01g" 0 $ts_cnt); do
iv=$(printf '%032x' $i)
ts_file=segment-$i.ts
echo [$i] $ts_file
openssl aes-128-cbc -e -in $ts_file -out encrypted_${ts_file} -nosalt -iv $iv -K $enc_key
done
popd

record / monitor asterisk incoming / outgoing calls on mp3

I am using vicidial based asterisk server 1.2 version. I want my asterisk server to record incoming and outgoing calls on mp3 format rather than the default format wav.
Please help
Install sox and libsox-fmt-mp3, in Debian/Ubuntu:
apt-get install sox libsox-fmt-mp3
Put the command below in your crontab:
nice find /tmp -iname "*.wav" -type f -exec bash \
-c 'WAV={}; MP3=${WAV/%wav/mp3}; sox -r 8000 -c 1 $WAV $MP3' \;
My solution for FreeBSD, one-time run the script, and then set up its periodical launch on the crontab:
#!/bin/sh
find /usr/asterisk/recording -name '*.wav' -type f -mmin +180 | while read filename; do
nice -19 lame -h -v -b 32 "$filename" "${filename%.wav}.mp3" && touch -r "$filename" "${filename%.wav}.mp3" && rm "$filename"
done

Resources