We help IT Professionals succeed at work.
Get Started

Python Error

98 Views
Last Modified: 2020-02-11
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 

Open in new window


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'

Open in new window


Any help is greatly appreciated.
Comment
Watch Question
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant

An Experts Exchange subscription includes unlimited access to online courses.

Get Started
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE