Example #1
0
def add_voice(video_path, term):
    videoclip = VideoFileClip(video_path)
    audioclip = AudioFileClip(speech(term))
    new_audioclip = CompositeAudioClip([videoclip.audio, audioclip])
    videoclip.audio = new_audioclip
    videoclip.write_videofile(video_path)
    os.remove('/home/benjamim/PycharmProjects/Nyte/ImagesAndSounds/' + term[0:3] + '.mp3')
def plot(audio_path, plot_path, prediction, sample_time=None, fps=30):
    render_animation(fps, output='temp.mp4', azim=75, prediction=prediction)
    if sample_time != None:
        audioclip = AudioFileClip(audio_path,
                                  fps=44100).subclip(sample_time[0],
                                                     sample_time[1])
    else:
        audioclip = AudioFileClip(audio_path, fps=44100)
    videoclip = VideoFileClip('temp.mp4')
    videoclip.audio = audioclip
    videoclip.write_videofile(plot_path, fps=fps)
    os.remove('temp.mp4')
Example #3
0
def test_issue_334():
    # NOTE: this is horrible. Any simpler version ?
    last_move = None
    last_move1 = None

    lis = [
        (0.0, 113, 167, 47),
        (0.32, 138, 159, 47),
        (0.44, 152, 144, 47),
        (0.48, 193, 148, 47),
        (0.6, 193, 148, 47),
        (0.76, 205, 138, 55),
        (0.88, 204, 121, 63),
        (0.92, 190, 31, 127),
        (1.2, 183, 59, 127),
        (1.4, 137, 22, 127),
        (1.52, 137, 22, 127),
        (1.72, 129, 67, 127),
        (1.88, 123, 69, 127),
        (2.04, 131, 123, 63),
        (2.24, 130, 148, 63),
        (2.48, 130, 148, 63),
        (2.8, 138, 180, 63),
        (3.0, 138, 180, 63),
        (3.2, 146, 192, 63),
        (3.28, 105, 91, 151),
        (3.44, 105, 91, 151),
        (3.72, 11, 48, 151),
        (3.96, 5, 78, 151),
        (4.32, 4, 134, 1),
        (4.6, 149, 184, 48),
        (4.8, 145, 188, 48),
        (5.0, 154, 217, 48),
        (5.08, 163, 199, 48),
        (5.2, 163, 199, 48),
        (5.32, 164, 187, 48),
        (5.48, 163, 200, 48),
        (5.76, 163, 200, 48),
        (5.96, 173, 199, 48),
        (6.0, 133, 172, 48),
        (6.04, 128, 165, 48),
        (6.28, 128, 165, 48),
        (6.4, 129, 180, 48),
        (6.52, 133, 166, 48),
        (6.64, 133, 166, 48),
        (6.88, 144, 183, 48),
        (7.0, 153, 174, 48),
        (7.16, 153, 174, 48),
        (7.24, 153, 174, 48),
        (7.28, 253, 65, 104),
        (7.64, 253, 65, 104),
        (7.8, 279, 116, 80),
        (8.0, 290, 105, 80),
        (8.24, 288, 124, 80),
        (8.44, 243, 102, 80),
        (8.56, 243, 102, 80),
        (8.8, 202, 107, 80),
        (8.84, 164, 27, 104),
        (9.0, 164, 27, 104),
        (9.12, 121, 9, 104),
        (9.28, 77, 33, 104),
        (9.32, 52, 23, 104),
        (9.48, 52, 23, 104),
        (9.64, 33, 46, 104),
        (9.8, 93, 49, 104),
        (9.92, 93, 49, 104),
        (10.16, 173, 19, 104),
        (10.2, 226, 173, 48),
        (10.36, 226, 173, 48),
        (10.48, 211, 172, 48),
        (10.64, 208, 162, 48),
        (10.92, 220, 171, 48),
    ]

    lis1 = [
        (0.0, 113, 167, 47),
        (0.32, 138, 159, 47),
        (0.44, 152, 144, 47),
        (0.48, 193, 148, 47),
        (0.6, 193, 148, 47),
        (0.76, 205, 138, 55),
        (0.88, 204, 121, 63),
        (0.92, 190, 31, 127),
        (1.2, 183, 59, 127),
        (1.4, 137, 22, 127),
        (1.52, 137, 22, 127),
        (1.72, 129, 67, 127),
        (1.88, 123, 69, 127),
        (2.04, 131, 123, 63),
        (2.24, 130, 148, 63),
        (2.48, 130, 148, 63),
        (2.8, 138, 180, 63),
        (3.0, 138, 180, 63),
        (3.2, 146, 192, 63),
        (3.28, 105, 91, 151),
        (3.44, 105, 91, 151),
        (3.72, 11, 48, 151),
        (3.96, 5, 78, 151),
        (4.32, 4, 134, 1),
        (4.6, 149, 184, 48),
        (4.8, 145, 188, 48),
        (5.0, 154, 217, 48),
        (5.08, 163, 199, 48),
        (5.2, 163, 199, 48),
        (5.32, 164, 187, 48),
        (5.48, 163, 200, 48),
        (5.76, 163, 200, 48),
        (5.96, 173, 199, 48),
        (6.0, 133, 172, 48),
        (6.04, 128, 165, 48),
        (6.28, 128, 165, 48),
        (6.4, 129, 180, 48),
        (6.52, 133, 166, 48),
        (6.64, 133, 166, 48),
        (6.88, 144, 183, 48),
        (7.0, 153, 174, 48),
        (7.16, 153, 174, 48),
        (7.24, 153, 174, 48),
        (7.28, 253, 65, 104),
        (7.64, 253, 65, 104),
        (7.8, 279, 116, 80),
        (8.0, 290, 105, 80),
        (8.24, 288, 124, 80),
        (8.44, 243, 102, 80),
        (8.56, 243, 102, 80),
        (8.8, 202, 107, 80),
        (8.84, 164, 27, 104),
        (9.0, 164, 27, 104),
        (9.12, 121, 9, 104),
        (9.28, 77, 33, 104),
        (9.32, 52, 23, 104),
        (9.48, 52, 23, 104),
        (9.64, 33, 46, 104),
        (9.8, 93, 49, 104),
        (9.92, 93, 49, 104),
        (10.16, 173, 19, 104),
        (10.2, 226, 173, 48),
        (10.36, 226, 173, 48),
        (10.48, 211, 172, 48),
        (10.64, 208, 162, 48),
        (10.92, 220, 171, 48),
    ]

    def posi(t):
        global last_move
        if len(lis) == 0:
            return (last_move[1], last_move[2])
        if t >= lis[0][0]:
            last_move = item = lis.pop(0)
            return (item[1], item[2])
        else:
            if len(lis) > 0:
                dura = lis[0][0] - last_move[0]
                now = t - last_move[0]
                w = (lis[0][1] - last_move[1]) * (now / dura)
                h = (lis[0][2] - last_move[2]) * (now / dura)
                # print t, last_move[1] + w, last_move[2] + h
                return (last_move[1] + w, last_move[2] + h)
            return (last_move[1], last_move[2])

    def size(t):
        global last_move1
        if len(lis1) == 0:
            return (last_move1[3], last_move1[3] * 1.33)
        if t >= lis1[0][0]:
            last_move1 = item = lis1.pop(0)
            return (item[3], item[3] * 1.33)
        else:
            if len(lis) > 0:
                dura = lis1[0][0] - last_move1[0]
                now = t - last_move1[0]
                s = (lis1[0][3] - last_move1[3]) * (now / dura)
                nsw = last_move1[3] + s
                nsh = nsw * 1.33
                # print t, nsw, nsh
                return (nsw, nsh)
            return (last_move1[3], last_move1[3] * 1.33)

    avatar = VideoFileClip("media/big_buck_bunny_432_433.webm", has_mask=True)
    avatar.audio = None
    maskclip = ImageClip("media/afterimage.png",
                         is_mask=True,
                         transparent=True)
    avatar.with_mask(maskclip)  # must set maskclip here..
    concatenated = concatenate_videoclips([avatar] * 3)

    tt = VideoFileClip("media/big_buck_bunny_0_30.webm").subclip(0, 3)
    # TODO: Setting mask here does not work:
    # .with_mask(maskclip).resize(size)])
    final = CompositeVideoClip(
        [tt, concatenated.with_position(posi).fx(resize, size)])
    final.duration = tt.duration
    final.write_videofile(os.path.join(TMP_DIR, "issue_334.mp4"), fps=10)
