Go Back   Cockos Incorporated Forums > REAPER Forums > REAPER for Video Editing/Mangling

Reply
 
Thread Tools Display Modes
Old 05-14-2019, 07:31 AM   #1
Justin
Administrator
 
Justin's Avatar
 
Join Date: Jan 2005
Location: NYC
Posts: 12,566
Default Video cut detection processor (and ReaScript to add markers)

I posted this in another thread but it really belongs here:

version that detects hue changes more than anything:
Code:
//cut detector (color-change detection)
//@param pixchgcnt "pixel change count" 40 1 99 50 1
//@param distchg   "color change thresh" 50 0 100 50 1
//@gmem=cutdetect

input=0;
colorspace='YV12';
input_info(input,project_w,project_h);
gfx_blit(input);

is_chg=0;
lf ? (
  code = "
    (sqr(su-u) + sqr(sv-v)) > thr ? (c1 += 1);
  ";  
  c1 = 0;
  thr = distchg * 41 / 100;
  cntreq=pixchgcnt * (project_w*project_h*.25*.01);
  gfx_evalrect(0,0,project_w,project_h,code,1|2,lf);
  
  is_chg = (c1 > cntreq);
  gfx_img_free(lf);
);
lf = gfx_img_hold(input);

is_chg && gmem[0] == 0 ? (
  last_project_time==0 || 
    project_time > last_project_time + 1.0 || 
    project_time < last_project_time-1.0 ? 
  (    
    gmem[0] = project_time;
    last_project_time = project_time;
    last_project_time == 0.0 ? last_project_time = 0.00000001;    
  );
);
version that detects luminance changes
Code:
//cut detector (luminance-change)
//@param pixchgcnt "pixel change count" 40 1 99 50 1
//@param distchg   "color change thresh" 50 0 100 50 1
//@gmem=cutdetect

input=0;
colorspace='YV12';
input_info(input,project_w,project_h);
gfx_blit(input);

is_chg=0;
lf ? (
  code = "
    sqr(sy1-y1)+sqr(sy2-y2)+sqr(sy3-y3)+sqr(sy4-y4) > thr ? (c1 += 1);
  ";  
  c1 = 0;
  thr = distchg * 41 / 100;
  cntreq=pixchgcnt * (project_w*project_h*.25*.01);
  gfx_evalrect(0,0,project_w,project_h,code,1|2,lf);
  
  is_chg = (c1 > cntreq);
  gfx_img_free(lf);
);
lf = gfx_img_hold(input);

is_chg && gmem[0] == 0 ? (
  last_project_time==0 || 
    project_time > last_project_time + 1.0 || 
    project_time < last_project_time-1.0 ? 
  (    
    gmem[0] = project_time;
    last_project_time = project_time;
    last_project_time == 0.0 ? last_project_time = 0.00000001;    
  );
);
(both of these will require threshold tweaks without any doubt)

And an EEL reascript which adds markers in response to it:

Code:
//@gmem=cutdetect

function run() (
  a = gmem[0];
  a != 0.0 ? (
    gmem[0] = 0.0;
    AddProjectMarker(0, 0, a, 0, "cut", -1);
  );
  defer("run();");
);

run();
Justin is offline   Reply With Quote
Old 05-14-2019, 07:44 AM   #2
dsyrock
Human being with feelings
 
dsyrock's Avatar
 
Join Date: Sep 2018
Location: China
Posts: 200
Default

Thanks, Justin!
dsyrock is offline   Reply With Quote
Old 05-14-2019, 07:40 PM   #3
dsyrock
Human being with feelings
 
dsyrock's Avatar
 
Join Date: Sep 2018
Location: China
Posts: 200
Default

@Justin

I wrote a lua script to creat the marker

Code:
it=reaper.GetSelectedMediaItem(0, 0)

start=reaper.GetMediaItemInfo_Value(it, "D_POSITION")

length=reaper.GetMediaItemInfo_Value(it, "D_LENGTH")

