Fade to black scene detection solution

johnmeyer wrote on 4/10/2008, 6:13 PM
If you have ever needed to cut commercials from a captured commercial broadcast, or have the need to find fade to black points, I have developed a nifty AVISynth script that does this. It runs at about 3x real time on my ancient computer, so I'm sure it will run at 10x real time on the computers most of you use.

You put this script into Notepad and then edit the script with the name and location of your video file. If that file is MPEG, you'll need to download the two DLLs referenced in the script and put the file name on the "mpegsource" line. If it is an AVI file, comment out the mpegsource line and put your file name instead on the AVISource line (and remove the comment "#" character on that line).

You should also specify the output file name and location you want to use.

Open the script in VirtualDub and then run the "Video Analysis Pass." This will create a text file with the frame number at which the video passes the threshold for an all-black frame (change the threshold if it doesn't work for your video). The script will also output a frame number at the point where the video comes back from black.

You then open Vegas, and put your video on the timeline. Then, right-click on the timeline (above your video) and choose Absolute Frames. Finally, open the Edit Details window, select Markers, click on the blank box at the intersection between the row and column headers, and copy/paste the frame numbers from the text file the script created. You will now have markers at the beginning and end of each blank spot in your video. Use whatever editing workflow you normally use to cut unwanted portions of video from your project.

I then save the project as an EDL list and use the EDL to Womble converter which creates the exact cut list I just created, but within Womble which then does lossless cuts on the MPEG-2 files.

Most of you have no need for this, but for those that do -- cutting commercials from a PVR capture, for instance -- it is a real time saver. I'm once again doing the entire Masters tournament for a friend who is actually at the tournament, and finding commercials within 16-20 hours of broadcast coverage is a huge time killer without this. Now, it takes just a few minutes (of my time) and I'm done.

Here's the AVISynth script:


#Script to find blank frames
loadPlugin("c:\Program Files\AviSynth 2.5\plugins\MPEGDecoder.dll")
loadPlugin("c:\Program Files\AviSynth 2.5\plugins\dgdecode.dll")

#Specify the name and location of the output file
filename = "e:\output_blank_frames.txt"
global blankthreshold=24

#Use one of the following to specify the name and location of your video file
#AVISource("e:\Football\Young Life (HDAGC).avi")
mpegsource("E:\Masters 002.mpg")

#Get rid of the borders, which on analog captures can have all sorts of
#black garbage that can affect the averageluma value
Crop(16,16,-16,-16)

#No need for audio when finding blank frames
#audio = mpegsource("E:\Masters 002.mpg")

i=AssumeBFF.ConvertToYV12
j=trim(i,1,0) #Previous frame

# Temporarily un-comment next line to display the average luma value on screen to help determine threshold value
#ScriptClip(i,"Subtitle(String(AverageLuma(i) ))" )

#This line below will output EVERY frame that is below threshold, which results in LOTS of frames
#Normally you don't do this, but it is included for those who want this capability.
#WriteFileIf(last, filename, "(AverageLuma(i)<blankthreshold)", "current_frame+1", append = false)

#This is the line that actually writes the frame number of the FIRST frame that falls below the threshold
WriteFileIf(last, filename, "(AverageLuma(i)<blankthreshold)&&AverageLuma(j)>blankthreshold", "current_frame+1", append = false)

#The line below finds the LAST blank frame.
WriteFileIf(last, filename, "(AverageLuma(i)>blankthreshold)&&AverageLuma(j)<blankthreshold", "current_frame+1", append = false)

#When all finished, write the end of file marker
WriteFileEnd(last, filename, """ "Now the script is closed" """)


Comments

farss wrote on 4/11/2008, 1:50 AM
Thanks John, I think I'll make use of this.
Not for finding commercials but for when the cameraperson has irised down.

Bob.
johnmeyer wrote on 4/11/2008, 9:16 AM
It is far faster than the old ("original") Scenalyzer or any other method I've used. I even wrote a version that added:

separatefields.selectodd

which throws away half the fields before doing the averageluma calculation. This increases performance by about 25%. You can also change the VirtualDub performance settings that control the buffers used and get another 10-15%.

Yesterday I scrubbed through four hours of capture from the Masters golf tournament, and it took about 45 minutes on my 5.5 year-old single core, single CPU Pentium.