MoviePy making pyqt5 progress bar

919 views Asked by At

Using moviepy module i want a callback function to display % audio export.

I have done that changes in module:

a) Call audio.write_audiofile("../disket box/mp3 files/"+title+".mp3",progress_function=self.update_progress_bar_local,verbose=True,logger=None) (adding progress_function parameter)

b)File Python38\Lib\site-packages\moviepy\audio\AudioClip.py line

def write_audiofile(self, filename, fps=None, nbytes=2, buffersize=2000, codec=None, bitrate=None, ffmpeg_params=None, write_logfile=False, verbose=True, logger='bar',progress_function=None):

c) Same file as b) Line 206 return ffmpeg_audiowrite(self, filename, fps, nbytes, buffersize, codec=codec, bitrate=bitrate, write_logfile=write_logfile, verbose=verbose, ffmpeg_params=ffmpeg_params, logger=logger,progress_function=progress_function)

d) File Python38\Lib\site-packages\moviepy\audio\io\ffmpeg_audiowriter.py lines 144 - end

def ffmpeg_audiowrite(clip, filename, fps, nbytes, buffersize,
                      codec='libvorbis', bitrate=None,
                      write_logfile=False, verbose=True,
                      ffmpeg_params=None, logger='bar',progress_function=None):
    """
    A function that wraps the FFMPEG_AudioWriter to write an AudioClip
    to a file.

    NOTE: verbose is deprecated.
    """

    if write_logfile:
        logfile = open(filename + ".log", 'w+')
    else:
        logfile = None
    logger = proglog.default_bar_logger(logger)
    logger(message="MoviePy - Writing audio in %s" % filename)
    writer = FFMPEG_AudioWriter(filename, fps, nbytes, clip.nchannels,
                                codec=codec, bitrate=bitrate,
                                logfile=logfile,
                                ffmpeg_params=ffmpeg_params)
    all_chunks = clip.iter_chunks(chunksize=buffersize,
                                  quantize=True,
                                  nbytes=nbytes, fps=fps,
                                  logger=logger)
    
    counter = 0
    total_chunks = "?????"
    for chunk in all_chunks:
        counter = counter + 1
        writer.write_frames(chunk)
        progress_function(str(counter),str(total_chunks))

    writer.close()

    if write_logfile:
        logfile.close()
    logger(message="MoviePy - Done.")

Ok the progress function works but i don't have an idea of how can i calculate total_chunks. I tried len(all_chunks) but it fails, because all_chunks is not a list (it's a generator).

Any advice of how can i calculate total_chunks would be usefull.

Thanks in advance, Chris Pappas

1

There are 1 answers

0
Chris P On BEST ANSWER

I modified \Python38\Lib\site-packages\moviepy\audio\io\AudioFileClip.py

I added a new method:

def len(self):
    return self.duration

Now Python38\Lib\site-packages\moviepy\audio\io\ffmpeg_audiowriter.py looks like:

import os
import subprocess as sp

import proglog

from moviepy.compat import DEVNULL
from moviepy.config import get_setting
from moviepy.decorators import requires_duration
import math

