Extracting Video Frames

Visuals, audio, animation. Blended, not stirred. If LiveCode is part of your rich media production toolbox, this is the forum for you.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
oliver.hardt
Posts: 7
Joined: Fri Oct 29, 2010 2:31 am

Extracting Video Frames

Post by oliver.hardt » Sun May 11, 2014 7:26 pm

Hello: I would like to analyze a video frame-by-frame.

In the forums I found an approach in which snapshots are taken from a running video. Based on this idea, I tried to advance the movie frame by frame (play step forward lClip; see below). This has the disadvantage that it is rather slow because apparently the video frame has first to be drawn, which requires time, and thus one needs to wait a bit before playing the next frame.

However, it is not necessary to store the frames into external video files, as is done in the handler below. This handler is intended at the moment to just be a proof of concept for the frame extraction process. It would suffice in the final version to take snapshot and then analyze it, then move to the next frame.

Any clever ideas as to how to speed this up?

Cheers,
Olli.

Code: Select all

on lH_extractFrames pVideoPlayer, pNoOfFramesToExtract
     -- figure out where the snapshot files shall be stored   
     answer folder "Where shall the images be stored?"
      put it into lFolder
     
     -- get rid of the control bar 
     set the showController of player pVideoPlayer to false
     
   -- this variable will be used to produce unique names for the snapshot files
   put 1 into lCount
   
   repeat pNoOfFramesToExtract
            -- move the player one frame forward
            play step forward pVideoPlayer
      
           -- wait so the frame can be drawn on the screen
            wait 500 milliseconds
      
           -- take a snapshot from the player and store it into a file
            export snapshot from player pVideoPlayer to file (lFolder&"/"&"snapshot"&lCount&".png") as png
      
            -- increase the file name counter
          add 1 to lCount
      end repeat
      
      set the showController of player pVideoPlayer to true
end lH_extractFrames

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4003
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Extracting Video Frames

Post by bn » Mon May 12, 2014 1:27 am

Hi Olli,

try this as script of your player:

Code: Select all

local sHowMany, sCounter
local sExport
local sFolder, sVideoPlayer
local sTime

on currentTimeChanged
   if sCounter >= sHowMany then
      -- finish up
      put false into sExport
      set the showController of player sVideoPlayer to true
      put "each export took: " & (the milliseconds - sTime) / sHowMany & " milliseconds" --  into field "fRes"
      exit currentTimeChanged
   end if
   
   if sExport then
      add 1 to sCounter
      lH_extractFrames
   end if
     send triggerNext to me in 5 milliseconds
end currentTimeChanged

on triggerNext
   play step forward sVideoPlayer
end triggerNext

on mouseUp
   -- initialize script local variables
   put 5 into sHowMany
   put 0 into sCounter
   
   answer folder "Where shall the images be stored?"
   if it is empty then exit mouseUp
   put it into sFolder
 
   put the short name of me into sVideoPlayer
   set the showController of player sVideoPlayer to false
   put true into sExport
   put the milliseconds into sTime
   
   -- get everything going
   play step forward sVideoPlayer
end mouseUp

on lH_extractFrames         
   -- take a snapshot from the player and store it into a file
   export snapshot from player sVideoPlayer to file (sFolder&"/"&"snapshot"&sCounter&".png") as png
end lH_extractFrames
Obviously you have to click at the player to start export.
I put it into the player because the player gets the "currentTimeChanged" message, could go up in the message path.

Had to do the retrigger by send in time in 5 milliseconds, somehow did not work otherwise. First I used 0 milliseconds but had the impression it stalled after 6 frames, increased to 5 milliseconds and worked for 100 exports.
Not tested extensively. Would need a couple checks in real app.
It worked for me, tested up to 100 frames, times for 1 frame around 140 milliseconds.

Kind regards
Bernd

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Extracting Video Frames

Post by Klaus » Mon May 12, 2014 1:19 pm

Hi Oliver,

as long as QuickTime is supported by Livecode, you might want to take a look at this free external:
http://www.bluemangolearning.com/revolu ... rnals/eqt/


Best

Klaus

Post Reply

Return to “Multimedia”