Random extraction of video frames from multiple files

Basically I have a folder with hundreds of video files (*. Avi) each with more or less an hour. What I would like to achieve is a piece of code that can go through each of these videos and randomly select two or three frames from each file and then stitch it together or alternatively save the frames in a folder as a jpeg. I originally thought I could do this with R, but quickly realized that I would need something else that would be possible to work with R.

Is it possible to call FFMPEG from R to accomplish the task above?

I've been spending the internet looking for things to get me started, but most of what I've found is too specific and really applicable to what I need to do.

Can someone help me or just point me in the right direction.

Many thanks

+3


source to share


1 answer


I recently had a related question and found it to be easier to do in bash if you are using a Unix system.

I could step back into oblivion to post this answer here as it is not related to R, but I hope this helps. Something like this worked for me:

#!/bin/bash

for i in *.avi

    do TOTAL_FRAMES=$(ffmpeg -i $i -vcodec copy -acodec copy -f null /dev/null 2>&1 | grep frame | cut -d ' ' -f 1 | sed s/frame=//)

    FPS=ffmpeg -i 18_1*avi -vcodec copy -acodec copy -f null /dev/null 2>&1 | grep fps | cut -d ' ' -f 2 | sed s/fps=//

    for j in {1..3}

        do RANDOM_FRAME=$[RANDOM % TOTAL_FRAMES]

        TIME=$((RANDOM_FRAME/FPS))

        ffmpeg -ss $TIME -i $i -frames:v 1 ${i}_${j}.jpg

    done

done

      



Basically, for each .avi in ​​the directory, the number of frames and FPS is calculated. Then, three random frames are extracted as .jpg using a function $RANDOM

in bash and feeding the random frame by ffmpeg

as time hh:mm:ss

, dividing RANDOM_FRAME

by FPS

.

You can always do these calculations inside R with calls system()

if you are not familiar with bash lingo.

+3


source







All Articles