1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Python: editing a video file using opencv (and optionally moviepy, ipython)?

Discussão em 'Python' iniciado por Stack, Setembro 10, 2025.

  1. Stack

    Stack Membro Participativo

    I need to read in a video using opencv, do some processing to it and export it. The code i have written is below. Right now the process is converting video to grayscale.

    import cv2

    # Define the codec and create VideoWriter object
    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    out = cv2.VideoWriter('output.avi',fourcc, 25.0, (960,540))

    cap = cv2.VideoCapture('input.mp4')

    while(cap.isOpened()):
    ret, frame = cap.read()
    if ret == True:
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    out.write(gray)
    else:
    break

    cap.release()
    out.release()


    The input file 'input.mp4' has these details. I am new to this so i don't know which attributes are necessarily needed, so i will post most of them. Right now the video is only writing 5.55 kb, and doesn't play the file if you click on it, says "Windows Media Player encountered a problem while playing this file".

    File Item type: MP4 Video Size: 2.51 MB Video Length: 00:00:08 Frame width: 960 Frame height: 540 Data rate: 2634kbps Total bitrate: 2637kbps Frame rate: 25 frames/second Audio Bit rate: 2kbps Channels: 2(stereo) Audio sample rate: 48 kHz

    Here is the more specific code of what i'm supposed to be doing, but i'm not sure if this is hacked up/pseudocode so i need to understand what i'm doing before i move to this step. If you understand on how to make this work, please clarify on this code as well. I just need to find a way to open, edit and save a working video file, it seems simple but a week has already passed...

    # Import everything needed to edit/save/watch video clips
    from moviepy.editor import VideoFileClip
    from IPython.display import HTML

    white_output = 'white.mp4'
    clip1 = VideoFileClip("solidWhiteRight.mp4")
    white_clip = clip1.fl_image(process_image)
    %time white_clip.write_videofile(white_output, audio=False)

    HTML("""
    <video width="960" height="540" controls>
    <source src="{0}">
    </video>
    """.format(white_output))

    Continue reading...

Compartilhe esta Página