不过大概30M的视频,最后只会升到5M

import ffmpy
import os 
import ffmpeg
import cv2
from tqdm import tqdm
import moviepy.editor as edr
import time

def export_audio_from_video(src_file, dst_file):
    """
    # 提取出来的格式为mp3
    :param src_file:
    :param dst_file:
    :return:
    """

    # 1. 读取目标文件
    video = edr.VideoFileClip(filename=src_file)

    # 2. 取出其中的音频数据结构
    video.audio.write_audiofile(filename=dst_file)

def export_video_from_video(video_path='E:/vdieo/C0467.MP4', out_path='E:/video_new/C0467.mp4'):  # 自定义输出后缀
    cap = cv2.VideoCapture(video_path)
    frame_cnt = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
    height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
    weight = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    fps = int(cap.get(cv2.CAP_PROP_FPS))

    size = (weight, height)
    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    out = cv2.VideoWriter(out_path, fourcc, fps, size)  # fourcc是编码格式,size是图片尺寸
    for n in tqdm(range(frame_cnt)):
        # print('视频合成进度:', n, frame_cnt)
        ret, frame = cap.read()
        # cv2.imshow('frame', frame)
        out.write(frame)
        if cv2.waitKey(10) & 0xFF == ord('q'):
            break

    cap.release()
    out.release()

if __name__ == "__main__":
    #输入的目录
    source = r"E:/vdieo/"
    #输出的目录
    output = r"E:/video_new/"
    output_video = r"E:/video_new_video/"
    output_audio = r"E:/video_new_audio/"
    list_1=os.listdir(source)
    for file in list_1:
        if file[-4:]!='.MP4':
           continue
        export_audio_from_video(source + file, output_audio + file.replace('MP4', 'mp3'))
        export_video_from_video(source + file, output_video + file)
        time.sleep(3)
        input_video = ffmpeg.input(output_video + file)
        input_audio = ffmpeg.input(output_audio + file.replace('MP4', 'mp3'))
        ffmpeg.concat(input_video, input_audio, v=1, a=1).output(output + file).run()
        #ff = ffmpy.FFmpeg(
        #     'D:/Program Files (x86)/ffmpeg-master-latest-win64-gpl-shared/bin/ffmpeg.exe',
        #     inputs = {source_file: None},
        #     outputs = {sink_file: None})
        #ff.run()
分类: 未分类

0 条评论

发表回复

Avatar placeholder