Link to home
Create AccountLog in
Scripting Languages

Scripting Languages

--

Questions

--

Followers

Top Experts

Avatar of Stevie Zakhour
Stevie Zakhour

Python Error
Hi Guys

I'm using Python 3.8.1 and below is a script I use

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

clip = VideoFileClip (print(glob.glob("*.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.audio.duration-2))]
averaged_volumes = np.array([sum(volumes[i:i+10])/10
                             for i in range(len(volumes)-10)])

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('iFLY - Cut.mp4') # low quality is the default

Open in new window


When I run the script, I get an error, see below

['movie.mp4']
Traceback (most recent call last):
  File "C:\Highlights\soccer_cuts.py", line 16, in <module>
    clip = VideoFileClip (print(glob.glob("*.mp4")))
  File "C:\Python38\lib\site-packages\moviepy\video\io\VideoFileClip.py", line 88, in __init__
    self.reader = FFMPEG_VideoReader(filename, pix_fmt=pix_fmt,
  File "C:\Python38\lib\site-packages\moviepy\video\io\ffmpeg_reader.py", line 32, in __init__
    infos = ffmpeg_parse_infos(filename, print_infos, check_duration,
  File "C:\Python38\lib\site-packages\moviepy\video\io\ffmpeg_reader.py", line 245, in ffmpeg_parse_infos
    is_GIF = filename.endswith('.gif')
AttributeError: 'NoneType' object has no attribute 'endswith'

Open in new window


Looking through the error I found line 16 to be the issue

clip = VideoFileClip (print(glob.glob("*.mp4")))

Open in new window


What I'm trying to do with line 16 is set a variable, the file name movie may be called movie1 or movie 2 though there will always be just the 1 mp4 file within the folder called Highlights. I'm hoping you can assist me with this.

Any help is greatly appreciated.

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of aikimarkaikimark🇺🇸

Remove the Print() from the line.

There's some extra white space before the leading (.  Remove it.

I suspect that you want to loop through the glob() result, feeding individual video files to the VideoFileClip() function.

Avatar of Stevie ZakhourStevie Zakhour

ASKER

Thanks Aikimark. Here is the modified script

import glob
import numpy as np # for numerical operations
from moviepy.editor import VideoFileClip, concatenate
clip = VideoFileClip (glob.glob("*.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.audio.duration-2))]
averaged_volumes = np.array([sum(volumes[i:i+10])/10
                             for i in range(len(volumes)-10)])
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('iFLY - Cut.mp4') # low quality is the default

Open in new window


When I run the above I get an error online 4 - clip = VideoFileClip (glob.glob("*.mp4"))

Traceback (most recent call last):
  File "C:\Highlights\soccer_cuts.py", line 4, in <module>
    clip = VideoFileClip (glob.glob("*.mp4"))
  File "C:\Python38\lib\site-packages\moviepy\video\io\VideoFileClip.py", line 88, in __init__
    self.reader = FFMPEG_VideoReader(filename, pix_fmt=pix_fmt,
  File "C:\Python38\lib\site-packages\moviepy\video\io\ffmpeg_reader.py", line 32, in __init__
    infos = ffmpeg_parse_infos(filename, print_infos, check_duration,
  File "C:\Python38\lib\site-packages\moviepy\video\io\ffmpeg_reader.py", line 245, in ffmpeg_parse_infos
    is_GIF = filename.endswith('.gif')
AttributeError: 'list' object has no attribute 'endswith'

Open in new window


Not sure if I'm doing anything wrong here. I also removed all spaces in the script.

Avatar of aikimarkaikimark🇺🇸

1. there is still a space between VideoFileClip and (
2. Is VideoFileClip capable of processing multiple files?  You are returning multiple files with the glob() method.

Do you want to process the files individually?  If so, you need to iterate the glob() results, invoking the VideoFileClip function on each file.

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Thanks Aikimark - I just need the script to process files individually though there will only be 1 file within the Highlights folder at any point in time.

Here is the edited script

import glob
import numpy as np # for numerical operations
from moviepy.editor import VideoFileClip, concatenate
clip = VideoFileClip(glob.glob("*.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.audio.duration-2))]
averaged_volumes = np.array([sum(volumes[i:i+10])/10
                             for i in range(len(volumes)-10)])
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('iFLY - Cut.mp4') # low quality is the default

Open in new window


Running the above script produces the same error. Unsure on how to iterate the glob() results. Any help is awesome

OK, I got it to work. See below script

import glob
import numpy as np # for numerical operations
from moviepy.editor import VideoFileClip, concatenate
path = 'c:\\Highlights\\'
files = [f for f in glob.glob(path + "**/*.mp4", recursive=True)]
for f in files:print(f)
clip=VideoFileClip(f)
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.audio.duration-2))]
averaged_volumes = np.array([sum(volumes[i:i+10])/10
                             for i in range(len(volumes)-10)])
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('iFLY - Cut.mp4') # low quality is the default

Open in new window


ASKER CERTIFIED SOLUTION
Avatar of Stevie ZakhourStevie Zakhour

ASKER

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account
Scripting Languages

Scripting Languages

--

Questions

--

Followers

Top Experts

A scripting language is a programming language that supports scripts, programs written for a special run-time environment that automate the execution of tasks that could alternatively be executed one-by-one by a human operator. Scripting languages are often interpreted (rather than compiled). Primitives are usually the elementary tasks or API calls, and the language allows them to be combined into more complex programs. Environments that can be automated through scripting include software applications, web pages within a web browser, the shells of operating systems (OS), embedded systems, as well as numerous games. A scripting language can be viewed as a domain-specific language for a particular environment; in the case of scripting an application, this is also known as an extension language.