Ejemplo n.º 1
0
    def rotate(self, path, sign):

        self.update(None)
        clip = VideoFileClip(path)
        clip.rotate(90 * sign)

        if path.endswith(('gif')):
            clip.write_gif(path)
        else:
            clip.write_videofile(path)

        clip.close()
Ejemplo n.º 2
0
def movie_reverse(vedio_path):
    """
    旋转角度播放
    :return:
    """
    clip = VideoFileClip(vedio_path, audio=False)
    clip_reverse = clip.rotate(180)  #旋转180度播放
    return clip_reverse
Ejemplo n.º 3
0
def load_clip(filename=None, obj=None, indir=None, ext=None):
    """
    load clip from filename or from yaml object with rotation info
    """
    if filename is not None:
        return VideoFileClip(filename)
    assert obj is not None and indir is not None and ext is not None
    clip = VideoFileClip(os.path.join(indir, obj['name'] + ext))
    if 'rotation' in obj:
        clip = clip.rotate(obj['rotation'])
    return clip
Ejemplo n.º 4
0
def part2(vedio_path, output_path):
    """
    堆叠
    :param vedio_path:
    :return:
    """
    print("part2 拼接中---------")
    clip = VideoFileClip(vedio_path, audio=False)
    clip2 = VideoFileClip(vedio_path, audio=False).add_mask().resize(0.5)

    final_clip = clip2.rotate(lambda t: 20 * t)
    final_clip = CompositeVideoClip(
        [clip, final_clip.set_pos("center").set_start(1)],
        bg_color=(255, 255, 255))

    final_clip.write_videofile(output_path, fps=10)
    final_clip.close()
    return output_path