fr=reaper.SNM_GetIntConfigVar("projfrbase", -1)

d=1/fr

reaper.gmem_attach("cutdetect")

reaper.gmem_write(0, 0.0)

time=math.ceil(length/d)

reaper.SetEditCurPos(start, 0, 0)

for i=1, time do

  cur=reaper.GetCursorPosition()

  reaper.SetEditCurPos(cur+d, 0, 1)

  pos=reaper.gmem_read(0)

  if pos~=0 then

    reaper.AddProjectMarker2(0, 0, pos, 0, "cut:"..i, reaper.CountProjectMarkers(0), 16777471)

    reaper.gmem_write(0, 0.0)

  end

end
But it's too slow. Is there any way to make it faster?
dsyrock is offline   Reply With Quote
Old 05-15-2019, 02:17 AM   #4
musicbynumbers
Human being with feelings
 
musicbynumbers's Avatar
 
Join Date: Jun 2009
Location: brighton, uk
Posts: 12,767
Default

Nice! Will give it a go!
__________________
subproject FRs click here
note: don't search for my pseudonym on the web. The "musicbynumbers" you find is not me or the name I use for my own music.
musicbynumbers is online now   Reply With Quote
Old 05-15-2019, 06:53 AM   #5
Justin
Administrator
 
Justin's Avatar
 
Join Date: Jan 2005
Location: NYC
Posts: 12,566
Default

Quote:
Originally Posted by dsyrock View Post

But it's too slow. Is there any way to make it faster?
You need to have the script defer and run periodically rather than sitting there in a loop. You could do a straight port of it (function run() ...dostuff... reaper.defer(run) end)
Justin is offline   Reply With Quote
Old 05-15-2019, 09:51 AM   #6
mespotine
Human being with feelings
 
mespotine's Avatar
 
Join Date: May 2017
Location: Leipzig, Germany
Posts: 1,681
Default

Quote:
Originally Posted by Justin View Post
You need to have the script defer and run periodically rather than sitting there in a loop. You could do a straight port of it (function run() ...dostuff... reaper.defer(run) end)
Time for my quick introduction into defer-loops

https://forum.cockos.com/showpost.ph...2&postcount=21
__________________
Ultraschall-API - a Lua-functions-library4Reaper: https://forum.cockos.com/showthread....98#post2067798
Reaper Internals - Developerdocs4Reaper: https://forum.cockos.com/showthread.php?t=207635
mespotine is offline   Reply With Quote
Old 05-15-2019, 11:30 AM   #7
dsyrock
Human being with feelings
 
dsyrock's Avatar
 
Join Date: Sep 2018
Location: China
Posts: 200
Default

Thanks Justin and mespotine. Yes running in defer doesn't block the GUI, but it runs as slowly as I play the whole video. Is there any way to get all those gmem[0]~=0 value without actually playing the video?
dsyrock is offline   Reply With Quote
Old 05-15-2019, 02:42 PM   #8
Justin
Administrator
 
Justin's Avatar
 
Join Date: Jan 2005
Location: NYC
Posts: 12,566
Default

Quote:
Originally Posted by dsyrock View Post
Thanks Justin and mespotine. Yes running in defer doesn't block the GUI, but it runs as slowly as I play the whole video. Is there any way to get all those gmem[0]~=0 value without actually playing the video?
Nope, but you could crank up the project playrate to 4x to get it to run faster...
Justin is offline   Reply With Quote
Old 05-15-2019, 07:18 PM   #9
dsyrock
Human being with feelings
 
dsyrock's Avatar
 
Join Date: Sep 2018
Location: China
Posts: 200
Default

Quote:
Originally Posted by Justin View Post
Nope, but you could crank up the project playrate to 4x to get it to run faster...
I have tried 4x 5x 10x, but it will get a wrong project time, which a little later then the correct one
dsyrock is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 06:38 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2019, vBulletin Solutions Inc.