How do I use the cropdetect filter to automatically crop frames from videos when simultaneously extracting them?
+ Reply to Thread
Results 1 to 24 of 24
-
-
avisynth,vapoursynth, opencv ,...
example in vapoursynth, extracting frames 100, 200, 300:
Code:import vapoursynth as vs from vapoursynth import core SOURCE = r'F:/source.avi' FRAMES =(100,200,300) IMAGE_FILENAME = r'F:/images/img%06d.png' clip = core.ffms2.Source(source=SOURCE) clip = core.acrop.AutoCrop(clip, range=240) # https://github.com/Irrational-Encoding-Wizardry/vapoursynth-autocrop/releases/tag/0.1 matrix = clip.get_frame(0).props.get('_Matrix', None) if matrix in [ None, 2, 3]: matrix = 1 clip = clip.resize.Bicubic(format=vs.RGB24, matrix_in=matrix) writing = core.imwri.Write(clip, imgformat='PNG', filename=IMAGE_FILENAME, overwrite=True) for frame in FRAMES: writing.get_frame(frame)
Last edited by _Al_; 1st May 2024 at 21:35.
-
-
User inputs are only SOURCE (your video source), FRAMES (frame numbers you want to extract from video) and IMAGE_FILENAME (directory and naming convention for image writer that would include frame number in the name).
clip (video clip name) , matrix (matrix needed for rgb conversion - is pulled from clip or it is defaulted if not in clip) are just script variables, that script uses in process to come up with images, no need to worry about those lines now.
For all frames, you do not need FRAMES, this will process all frames from source.avi video:
Code:import vapoursynth as vs from vapoursynth import core SOURCE = r'F:/source.avi' IMAGE_FILENAME = r'F:/images/img%06d.png' clip = core.ffms2.Source(source=SOURCE) clip = core.acrop.AutoCrop(clip, range=240) # https://github.com/Irrational-Encoding-Wizardry/vapoursynth-autocrop/releases/tag/0.1 matrix = clip.get_frame(0).props.get('_Matrix', None) if matrix in [ None, 2, 3]: matrix = 1 clip = clip.resize.Bicubic(format=vs.RGB24, matrix_in=matrix) writing = core.imwri.Write(clip, imgformat='PNG', filename=IMAGE_FILENAME, overwrite=True) for frame in writing.frames(): #just requesting a frame would initiate image writing pass
That was just an example, if you use Avisynth, you should use avisynth , script would be different, but same logic. Using autocrop and saving images.Last edited by _Al_; 2nd May 2024 at 00:49.
-
-
https://github.com/Irrational-Encoding-Wizardry/vapoursynth-autocrop/wiki/AutoCrop
That would mean that autocrop would check possible bar max left, right, top, bottom for 240 pixels.
The smaller value, the faster it works I guess, but if you have range=20 for example, it will only detect bars up to 20 pixels, it applies autocrop and rest is ignored, if the bar is wider.
Your video path is variable SOURCE. -
It helps to prevent false positives, e.g. in dark scenes where much too large bars could be detected.
Autocrop may also be fooled when the movie aspect ratio intentionally changes between say full 16:9 scenes without bars and 1.85:1 scenes with top and bottom bars, like in some IMAX productions. -
Oh man, is that you, yet another nick?
Code pulls matrix from vapoursynth clip properties, which was set from source plugin. If it does not exist (None) or it is 2 or 3 (not useful) then it defaults to either matrix=1 for HD sources or 5 SD sources. But defaulting could always be wrong. So if the source was originally HD most likely it is 1, you leave 1 there. -
-
-
It is some ai project, translations guessing, no charisma, features, proper background explanations and pissing a human for changing identity. That's not how humans work guys. There is no juice. You can bark a question only so many times, then there has to be a relief of some sort. Fix your matrix.
-
-
-
Bars do not change during video, but are the same thruout the video, you just use more reliable crop then.
Video is 640x358 letterboxed in 640x480. So video is cropped so black bars are removed.
Video has no matrix coefficient encoded/flagged in video so if original is HD it is 1, if SD then 5. Guessing 5.
Code:import vapoursynth as vs from vapoursynth import core SOURCE = r'F:/e1_intro.mkv' # use your correct path IMAGE_FILENAME = r'F:/images/img%06d.png' # use your correct path clip = core.ffms2.Source(source=SOURCE) clip = clip.std.CropAbs(width=640, height=358, left=0, top=58) matrix = clip.get_frame(0).props.get('_Matrix', None) if matrix in [ None, 2, 3]: matrix = 5 clip = clip.resize.Bicubic(format=vs.RGB24, matrix_in=matrix) writing = core.imwri.Write(clip, imgformat='PNG', filename=IMAGE_FILENAME, overwrite=True) for frame in writing.frames(): #just requesting a frame would initiate image writing pass
-
Just before crop line you add one more line:
clip = clip[100:200]
That will cut/slice the clip to only those frames. Frames are numbered from zero in python. Frames will be sliced from 100 to 199 frame. That frame number 200 will not be included. That's how python slicing works. If you wanted to include that frame number 200, you'd need to use:
clip = clip[100:201]
Other examples:
clip = clip[0:100] # will include frames from first frame 0 to frame number 99
clip = clip[600:] #would include frames from frame number 600 to the end of your video -
avif works as well like that, need to specify IMAGE_FILENAME with avif extension and imgformat='AVIF'
but it only works in RGB, not YUV in vapoursynth (imwri plugin of ImageMagick plugin) -
You can always run standalone vapoursynth script, not using Staxrip. You need portable python and portable vapoursynth in a directory.
It might not be intuitive how to do it. But actually I uploaded that setup while ago, for totally different purpose, but you can use that upload.
1.You can upload it from videohelp storage portable python 3.11.3 and vapoursynth R62 , unzip it to a directory of your choice.
2.Make scripts I posted in here, name it, "my_writer.py" and put it in that unzipped directory (there is lots of files already)
3. Update to latest image writer dll. Go here, download latest libimwri.dll, that image Writerdll for vapoursynth and put that dll into unzipped directory into \vapoursynth64\plugins , owerwrite that old libimwri.dll
4.In cmd prompt navigate to that unzipped directory and type:
python my_writer.py
That would generate images -
But all this is most likely possible using ffmpeg, because all you need is just use simple cropping filter and cut your video (though maybe using timestamp instead of frames).
I don't use ffmpeg much, but it should not be problem, google cropping syntax for ffmpeg and find out how to cut video using timestamp.
Similar Threads
-
Auto-Crop Incorrect
By Jay123210599 in forum Newbie / General discussionsReplies: 4Last Post: 20th Nov 2023, 13:07 -
In Staxrip how would you setup QTGMC > auto-crop > resize to 1:1 PAR at 4:3
By LaserBones in forum Newbie / General discussionsReplies: 2Last Post: 29th Jul 2023, 14:19 -
Video players that auto-detect black bars and crop the aspect ratio?
By Nerva in forum Software PlayingReplies: 1Last Post: 18th Apr 2023, 17:57 -
How to find if a video is auto-rotated? Show real auto-rotate header field?
By pxstein in forum Newbie / General discussionsReplies: 5Last Post: 27th Jan 2023, 18:52 -
Auto Crop Black Borders
By m00511 in forum Video ConversionReplies: 17Last Post: 4th Apr 2022, 22:03