troubleshooting Question

Python Error

Avatar of Stevie Zakhour
Stevie Zakhour asked on
* python3* Video EditingPythonScripting Languages
4 Comments1 Solution104 ViewsLast Modified:
Hi Guys

I have a python script that takes a video file called video.mp4 and cuts it down based on sound activity. See below. The python script is saved as PythonVideo.py in directory C:\Users\user\Desktop\Video

import numpy as np # for numerical operations
from moviepy.editor import VideoFileClip, concatenate

clip = VideoFileClip("video.mp4")
cut = lambda i: clip.audio.subclip(i,i+1).to_soundarray(fps=22000) 
volume = lambda array: np.sqrt(((1.0*array)**2).mean())
volumes = [volume(cut(i)) for i in range(0,int(clip.duration-1))]

increases = np.diff(averaged_volumes)[:-1]>=0
decreases = np.diff(averaged_volumes)[1:]<=0
peaks_times = (increases * decreases).nonzero()[0]
peaks_vols = averaged_volumes[peaks_times]
peaks_times = peaks_times[peaks_vols>np.percentile(peaks_vols,90)]

final_times=[peaks_times[0]]
for t in peaks_times:
    if (t - final_times[-1]) < 60:
        if averaged_volumes[t] > averaged_volumes[final_times[-1]]:
            final_times[-1] = t
    else:
        final_times.append(t)

final = concatenate([clip.subclip(max(t-5,0),min(t+5, clip.duration))
                     for t in final_times])
final.to_videofile('video1.mp4') # low quality is the default 

The issue here is when I run PythonVideo.py I get lines of error, see below. Is there something wrong I'm doing here? The python version I'm using 3.6.6

Traceback (most recent call last):
  File "C:\Users\user\Desktop\Video\PythonVideo.py", line 7, in <module>
    volumes = [volume(cut(i)) for i in range(0,int(clip.duration-1))]
  File "C:\Users\user\Desktop\Video\PythonVideo.py", line 7, in <listcomp>
    volumes = [volume(cut(i)) for i in range(0,int(clip.duration-1))]
  File "C:\Users\user\Desktop\Video\PythonVideo.py", line 5, in <lambda>
    cut = lambda i: clip.audio.subclip(i,i+1).to_soundarray(fps=22000)
  File "<C:\Program Files\Python36\lib\site-packages\decorator.py:decorator-gen-72>", line 2, in to_soundarray
  File "C:\Program Files\Python36\lib\site-packages\moviepy\decorators.py", line 54, in requires_duration
    return f(clip, *a, **k)
  File "C:\Program Files\Python36\lib\site-packages\moviepy\audio\AudioClip.py", line 126, in to_soundarray
    snd_array = self.get_frame(tt)
  File "<C:\Program Files\Python36\lib\site-packages\decorator.py:decorator-gen-10>", line 2, in get_frame
  File "C:\Program Files\Python36\lib\site-packages\moviepy\decorators.py", line 89, in wrapper
    return f(*new_a, **new_kw)
  File "C:\Program Files\Python36\lib\site-packages\moviepy\Clip.py", line 95, in get_frame
    return self.make_frame(t)
  File "C:\Program Files\Python36\lib\site-packages\moviepy\Clip.py", line 138, in <lambda>
    newclip = self.set_make_frame(lambda t: fun(self.get_frame, t))
  File "C:\Program Files\Python36\lib\site-packages\moviepy\Clip.py", line 190, in <lambda>
    return self.fl(lambda gf, t: gf(t_func(t)), apply_to,
  File "<C:\Program Files\Python36\lib\site-packages\decorator.py:decorator-gen-10>", line 2, in get_frame
  File "C:\Program Files\Python36\lib\site-packages\moviepy\decorators.py", line 89, in wrapper
    return f(*new_a, **new_kw)
  File "C:\Program Files\Python36\lib\site-packages\moviepy\Clip.py", line 95, in get_frame
    return self.make_frame(t)
  File "C:\Program Files\Python36\lib\site-packages\moviepy\audio\io\AudioFileClip.py", line 78, in <lambda>
    self.make_frame = lambda t: self.reader.get_frame(t)
  File "C:\Program Files\Python36\lib\site-packages\moviepy\audio\io\readers.py", line 184, in get_frame
    self.buffer_around(fr_max)
  File "C:\Program Files\Python36\lib\site-packages\moviepy\audio\io\readers.py", line 238, in buffer_around
    array = self.read_chunk(chunksize)
  File "C:\Program Files\Python36\lib\site-packages\moviepy\audio\io\readers.py", line 112, in read_chunk
    s = self.proc.stdout.read(L)
AttributeError: 'NoneType' object has no attribute 'stdout'

Any help is greatly appreciated.
ASKER CERTIFIED SOLUTION
Stevie Zakhour

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 4 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros