Zulko / moviepy

Video editing with Python
https://zulko.github.io/moviepy/
MIT License
12.07k stars 1.51k forks source link

[WinError 6] The handle is invalid #2126

Open erotavlas opened 4 months ago

erotavlas commented 4 months ago

When code contains an import to Mediapipe, we get an error when trying to write audio clip to file. Mediapipe version is 0.10.10.

Actual Behavior

Exception has occurred: OSError [WinError 6] The handle is invalid

Steps to Reproduce the Problem

add import mediapipe as mp to the top of your code

from moviepy.editor import VideoFileClip, AudioFileClip
from datetime import datetime, timedelta

from PIL import Image
import os

from pathlib import Path
import numpy as np

import subprocess

import mediapipe as mp  # <-- works if this is commented out, does not work with it

def extract_audio(videofile, start, end, output, output_ext="mp3"):
    audioclip = AudioFileClip(videofile)
    audiosubclip = get_subclip(audioclip, start, end)

    audiosubclip.write_audiofile(output, codec='pcm_s16le', write_logfile=True)

def get_subclip(clip, start, end):

    duration = clip.duration # value is in seconds
    # fps = clip.fps
    # width, height = clip.size

    if end == -1:
        dt_object = datetime.strptime(start, '%H:%M:%S.%f') + timedelta(seconds=duration)
        end = dt_object.strftime('%H:%M:%S.%f') 

    return clip.subclip(t_start=start , t_end=end)

if __name__ == "__main__":

    videofile_ext = '.mp4'

    # process mp4 files in directory
    videos_dir = 'Z:/'

    files = [f for f in os.listdir(videos_dir) if os.path.isfile(os.path.join(videos_dir, f))]
    for filename in files:

        video_fullpath = videos_dir + '/' + filename

        if os.path.isfile(video_fullpath) and Path(video_fullpath).suffix == videofile_ext:

            # get the audio wav file
            audio_out = videos_dir + '/' + Path(video_fullpath).stem  + '.wav'

            extract_audio(video_fullpath, '00:00:00.00', -1, audio_out, '.wav')

Specifications

keikoro commented 3 months ago

This sounds like an issue with (named) imports. What happens when you don't use mp?

erotavlas commented 3 months ago

I tried import mediapipe as mpzzz and just import mediapipe and either way it produces the same error. To get around it I refactored my code into two separate python files (one for mediapipe tasks and one for movipy tasks) and execute them separately to avoid this collision.