## 새로만들 동영상의 정보 ##
pathOut = "C:/Users/forea/PycharmProjects/untitled1/test.mp4"  # output 경로
fps = 30  # 프레임
lent = cnt  # 사진의 수
frame_array = []  #사진 정보가 담길 리스트

## 사진 정보를 읽어와 frame_append에 저장 ##
for idx in range(0, lent):
    img = cv2.imread("C:/Users/forea/test/" + str(idx) + ".png")
    height, width, layers = img.shape  # img를 읽어서 H,W,L을 가져오는데 사실 채널은 쓸모가 없다..
    size = (width, height)  # 사이즈
    frame_array.append(img)
out = cv2.VideoWriter(pathOut, cv2.VideoWriter_fourcc(*'DIVX'), fps,
                      size)  # out은 결과물 VideoWriter("경로", 확장자, 프레임, 크기)

## 사진 -> 동영상 ##
for i in range(len(frame_array)):  # out에 사진들을 쓰는 반복문
    # writing to a image array
    out.write(frame_array[i])
    print(str(i) + "번째 사진을 쓰는 중")
out.release()

## 위에서 만든 동영상 + 소리 ###
videoclip = VideoFileClip(
    "C:/Users/forea/PycharmProjects/untitled1/test.mp4")  # 위에서 새로 만든 Video
