Beispiel #1
0
def writeAvi(filename, images, duration=0.1, encoding='mpeg4',
             inputOptions='', outputOptions=''):
    """Export movie to a AVI file, which is encoded with the given
    encoding. Hint for Windows users: the 'msmpeg4v2' codec is
    natively supported on Windows.

    Images should be a list consisting of PIL images or numpy arrays.
    The latter should be between 0 and 255 for integer types, and
    between 0 and 1 for float types.

    Requires the "ffmpeg" application:
      * Most linux users can install using their package manager
      * There is a windows installer on the visvis website

    :param str filename: output filename
    :param images:
    :param float duration:
    :param str encoding: the encoding type
    :param inputOptions:
    :param outputOptions:
    """

    # Get fps
    try:
        fps = float(1.0/duration)
    except Exception:
        raise ValueError("Invalid duration parameter for writeAvi.")

    # Determine temp dir and create images
    tempDir = os.path.join(os.path.expanduser('~'), '.tempIms')
    images2ims.writeIms(os.path.join(tempDir, 'im*.png'), images)

    # Determine formatter
    N = len(images)
    formatter = '%04d'
    if N < 10:
        formatter = '%d'
    elif N < 100:
        formatter = '%02d'
    elif N < 1000:
        formatter = '%03d'

    # Compile command to create avi
    command = "ffmpeg -r %i %s " % (int(fps), inputOptions)
    command += "-i im%s.png " % (formatter,)
    command += "-g 1 -vcodec %s %s " % (encoding, outputOptions)
    command += "output.avi"

    # Run ffmpeg
    S = subprocess.Popen(command, shell=True, cwd=tempDir,
                         stdout=subprocess.PIPE, stderr=subprocess.PIPE)

    # Show what ffmpeg has to say
    outPut = S.stdout.read()

    if S.wait():
        # An error occurred, show
        print(outPut)
        print(S.stderr.read())
        # Clean up
        _cleanDir(tempDir)
        raise RuntimeError("Could not write avi.")
    else:
        # Copy avi
        shutil.copy(os.path.join(tempDir, 'output.avi'), filename)
        # Clean up
        _cleanDir(tempDir)
Beispiel #2
0
def writeAvi(filename, images, duration=0.1, encoding='mpeg4',
             inputOptions='', outputOptions=''):
    """Export movie to a AVI file, which is encoded with the given
    encoding. Hint for Windows users: the 'msmpeg4v2' codec is
    natively supported on Windows.

    Images should be a list consisting of PIL images or numpy arrays.
    The latter should be between 0 and 255 for integer types, and
    between 0 and 1 for float types.

    Requires the "ffmpeg" application:
      * Most linux users can install using their package manager
      * There is a windows installer on the visvis website

    :param str filename: output filename
    :param images:
    :param float duration:
    :param str encoding: the encoding type
    :param inputOptions:
    :param outputOptions:
    """

    # Get fps
    try:
        fps = float(1.0/duration)
    except Exception:
        raise ValueError("Invalid duration parameter for writeAvi.")

    # Determine temp dir and create images
    tempDir = os.path.join(os.path.expanduser('~'), '.tempIms')
    images2ims.writeIms(os.path.join(tempDir, 'im*.png'), images)

    # Determine formatter
    N = len(images)
    formatter = '%04d'
    if N < 10:
        formatter = '%d'
    elif N < 100:
        formatter = '%02d'
    elif N < 1000:
        formatter = '%03d'

    # Compile command to create avi
    command = "ffmpeg -r %i %s " % (int(fps), inputOptions)
    command += "-i im%s.png " % (formatter,)
    command += "-g 1 -vcodec %s %s " % (encoding, outputOptions)
    command += "output.avi"

    # Run ffmpeg
    S = subprocess.Popen(command, shell=True, cwd=tempDir,
                         stdout=subprocess.PIPE, stderr=subprocess.PIPE)

    # Show what ffmpeg has to say
    outPut = S.stdout.read()

    if S.wait():
        # An error occurred, show
        print(outPut)
        print(S.stderr.read())
        # Clean up
        _cleanDir(tempDir)
        raise RuntimeError("Could not write avi.")
    else:
        # Copy avi
        shutil.copy(os.path.join(tempDir, 'output.avi'), filename)
        # Clean up
        _cleanDir(tempDir)
Beispiel #3
0
def writeAvi(
    filename,
    images,
    duration=0.1,
    encoding="mpeg4",
    inputOptions="",
    outputOptions="",
    bg_task=False,
):
    """Export movie to a AVI file, which is encoded with the given
    encoding. Hint for Windows users: the 'msmpeg4v2' codec is
    natively supported on Windows.

    Images should be a list consisting of PIL images or numpy arrays.
    The latter should be between 0 and 255 for integer types, and
    between 0 and 1 for float types.

    Requires the "ffmpeg" application:
      * Most linux users can install using their package manager
      * There is a windows installer on the visvis website

    :param str filename: output filename
    :param images:
    :param float duration:
    :param str encoding: the encoding type
    :param inputOptions:
    :param outputOptions:
    :param bool bg_task: if thread background task, not raise but
    return error message

    :return str: error message
    """

    # Get fps
    try:
        fps = float(1.0 / duration)
    except Exception:
        raise ValueError(_("Invalid duration parameter for writeAvi."))

    # Determine temp dir and create images
    tempDir = os.path.join(os.path.expanduser("~"), ".tempIms")
    images2ims.writeIms(os.path.join(tempDir, "im*.png"), images)

    # Determine formatter
    N = len(images)
    formatter = "%04d"
    if N < 10:
        formatter = "%d"
    elif N < 100:
        formatter = "%02d"
    elif N < 1000:
        formatter = "%03d"

    # Compile command to create avi
    command = "ffmpeg -r %i %s " % (int(fps), inputOptions)
    command += "-i im%s.png " % (formatter, )
    command += "-g 1 -vcodec %s %s " % (encoding, outputOptions)
    command += "output.avi"

    # Run ffmpeg
    S = subprocess.Popen(command,
                         shell=True,
                         cwd=tempDir,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE)

    # Show what ffmpeg has to say
    outPut = S.stdout.read()

    if S.wait():
        # Clean up
        _cleanDir(tempDir)
        if bg_task:
            return (gscript.decode(outPut) + "\n" +
                    gscript.decode(S.stderr.read()) + "\n" +
                    _("Could not write avi."))
        else:
            # An error occurred, show
            print(gscript.decode(outPut))
            print(gscript.decode(S.stderr.read()))
            raise RuntimeError(_("Could not write avi."))
    else:
        try:
            # Copy avi
            shutil.copy(os.path.join(tempDir, "output.avi"), filename)
        except Exception as err:
            # Clean up
            _cleanDir(tempDir)
            if bg_task:
                return str(err)
            else:
                raise

        # Clean up
        _cleanDir(tempDir)