Ejemplo n.º 5
0
def main(argv):
    inputFolder = None
    outputFolder = None
    videoFile = None
    videoEndFile = None
    imageFile = None
    imageEndFile = None
    imageTime = 3
    cutFromBegining = 0
    cutFromEnd = 0
    codec = None
    fixedLength = None
    watermarkImage = None
    watermarkWidth = 100
    watermarkDuration = None
    fadeDuration = 1
    blackTop = 0
    blackBottom = 0
    cutUnitsPx = True
    cutUnitsStartPx = True
    cutUnitsEndPx = True
    watermarkX = "right"
    watermarkY = "top"
    watermarkStarts = None
    codecSuffix = None
    params = None
    resizeWithIntro = False
    resizeWithOutro = False
    mirrorVideo = False
    rotateVideo = None

    print_short_licence()

    try:
        opts, args = getopt.getopt(argv, "humf:v:i:t:s:e:o:c:l:w:r:", [
            "help", "folder=", "output=", "video=", "video-end=", "image=",
            "image-end=", "image-time=", "cut-start=", "cut-end=", "licence",
            "watermark=", "black-top=", "black-bottom=", "cut-percent",
            "watermark-width=", "cut-percent-start", "cut-percent-end",
            "watermark-to-left", "watermark-to-bottom", "watermark-to-center",
            "watermark-duration=", "watermark-fade-duration=",
            "watermark-show-at=", "copy-codec=", "ffmpeg=", "suffix=",
            "resize-by-intro", "resize-by-outro", "mirror", "rotate="
        ])
    except getopt.GetoptError:
        print_help()
        sys.exit(2)

    for opt, arg in opts:
        if opt == '-h':
            print_help()
            sys.exit()

        if opt == '--licence':
            print_licence()
            sys.exit()
        elif opt in ("-f", "--folder"):
            inputFolder = arg
        elif opt in ("-o", "--output"):
            outputFolder = arg
        elif opt in ("-v", "--video"):
            videoFile = arg
        elif opt in ("-s", "--cut-start"):
            cutFromBegining = max(0, int(arg))
        elif opt in ("-e", "--cut-end"):
            cutFromEnd = max(0, int(arg))
        elif opt in ("-t", "--image-time"):
            imageTime = max(1, int(arg))
        elif opt in ("-i", "--image"):
            imageFile = arg
        elif opt in ("--image-end"):
            imageEndFile = arg
        elif opt in ("--video-end"):
            videoEndFile = arg
        elif opt in ("-c"):
            codec = arg
        elif opt in ("-l"):
            fixedLength = int(arg)
        elif opt in ("-w", "--watermark"):
            watermarkImage = arg
        elif opt in ("--watermark-to-left"):
            watermarkX = "left"
        elif opt in ("--watermark-to-center"):
            watermarkX = "center"
            watermarkY = "center"
        elif opt in ("--watermark-to-bottom"):
            watermarkY = "bottom"
        elif opt in ("--watermark-duration"):
            watermarkDuration = int(arg)
        elif opt in ("--watermark-fade-duration"):
            fadeDuration = int(arg)
        elif opt in ("--watermark-show-at"):
            watermarkStarts = [int(a) for a in arg.split(',')]
        elif opt in ("--black-top"):
            blackTop = int(arg)
        elif opt in ("--black-bottom"):
            blackBottom = int(arg)
        elif opt in ("-u", "--cut-percent"):
            cutUnitsPx = False
        elif opt in ("--watermark-width"):
            watermarkWidth = max(0, int(arg))
        elif opt in ("--cut-percent-start"):
            cutUnitsStartPx = False
        elif opt in ("--cut-percent-end"):
            cutUnitsEndPx = False
        elif opt in ("--copy-codec"):
            codec = 'copy'
            codecSuffix = arg
        elif opt in ("--suffix"):
            codecSuffix = arg
        elif opt in ("--ffmpeg"):
            params = arg.split(' ')
        elif opt in ("--resize-by-intro"):
            resizeWithIntro = True
        elif opt in ("--resize-by-outro"):
            resizeWithOutro = True
        elif opt in ("-m", "--mirror"):
            mirrorVideo = True
        elif opt in ("-r", "--rotate"):
            rotateVideo = int(arg)

    fileList = []
    ffvideoFile = None
    ffvideoEndFile = None
    ffimageFile = None
    ffimageEndFile = None
    ffwatermark = None

    if not outputFolder:
        print(
            'Output folder is not found, use -h or --help to show help message.'
        )
        sys.exit(2)

    if not os.path.isdir(outputFolder):
        os.makedirs(outputFolder)

    if (videoFile and not os.path.isfile(videoFile)) or (
            videoEndFile and not os.path.isfile(videoEndFile)):
        print(
            'Input video file does not exists, use -h or --help to show help message.'
        )
        sys.exit(2)

    if (imageFile and not os.path.isfile(imageFile)) or (
            imageEndFile and not os.path.isfile(imageEndFile)):
        print(
            'Input image file does not exists, use -h or --help to show help message.'
        )
        sys.exit(2)

    if (watermarkImage and not os.path.isfile(watermarkImage)):
        print(
            'Input watermark image does not exists, use -h or --help to show help message.'
        )
        sys.exit(2)

    if codec is not None and not codecs.has_key(codec):
        print('Unknown codec, use -h or --help to show help message.')
        sys.exit(2)
    elif codec is not None and codecSuffix is None:
        codecSuffix = codecs[codec]

    if resizeWithIntro and not videoFile:
        print(
            'No intro for resize found, use -h or --help to show help message.'
        )
        sys.exit(2)

    if resizeWithIntro and not videoEndFile:
        print(
            'No outro for resize found, use -h or --help to show help message.'
        )
        sys.exit(2)

    if (videoFile):
        ffvideoFile = VideoFileClip(videoFile)

    if (videoEndFile):
        ffvideoEndFile = VideoFileClip(videoEndFile)

    if (imageFile):
        ffimageFile = ImageClip(imageFile, duration=imageTime)

    if (imageEndFile):
        ffimageEndFile = ImageClip(imageEndFile, duration=imageTime)

    if (watermarkImage):
        ffwatermark = ImageClip(watermarkImage)

    try:
        fileList = os.listdir(inputFolder)
    except:
        print('Input folder not found, use -h or --help to show help message.')
        sys.exit(2)

    for filename in fileList:
        try:
            if (os.path.isfile(os.path.join(inputFolder, filename))):
                video = VideoFileClip(os.path.join(inputFolder, filename))

                if (resizeWithIntro and ffvideoFile):
                    video = video.resize(ffvideoFile.size)

                if (resizeWithOutro and ffvideoEndFile):
                    video = video.resize(ffvideoEndFile.size)

                beginingCut = cutFromBegining
                endingCut = -cutFromEnd if cutFromEnd > 0 else None

                if (not cutUnitsPx or not cutUnitsStartPx):
                    beginingCut = (cutFromBegining * video.duration) / 100

                if (not cutUnitsPx or not cutUnitsEndPx):
                    endingCut = -(cutFromEnd * video.duration
                                  ) / 100 if cutFromEnd > 0 else None

                video = video.subclip(beginingCut, endingCut)
                width, height = video.size

                if (mirrorVideo):
                    video = video.fx(vfx.mirror_x)

                if (rotateVideo is not None):
                    video = video.rotate(rotateVideo)

                # video = (video
                #    # .fx(vfx.mirror_x)
                #    # .rotate(1)
                #    # .fx(vfx.colorx, factor=1.4)
                #    # .fx(vfx.painting, saturation=1.6, black=0.01)
                # )

                if (fixedLength is not None):
                    video = video.subclip(0, fixedLength)

                if (blackTop > 0):
                    blackHeight = (blackTop * height) / 100

                    def top_filter(frame):
                        frame[0:blackHeight, 0:width] = 0
                        return frame

                    video = video.fl_image(top_filter)

                if (blackBottom > 0):
                    blackHeight = (blackBottom * height) / 100

                    def bottom_filter(frame):
                        frame[(height - blackHeight):height, 0:width] = 0
                        return frame

                    video = video.fl_image(bottom_filter)

                if (ffwatermark is not None):
                    wWidth = (watermarkWidth * width) / 100
                    duration = (watermarkDuration if watermarkDuration
                                is not None else video.duration)
                    logo = (ffwatermark.set_duration(duration).margin(
                        left=0, right=0, bottom=0, top=0,
                        opacity=0.6).resize(width=wWidth).set_pos(
                            (watermarkX, watermarkY)).crossfadein(
                                fadeDuration).crossfadeout(fadeDuration))

                    if (watermarkStarts is None):
                        video = CompositeVideoClip([video, logo])
                    else:
                        logos = []
                        for w in watermarkStarts:
                            w_start = int(w * video.duration / 100)
                            logos.append(
                                logo.set_start(w_start).set_duration(
                                    min(video.duration - w_start, duration)))

                        video = CompositeVideoClip([video] + logos)

                videos = []
                if (ffvideoFile):
                    videos.append(ffvideoFile.resize(video.size))

                if (ffimageFile):
                    videos.append(ffimageFile.resize(video.size))

                videos.append(video)

                if (ffvideoEndFile):
                    videos.append(ffvideoEndFile.resize(video.size))

                if (ffimageEndFile):
                    videos.append(ffimageEndFile.resize(video.size))

                result = concatenate_videoclips(videos)

                translated_filename = filename if codecSuffix is None else os.path.splitext(
                    filename)[0] + "." + codecSuffix
                result.write_videofile(os.path.join(outputFolder,
                                                    translated_filename),
                                       codec=codec,
                                       ffmpeg_params=params)
        except Exception as e:
            print('Error while transfering file: ', filename)
            print('Original Error: ', str(e))
Ejemplo n.º 6
0
from moviepy.editor import VideoFileClip

av_file_path = 'E:/LG/GitHub/AiEduPro/datasets/friends.mp4'
result_file_path = '../datasets/rotate90.mp4'

video = VideoFileClip(av_file_path)
video = video.rotate(-90)
video.write_videofile(result_file_path)
Ejemplo n.º 7
0
import sys
from pprint import pprint
from moviepy.editor import VideoFileClip, vfx
e = sys.exit

video = VideoFileClip(
    r'C:\Users\alex_\OneDrive\Desktop\Matsuev_Video_Flat_Curve\C0006.MP4')
out = video.rotate(90)
pprint(dir(vfx))
#e()
out.write_videofile(
    r'C:\Users\alex_\OneDrive\Desktop\Matsuev_Video_Flat_Curve\C0006_rotated.MP4'
)