VideoHelp Forum
+ Reply to Thread
Results 1 to 24 of 24
Thread
  1. How do I use the cropdetect filter to automatically crop frames from videos when simultaneously extracting them?
    Quote Quote  
  2. 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.
    Quote Quote  
  3. Originally Posted by _Al_ View Post
    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)
    What should I put in for "SOURCE", "matrix", and "clip"? What's "matrix = clip.get_frame(0).props.get('_Matrix', None)
    if matrix in [ None, 2, 3]: matrix = 1" for? What does it mean? How do I auto crop all frames?
    Quote Quote  
  4. 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
    Autocrop works per frame, so there could be different bars within video and it should work. (Using vapoursynth, not sure about avisynth)

    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.
    Quote Quote  
  5. Originally Posted by _Al_ View Post
    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
    Autocrop works per frame, so there could be different bars within video and it should work. (Using vapoursynth, not sure about avisynth)

    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.
    What does "range=240" mean? Do I put in the file path with the name of my video for "clip"?
    Quote Quote  
  6. 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.
    Quote Quote  
  7. 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.
    Quote Quote  
  8. Originally Posted by _Al_ View Post
    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.
    What should I put in for "matrix"?
    Quote Quote  
  9. 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.
    Quote Quote  
  10. Originally Posted by _Al_ View Post
    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.
    But what if my video is 640x360, 640x480, or 1280x720?
    Quote Quote  
  11. Member DB83's Avatar
    Join Date
    Jul 2007
    Location
    United Kingdom
    Search Comp PM
    Originally Posted by _Al_ View Post
    Oh man, is that you, yet another nick?
    ....................................
    Appreciated that days ago. But, maybe, I am being even more paranoid.


    Let him report me and then see what happens.
    Quote Quote  
  12. 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.
    Quote Quote  
  13. Originally Posted by Sharc View Post
    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.
    Is this video one of them? How do I accurately crop its frames, then?
    Image Attached Files
    Quote Quote  
  14. Originally Posted by VideoFan576 View Post
    Originally Posted by Sharc View Post
    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.
    Is this video one of them? How do I accurately crop its frames, then?
    Something along this line, yes. Actually I don't use autocrop, just made few tests out of curiousity.
    Quote Quote  
  15. 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
    Quote Quote  
  16. How do I only extract certain frames like frames 100-200?
    Quote Quote  
  17. 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
    Quote Quote  
  18. 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)
    Quote Quote  
  19. Now, here's the thing. I want to cut a selection of a video using StaxRip and then make an image sequence out of it with my cropped frames. But imwri automatically makes one without allowing me to cut the video first. How do I fix this?
    Quote Quote  
  20. Can you edit vapoursynth script in StaxRip?
    Quote Quote  
  21. 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
    Quote Quote  
  22. 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.
    Quote Quote  
  23. Originally Posted by _Al_ View Post
    Can you edit vapoursynth script in StaxRip?
    Yes, you could. And how do I cut by timestamps instead of frames in ffmpeg?
    Quote Quote  
  24. You cannot google a ffmpeg command or you cannot turn first frame into a timecode?
    Quote Quote  



Similar Threads

Visit our sponsor! Try DVDFab and backup Blu-rays!