Issue with MoviePy video creation

ringoPRO

8 months ago

Hi,

we are building an video generator with Moviepy for our website. When creating the video, moviepy succeeds writing the audio but then crashes when writing the video. we assume this line code might be the problem:
video_clip.write_videofile(f'{MEDIA_ROOT}/{yt_video_path}', fps=24)
It works totally fine on our local machines.

Did anyone had similar problems with moviepy? Any help would be appreciated!

17 Replies

8 months ago

Hi, could you share which service you use to run the encoding, and what MEDIA_ROOT is set to ?


Status changed to Awaiting User Response railway[bot] 9 months ago


ringoPRO

8 months ago

Our Media Root is: MEDIA_ROOT = os.path.join(BASE_DIR, 'media') Amd what do you mean by which service we use for encoding? We just create an Image and a AudioFileClip and then put the audio on the ImageFileClip. And then we write the Video file Clip:  # Bildclip erstellen
    image_clip = ImageClip(image_path)

    # Audiodatei laden
    audio_clip = AudioFileClip(audio_path)

    # Dauer des Bildclips an die Länge der Audiodatei anpassen
    image_clip = image_clip.set_duration(audio_clip.duration)

    # Audio zum Bild hinzufügen
    title=title.replace('|', '')
    video_clip = image_clip.set_audio(audio_clip)
    yt_video_path = f'yt_videos/{title}.mp4'
   
    # Video exportieren
    video_clip.write_videofile(f'{MEDIA_ROOT}/{yt_video_path}', fps=24)
    
    return yt_video_path 
on our computers everything works fine but on the server it just doesnt create the video and we get a server error 500 in the browser while we dont get a error message in the terminal. Any Help would be appreciated. Thank you.

Status changed to Awaiting Railway Response railway[bot] 9 months ago


8 months ago

Perfect, thank you !

My theory is that you're not writing to the right path and therefore and not using the volume, as services don't have filesystem access and will need to write to volumes.

The mount path for the volume you have on [BeatTracker2] is /app/media, but you're joining BASE_DIR and media which might not result in what you want. Could you try hard-setting to /app/media as a test ? And if that works, you can then just use an env variable to set the path to write videos to.


Status changed to Awaiting User Response railway[bot] 9 months ago


ringoPRO

8 months ago

ah okay but we get this error in our Beattracker-Stage project where we dont have a volume attached I think. But I can still try it do you want me to just make the MEDIA_ROOT to /app/media/?


Status changed to Awaiting Railway Response railway[bot] 9 months ago


ringoPRO

8 months ago

We also use the Media root at some other point to access media files and there it works fine.


8 months ago

ahhh okay if you don't have a volume attached you'll just need to add one and it should work - sorry, thought it was on the service w/a volume. no need to try my suggestion if it works elsewhere


Status changed to Awaiting User Response railway[bot] 9 months ago


ringoPRO

8 months ago

okay Thank you I will try it out. Just for my understanding:So the volume is to store media and static files? Because I thought they are stored on the server. And on stage we are already using other functions to upload media files but there it works and we delete it after a few seconds. where is the difference to the video and the other Media files?


Status changed to Awaiting Railway Response railway[bot] 9 months ago


ringoPRO

8 months ago

okay I tried it out and attached the volume but I still get the server error. This is the content from the terminal there are no error messages... but we get the server error and It didnt create the video.

Attachments


ringoPRO

8 months ago

maybe do we need to make a higher timeout start command since the request is taking longer than usualy?


8 months ago

okay Thank you I will try it out. Just for my understanding:So the volume is to store media and static files? Because I thought they are stored on the server. And on stage we are already using other functions to upload media files but there it works and we delete it after a few seconds. where is the difference to the video and the other Media files?

so volumes are used to store any file. by default a service running on railway won't be given a disk space / writeable file system to work with. if you want to write anything to the file system (and not ram), you'll need a volume attached


Status changed to Awaiting User Response railway[bot] 9 months ago


ringoPRO

8 months ago

maybe do we need to make a higher timeout start command since the request is taking longer than usualy?

I changed the timeout but it didnt have an impact still server error.


Status changed to Awaiting Railway Response railway[bot] 9 months ago


ringoPRO

8 months ago

Maybe it is because in github it doesnt show the yt_videos directory in the media directory? but when I add it to my code github doesnt recognize the empty folder. Do you think this could be a problem? Any help would be really great.


8 months ago

do you have a detailed error log you can share ? but yes, a missing path could be the reason for the failure. let's say we have /folder and we want to write to /folder/sub/file.mp4; this would fail as /folder/sub is missing. most video frameworks I've worked with don't handle ensuring the path exists for you, so this is worth a shot.


Status changed to Awaiting User Response railway[bot] 9 months ago


ringoPRO

8 months ago

do you have a detailed error log you can share ? but yes, a missing path could be the reason for the failure. let's say we have /folder and we want to write to /folder/sub/file.mp4; this would fail as /folder/sub is missing. most video frameworks I've worked with don't handle ensuring the path exists for you, so this is worth a shot.

yes I tried it, created the folder and put a video in it so github could recognize it but still error.Our only error log are the deploy logs. I put a try except statement around the write_video line and now it puts out this error:I thinks its the folder bug but in Github it shows me that the folder should exist. Maybe you can take a look if you see the folder existing?

After writing the video we only store the path in a variable and redirect to another page, so it must be the video.

Attachments


Status changed to Awaiting Railway Response railway[bot] 9 months ago


8 months ago

since you're using a volume and mounting it, adding the directory to github won't help.

I would add code to check if the folder exists and create it if not


Status changed to Awaiting User Response railway[bot] 9 months ago


charliePRO

a month ago

Hi, I'm also having issues with moviepy on Railway (i.e. working completely fine in development but then breaking in production) and perhaps my situation can shed some light (because it's strange!). I have an AWS S3 bucket I store the videos in and then create a secure url to create the video path and I call it:

from moviepy import VideoFileClip
...
video = VideoFileClip(video_path)

This works fine in development but breaks in production. What is strange is that I can print the exact video path and it points to a perfectly valid S3 URL so I don't think it's the video path. To further test this point I've simply tried the URL https://www.w3schools.com/html/mov_bbb.mp4 and that's also failed with the same error which is:

MoviePy error: failed to read the first frame of video file https://www.w3schools.com/html/mov_bbb.mp4. That might mean that the file is corrupted. That may also mean that you are using a deprecated version of FFMPEG. On Ubuntu/Debian for instance the version in the repos is deprecated. Please update to a recent version from the website. : Traceback (most recent call last):

I'm using the latest version of moviepy .


Status changed to Awaiting Railway Response railway[bot] 27 days ago


charliePRO

a month ago

For reference, I was able to solve my issue: https://station.railway.com/questions/moviepy-issue-762629e7