OSError: [WinError 6] The handle is invalid Python 3.10 Windows moviepy

38 Views Asked by At

I'm trying to make a clip using MoviePy but I getting error OSError: [WinError 6]

Using Windows 11, Python 3.10.11 This is the method I'm trying to replicate. (The full github is here)

def clip(
        content: str, 
        video_file: str, 
        outfile: str, 
        image_file: str = '', 
        offset: int = 0, 
        duration: int = 0):
    """
    Generate the Complete Clip
    content: str - Full content text
    video_file: str - Background video
    outfile: str - Filename of output
    image_file: str - Banner to display
    offset: int - Offset starting point of background video (default: 0)
    duration: int - Limit the video (default: audio length)
    """
    audio_comp, text_comp = generate_audio_text(split_text(content))

    audio_comp_list = []
    for audio_file in track(audio_comp, description='Stitching Audio...'):
        audio_comp_list.append(AudioFileClip(audio_file))
    audio_comp_stitch = concatenate_audioclips(audio_comp_list)
    audio_comp_stitch.write_audiofile('temp_audio.mp3', fps=44100)

    audio_duration = audio_comp_stitch.duration
    if duration == 0:
        duration = audio_duration

    audio_comp_stitch.close()

    vid_clip = VideoFileClip(video_file).subclip(offset, offset + duration)
    vid_clip = vid_clip.resize((1980, 1280))
    vid_clip = vid_clip.crop(x_center=1980 / 2, y_center=1280 / 2, width=720, height=1280)

    if image_file != '':
        image_clip = ImageClip(image_file).set_duration(duration).set_position(("center", 'center')).resize(0.8) # Adjust if the Banner is too small
        vid_clip = CompositeVideoClip([vid_clip, image_clip])

    vid_clip = CompositeVideoClip([vid_clip, concatenate_videoclips(text_comp).set_position(('center', 860))])

    vid_clip = vid_clip.set_audio(AudioFileClip('temp_audio.mp3').subclip(0, duration))
    vid_clip.write_videofile(outfile, audio_codec='aac')
    vid_clip.close()

My overly simplified code looks like this:

clip1 = TextClip(
  txt="test",
  font="Komika",  # Change Font if not found
  fontsize=32,
  color="white",
  align="center",
  method="caption",
  size=(660, None),
  stroke_width=2,
  stroke_color="black",
)
clip2 = TextClip(
  txt="webos",
  font="Komika",  # Change Font if not found
  fontsize=32,
  color="white",
  align="center",
  method="caption",
  size=(660, None),
  stroke_width=2,
  stroke_color="black",
)
conc = concatenate_videoclips([clip1, clip2]).set_position(("center", 860))

The problem is happening in the concatenate statement.

The error output:

PS C:\Users\ernes\OneDrive\Desktop\Bots\autocap\autocap_with_mp3> python .\autocap_with_mp3.py text.txt
Traceback (most recent call last):
  File "C:\Users\ernes\OneDrive\Desktop\Bots\autocap\autocap_with_mp3\autocap_with_mp3.py", line 218, in <module>
    main()
  File "C:\Users\ernes\OneDrive\Desktop\Bots\autocap\autocap_with_mp3\autocap_with_mp3.py", line 207, in main
    generate_video(
  File "C:\Users\ernes\OneDrive\Desktop\Bots\autocap\autocap_with_mp3\autocap_with_mp3.py", line 165, in generate_video
    conc = concatenate_videoclips([clip1, clip2]).set_position(("center", 860))
  File "C:\Users\ernes\AppData\Local\Programs\Python\Python310\lib\site-packages\moviepy\video\compositing\concatenate.py", line 71, in concatenate_videoclips
    tt = np.cumsum([0] + [c.duration for c in clips])
  File "C:\Users\ernes\AppData\Local\Programs\Python\Python310\lib\site-packages\numpy\core\fromnumeric.py", line 2586, in cumsum
    return _wrapfunc(a, 'cumsum', axis=axis, dtype=dtype, out=out)
  File "C:\Users\ernes\AppData\Local\Programs\Python\Python310\lib\site-packages\numpy\core\fromnumeric.py", line 56, in _wrapfunc
    return _wrapit(obj, method, *args, **kwds)
  File "C:\Users\ernes\AppData\Local\Programs\Python\Python310\lib\site-packages\numpy\core\fromnumeric.py", line 45, in _wrapit
    result = getattr(asarray(obj), method)(*args, **kwds)
TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'
Exception ignored in: <function FFMPEG_AudioReader.__del__ at 0x000001E727215120>
Traceback (most recent call last):
  File "C:\Users\ernes\AppData\Local\Programs\Python\Python310\lib\site-packages\moviepy\audio\io\readers.py", line 254, in __del__
    self.close_proc()
  File "C:\Users\ernes\AppData\Local\Programs\Python\Python310\lib\site-packages\moviepy\audio\io\readers.py", line 149, in close_proc
    self.proc.terminate()
  File "C:\Users\ernes\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 1589, in terminate
    _winapi.TerminateProcess(self._handle, 1)
OSError: [WinError 6] The handle is invalid
PS C:\Users\ernes\OneDrive\Desktop\Bots\autocap\autocap_with_mp3> 

I've tried closing the vid_clip variable and audio clips I have throughout the code but nothing seems to work.

I'm trying to solve this without the need of multithreading, because I saw a couple of fixes that involved that but I don't think it is necessary in my case

1

There are 1 best solutions below

1
ernesto casco velazquez On

The main problem with my code was that the TextClips got to STRICTLY have a duration defined - Through set_duration() or setting BOTH start and end.

My issue here was the lack of set_end after set_start.