audioclip = AudioFileClip(
    "C:/Users/forea/PycharmProjects/untitled1/audio.mp3")  # 맨 처음에 추출한 소리

videoclip.audio = audioclip  # 소리를 새로 만든 Video에 합성
videoclip.write_videofile("new video.mp4")  # Output
Example #5
0
def cartoonize_video(path, is_high=False):
    global final_output_path, output_video, output_file
    # code for video
    #start_time = time.time()
    #Get the input video through VideoFileClip
    input_video = VideoFileClip(path)
    #Extract the audio file from this video to write it with the modified video
    audio_file = input_video.audio
    #we create output_file first to put in it the video with the modification but without audio in the file named output
    #this file is just temporary, we will remove it in the end
    output_file = os.getcwd() + "/output.mp4"
    # Checks and deletes the output file
    if os.path.isfile(output_file):
        os.remove(output_file)
    # Getting the frames from the video
    cap = cv2.VideoCapture(path)

    # Get current width of frame
    frame_width = int(cap.get(3))
    #print(frame_width)
    # Get current height of frame
    frame_height = int(cap.get(4))
    #print(frame_height)
    #get the frame per second for every video
    fps = cap.get(cv2.CAP_PROP_FPS)
    #print(fps)
    # Define the codec and create VideoWriter object
    #Another formats for foucc MJPG, DIVX, XVID
    #we define fourcc with mp4v for mp4 videos
    fourcc = cv2.VideoWriter_fourcc(*'mp4v')
    #we start creating the output_video,using its path,fourcc for encoding the video, frame per second, the width & height
    out = cv2.VideoWriter(output_file, fourcc, fps,
                          (frame_width, frame_height))
    while (cap.isOpened()):
        #read frame by frame
        ret, frame = cap.read()
        if ret == True:
            # cartoon the frame
            #if you want the video with better quality
            if is_high:
                cartoon = cartoonize_with_K_video_means(frame)
            else:
                cartoon = cartoonize_video_frame(frame)
            #if is_high
            #write cartoonized frame to video
            out.write(cartoon)
            # cv2.imshow('frame',cartoon)
        else:
            break
        k = cv2.waitKey(5) & 0xFF
        if k == 27:
            break

    # When everything done, release the capture
    cap.release()
    out.release()
    #Create the final output video by reading the cartoonized video
    output_video = VideoFileClip(output_file)
    #then add the audio file from the original video
    output_video.audio = audio_file
    #we get the name of video in file_name
    file_name = (path.split('/'))[-1]
    #then we get the extension to make the output video the same extension and to its name _cartoon
    tmp = os.path.splitext(file_name)
    name = tmp[0] + "_cartoon" + tmp[1]
    name = os.path.join(os.getcwd(), name)
    final_output_path = name
    #then we write the final video to the same path where the program run
    output_video.write_videofile(final_output_path)
    #print("Execution Time = %s second " % (time.time() - start_time))
    return final_output_path