class FFMPEG_AudioWriter:
    """
    A class to write an AudioClip into an audio file.

    Parameters
    ------------

    filename
      Name of any video or audio file, like ``video.mp4`` or ``sound.wav`` etc.

    size
      Size (width,height) in pixels of the output video.

    fps_input
      Frames per second of the input audio (given by the AUdioClip being
      written down).

    codec
      Name of the ffmpeg codec to use for the output.

    bitrate:
      A string indicating the bitrate of the final video. Only
      relevant for codecs which accept a bitrate.

    """

    def __init__(self, filename, fps_input, nbytes=2,
                 nchannels=2, codec='libfdk_aac', bitrate=None,
                 input_video=None, logfile=None, ffmpeg_params=None):

        self.filename = filename
        self.codec = codec

        if logfile is None:
            logfile = sp.PIPE

        cmd = ([get_setting("FFMPEG_BINARY"), '-y',
                "-loglevel", "error" if logfile == sp.PIPE else "info",
                "-f", 's%dle' % (8*nbytes),
                "-acodec",'pcm_s%dle' % (8*nbytes),
                '-ar', "%d" % fps_input,
                '-ac', "%d" % nchannels,
                '-i', '-']
               + (['-vn'] if input_video is None else ["-i", input_video, '-vcodec', 'copy'])
               + ['-acodec', codec]
               + ['-ar', "%d" % fps_input]
               + ['-strict', '-2']  # needed to support codec 'aac'
               + (['-ab', bitrate] if (bitrate is not None) else [])
               + (ffmpeg_params if ffmpeg_params else [])
               + [filename])

        popen_params = {"stdout": DEVNULL,
                        "stderr": logfile,
                        "stdin": sp.PIPE}

        if os.name == "nt":
            popen_params["creationflags"] = 0x08000000

        self.proc = sp.Popen(cmd, **popen_params)

    def write_frames(self, frames_array):
        try:
            try:
                self.proc.stdin.write(frames_array.tobytes())
            except NameError:
                self.proc.stdin.write(frames_array.tostring())
        except IOError as err:
            ffmpeg_error = self.proc.stderr.read()
            error = (str(err) + ("\n\nMoviePy error: FFMPEG encountered "
                                 "the following error while writing file %s:" % self.filename
                                 + "\n\n" + str(ffmpeg_error)))

            if b"Unknown encoder" in ffmpeg_error:

                error = (error +
                         ("\n\nThe audio export failed because FFMPEG didn't "
                          "find the specified codec for audio encoding (%s). "
                          "Please install this codec or change the codec when "
                          "calling to_videofile or to_audiofile. For instance "
                          "for mp3:\n"
                          "   >>> to_videofile('myvid.mp4', audio_codec='libmp3lame')"
                          ) % (self.codec))

            elif b"incorrect codec parameters ?" in ffmpeg_error:

                error = (error +
                         ("\n\nThe audio export failed, possibly because the "
                          "codec specified for the video (%s) is not compatible"
                          " with the given extension (%s). Please specify a "
                          "valid 'codec' argument in to_videofile. This would "
                          "be 'libmp3lame' for mp3, 'libvorbis' for ogg...")
                         % (self.codec, self.ext))

            elif b"encoder setup failed" in ffmpeg_error:

                error = (error +
                         ("\n\nThe audio export failed, possily because the "
                          "bitrate you specified was two high or too low for "
                          "the video codec."))

            else:
                error = (error +
                         ("\n\nIn case it helps, make sure you are using a "
                          "recent version of FFMPEG (the versions in the "
                          "Ubuntu/Debian repos are deprecated)."))

            raise IOError(error)

    def close(self):
        if hasattr(self, 'proc') and self.proc:
            self.proc.stdin.close()
            self.proc.stdin = None
            if self.proc.stderr is not None:
                self.proc.stderr.close()
                self.proc.stdee = None
            # If this causes deadlocks, consider terminating instead.
            self.proc.wait()
            self.proc = None

    def __del__(self):
        # If the garbage collector comes, make sure the subprocess is terminated.
        self.close()

    # Support the Context Manager protocol, to ensure that resources are cleaned up.

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        self.close()


@requires_duration
def ffmpeg_audiowrite(clip, filename, fps, nbytes, buffersize,
                      codec='libvorbis', bitrate=None,
                      write_logfile=False, verbose=True,
                      ffmpeg_params=None, logger='bar',progress_function=None):
    """
    A function that wraps the FFMPEG_AudioWriter to write an AudioClip
    to a file.

    NOTE: verbose is deprecated.
    """

    if write_logfile:
        logfile = open(filename + ".log", 'w+')
    else:
        logfile = None
    logger = proglog.default_bar_logger(logger)
    logger(message="MoviePy - Writing audio in %s" % filename)
    writer = FFMPEG_AudioWriter(filename, fps, nbytes, clip.nchannels,
                                codec=codec, bitrate=bitrate,
                                logfile=logfile,
                                ffmpeg_params=ffmpeg_params)
    all_chunks = clip.iter_chunks(chunksize=buffersize,
                                  quantize=True,
                                  nbytes=nbytes, fps=fps,
                                  logger=logger)
    
    counter = 0
    total_chunks = math.ceil(fps*clip.len()/buffersize)
    print(clip.len())
    for chunk in all_chunks:
        counter = counter + 1
        writer.write_frames(chunk)
        progress_function(str(counter),str(total_chunks))

    writer.close()

    if write_logfile:
        logfile.close()
    logger(message="MoviePy - Done.")

and total_chunks is same as final counter!!