0

I'm trying to write a basic python script that downloads videos from youtube, clips 10 seconds of it, and then concats it with another pre-downloaded video I have in my root directorywhatever.mp4.

I have a list of search words in words.txt that I loop over and download when I run my python script:

import urllib.request
import re
from pytube import YouTube
import os
from moviepy.editor import VideoFileClip, concatenate_videoclips

f = open('words.txt', 'r')
for line in f:
    y = line.split()
    link = urllib.request.urlopen(f"https://www.youtube.com/results?search_query={y}")
    pattern = r"watch\?v=(\S{11})"
    video_ids = re.findall(pattern, link.read().decode())
    vid = f"https://www.youtube.com/watch?v={video_ids[0]}"
    yt = YouTube(vid)
    yt.streams.filter(mime_type="video/mp4", progressive=True).first().download(output_path='highlights', filename=f"{y}.mp4")
    print("Video downloaded as mp4")

for filename in os.listdir("videos"):
    clip = VideoFileClip(filename).subclip(0, 10)
    rick = VideoFileClip('whatever.mp4')
    new = concatenate_videoclips([clip, whatever])
    new.write_videofile(f'new_{filename}.mp4')
f.close()

When I run the script the first video downloads successfully, but I get this error:

$ python3 index.py 
Traceback (most recent call last):
  File "/Users/me/Desktop/Code/pytube_moviepy/index.py", line 23, in <module>
    clip = VideoFileClip(str(filename)).subclip(0, 10)
  File "/opt/homebrew/lib/python3.9/site-packages/moviepy/video/io/VideoFileClip.py", line 88, in __init__
    self.reader = FFMPEG_VideoReader(filename, pix_fmt=pix_fmt,
  File "/opt/homebrew/lib/python3.9/site-packages/moviepy/video/io/ffmpeg_reader.py", line 35, in __init__
    infos = ffmpeg_parse_infos(filename, print_infos, check_duration,
  File "/opt/homebrew/lib/python3.9/site-packages/moviepy/video/io/ffmpeg_reader.py", line 270, in ffmpeg_parse_infos
    raise IOError(("MoviePy error: the file %s could not be found!\n"
OSError: MoviePy error: the file video1.mp4 could not be found!
Please check that you entered the correct path.

Everything else seems to be working which is why I'm confused:

  1. The video is downloaded.
  2. The video is saved as video1.mp4 in the videos directory.
  3. MoviePy can't find the file.

What's more confusing is that if my whatever.mp4 file into the videos directory and then run the second half of my script (by commenting out the first for loop) the videos get concatenated. What am I doing wrong? What's the issue?

My file layout:

.
├── Pipfile
├── Pipfile.lock
├── videos
│   └── video1.mp4
├── index.py
├── words.txt
└── whataver.mp4
Yehuda
  • 27
  • 15
  • In the first line of your for loop, did you mean to have `y = line.strip()` instead of `y=line.split()`? line.split() is going to give you an list of single characters, which I would expect is messing up both your search and the filename generation. The video is saved as `video1.mp4`, but at which ever step the error is occuring, it is looking for `video.mp4`. Maybe generate the filename before calling yt.stream.filter() and store it in a variable so you can print it out as the program runs, instead of calculating `{y}.mp4` as part of the passed parameter. – nigh_anxiety Aug 03 '22 at 22:14
  • when I run `y = line.split()` I get this: `print(y) ['0', '1', '2']` (the numbers being the three parts of the line. That part works – Yehuda Aug 04 '22 at 00:59
  • Also I edited the error `video.mp4` is actually coming out as `video1.mp4` as in the error is saying the correct file could not be found. I just used stand in file names to make it easier. – Yehuda Aug 04 '22 at 01:05
  • 1
    Syntax highlighting should clearly tell you that there's a typo in the posted question. Please don't post code that doesn't run... – Mad Physicist Aug 04 '22 at 01:26
  • EDIT: fixed the typo code was missing a `"` – Yehuda Aug 04 '22 at 01:30

1 Answers1

0

Wow answer was pretty simple. Just had to add the directory in the second for loop:

for filename in os.listdir("videos"):
    clip = VideoFileClip(f"videos/{filename}").subclip(0, 10)
    rick = VideoFileClip('whatever.mp4')
    new = concatenate_videoclips([clip, whatever])
    new.write_videofile(f'new_{filename}.mp4')

I assumed that if I was looping through the directory that it would look for a file within that directory but I guess VideoFileClip needs the path from the root.

Yehuda
  • 27
  • 15