0

I have been developing this small application to download and cut Youtube videos. It works fine but an error or misformatted message is the issue I have not fixed. When it comes to the cutting process, the function ffmpeg_extract_subclip is used and right after that point, I get the weird error below: enter image description here

Below, the script working fine. enter image description here

The function responsible for cutting the video

# cutting the video by section
def cut_video(video_local_path, start_time, end_time, final_file):

    print("- Cutting your video...")
    ffmpeg_extract_subclip(video_local_path, time_to_sec(start_time), time_to_sec(end_time), targetname=f"{final_file}.mp4")

If necessary, you can check the full code here on github.

I have extensively read the API for ffmpeg and moviepy, debugged on vscode and checked alternatives like VideoFileClip but it would never give me the same performance.

Thanks in advance.

João Victor
  • 65
  • 1
  • 1
  • 5
  • Have you considered just calling FFmpeg yourself? Using subprocess, it should be as simple as `sp.run([ffmpeg_path, '-ss', start_time, '-to', end_time, '-i', video_local_path, final_file], stderr=sp.NULL, stdout=sp.NULL)`. – kesh Feb 27 '22 at 20:32
  • If I do the way you are saying, will I be relying on the ffmpeg installed by the system or it really makes no difference between doing this through Python? – João Victor Feb 28 '22 at 16:23

1 Answers1

1

There are a couple ways to work around this behavior.

1. Momentarily redirect stdout/stderr

See this post

2. Use FFmpeg directly

moviepy appears to be depending on imageio-ffmpeg for its FFmpeg support, and imageio-ffmpeg gets the FFmpeg path from IMAGEIO_FFMPEG_EXE environmental path downloads FFmpeg executables when the package is installed. So, you should be able to do the following


import imageio_ffmpeg
import subprocess as sp

ffmpeg_path = imageio_ffmpeg.get_ffmpeg_exe()

sp.run([ffmpeg_path, '-ss', start_time, '-to', end_time, '-i', video_local_path, 
       final_file, '-y'], stderr=sp.NULL, stdout=sp.NULL)

Now, if you are using moviepy only for this operation (and other FFmpeg operations), you can drop it altogether and set ffmpeg_path to the FFmpeg path. and just download the FFmpeg binaries via ffmpeg-downloader or static-ffmpeg package. The former is a better choice if you use FFmpeg in multiple venvs (as it saves FFmpeg files in an OS designated user data area, so it only downloads once). The latter is 100% automatic downloading approach similar to imageio-ffmpeg but with no frills.

[edit: imageio-ffmpeg does not download FFmpeg on its own, so you should already have the FFmpeg executables on your system.]

kesh
  • 4,515
  • 2
  • 12
  • 20
  • Your response actually solved it for me. The only adjustment I made was to change the ffmpeg command a bit since I still got errors regarding the NULL attribute for stderr and stdout. In any case, the final command is below and it works perfectly: ```ffmpeg_path = imageio_ffmpeg.get_ffmpeg_exe() sp.call([ffmpeg_path, '-loglevel', 'quiet', '-ss', start_time, '-to', end_time, '-i', video_local_path, '-c', 'copy', f"{final_file}.mp4"]) ``` Thank you! – João Victor Mar 01 '22 at 13:13