def ffmpeg_extract_audio(inputfile, output, bitrate=3000, fps=44100): """ extract the sound from a video file and save it in ``output`` """ cmd = [ get_setting("FFMPEG_BINARY"), "-y", "-i", inputfile, "-ab", "%dk" % bitrate, "-ar", "%d" % fps, output ] subprocess_call(cmd)
def ffmpeg_resize(video, output, size): """ resizes ``video`` to new size ``size`` and write the result in file ``output``. """ cmd = [ get_setting("FFMPEG_BINARY"), "-i", video, "-vf", "scale=%d:%d" % (res[0], res[1]), output ] subprocess_call(cmd)
def ffmpeg_merge_video_audio(video, audio, output, vcodec='copy', acodec='copy', ffmpeg_output=False, verbose=True): """ merges video file ``video`` and audio file ``audio`` into one movie file ``output``. """ cmd = [ get_setting("FFMPEG_BINARY"), "-y", "-i", audio, "-i", video, "-vcodec", vcodec, "-acodec", acodec, output ] subprocess_call(cmd, verbose=verbose)
def ffmpeg_extract_subclip(filename, t1, t2, targetname=None): """ makes a new video file playing video file ``filename`` between the times ``t1`` and ``t2``. """ name, ext = os.path.splitext(filename) if not targetname: T1, T2 = [int(1000 * t) for t in [t1, t2]] targetname = name + "%sSUB%d_%d.%s" (name, T1, T2, ext) cmd = [ get_setting("FFMPEG_BINARY"), "-y", "-i", filename, "-ss", "%0.2f" % t1, "-t", "%0.2f" % (t2 - t1), "-vcodec", "copy", "-acodec", "copy", targetname ] subprocess_call(cmd)
def ffmpeg_movie_from_frames(filename, folder, fps, digits=6): """ Writes a movie out of the frames (picture files) in a folder. Almost deprecated. """ s = "%" + "%02d" % digits + "d.png" cmd = [ get_setting("FFMPEG_BINARY"), "-y", "-f", "image2", "-r", "%d" % fps, "-i", os.path.join(folder, folder) + '/' + s, "-b", "%dk" % bitrate, "-r", "%d" % self.fps, filename ] subprocess_call(cmd)
def download_webfile(url, filename, overwrite=False): """ Small utility to download the file at 'url' under name 'filename'. If url is a youtube video ID like z410eauCnH it will download the video using youtube-dl (install youtube-dl first !). If the filename already exists and overwrite=False, nothing will happen. """ if os.path.exists(filename) and not overwrite: return if '.' in url: urlretrieve(url, filename) else: try: subprocess_call(['youtube-dl', url, '-o', filename]) except OSError as e: raise OSError( e.message + '\n A possible reason is that youtube-dl' ' is not installed on your computer. Install it with ' ' "pip install youtube-dl"')
def write_gif_with_tempfiles(clip, filename, fps=None, program='ImageMagick', opt="OptimizeTransparency", fuzz=1, verbose=True, loop=0, dispose=True, colors=None, tempfiles=False): """ Write the VideoClip to a GIF file. Converts a VideoClip into an animated GIF using ImageMagick or ffmpeg. Does the same as write_gif (see this one for more docstring), but writes every frame to a file instead of passing them in the RAM. Useful on computers with little RAM. """ fileName, fileExtension = os.path.splitext(filename) tt = np.arange(0, clip.duration, 1.0 / fps) tempfiles = [] verbose_print( verbose, "\n[CartoonPy] Building file %s\n" % filename + 40 * "-" + "\n") verbose_print(verbose, "[CartoonPy] Generating GIF frames...\n") total = int(clip.duration * fps) + 1 for i, t in tqdm(enumerate(tt), total=total): name = "%s_GIFTEMP%04d.png" % (fileName, i + 1) tempfiles.append(name) clip.save_frame(name, t, withmask=True) delay = int(100.0 / fps) if program == "ImageMagick": verbose_print(verbose, "[CartoonPy] Optimizing GIF with ImageMagick... ") cmd = [ get_setting("IMAGEMAGICK_BINARY"), '-delay', '%d' % delay, "-dispose", "%d" % (2 if dispose else 1), "-loop", "%d" % loop, "%s_GIFTEMP*.png" % fileName, "-coalesce", "-layers", "%s" % opt, "-fuzz", "%02d" % fuzz + "%", ] + (["-colors", "%d" % colors] if colors is not None else []) + [filename] elif program == "ffmpeg": cmd = [ get_setting("FFMPEG_BINARY"), '-y', '-f', 'image2', '-r', str(fps), '-i', fileName + '_GIFTEMP%04d.png', '-r', str(fps), filename ] try: subprocess_call(cmd, verbose=verbose) verbose_print(verbose, "[CartoonPy] GIF %s is ready." % filename) except (IOError, OSError) as err: error = ("CartoonPy Error: creation of %s failed because " "of the following error:\n\n%s.\n\n." % (filename, str(err))) if program == "ImageMagick": error = error + ( "This error can be due to the fact that " "ImageMagick is not installed on your computer, or " "(for Windows users) that you didn't specify the " "path to the ImageMagick binary in file conf.py.") raise IOError(error) for f in tempfiles: os.remove(f)