栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python 对视频进行压缩

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python 对视频进行压缩

作用:对视频压缩,用多个进程
文件目录说明

deal_video 存放原始视频文件 MP4类型
deal_result 存放压缩完成的视频

参数调整: -crf 24 (0是无损画质,18到25还可以,多了就不清楚了)
参数越小,视频越大

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os
import sys
import time
import cv2
convert_cmd = 'ffmpeg -threads 4 -i deal_video\src_video_name -vcodec libx264 -preset fast -crf 24 -y -vf "scale=1920:-1" -acodec libmp3lame -ab 32k deal_result\dest_video_name'


def transform():
    root = os.getcwd()
    video_src = "deal_video\"
    i_num = 0
    root_to = os.path.join(root,video_src)
    for (dirname, subdir, subfile) in os.walk(root_to):
        for f in subfile:
            if f.find(".mp4") > -1:
                # video_names.append(f)
                if i_num <2:
                    i_num += 1
                else:
                    time.sleep(50)
                    i_num = 0
                start_time = time.time()
                roots = os.path.join(root_to, f)
                new_video_names = f.replace(' ','#')

                dstFile = os.path.join(root_to, new_video_names)
                os.rename(roots, dstFile)
                video_1 = cv2.VideoCapture(dstFile)
                video_width = int(video_1.get(cv2.CAP_PROP_frame_WIDTH))
                video_height = int(video_1.get(cv2.CAP_PROP_frame_HEIGHT))
                print(f)
                print(video_width)
                print(video_height)
                if not os.path.exists(roots):
                    continue
                create_time = os.path.getctime(roots)
                cmd = convert_cmd.replace('1920', str(video_width)).replace('-1', str(video_height)).replace('src_video_name', new_video_names).replace("dest_video_name",new_video_names)
                print(cmd)
                os.popen(cmd)
                end_time = time.time()
                print("Done in " + str(end_time - start_time) + " seconds")

if __name__ == '__main__':
    transform()
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/344599.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号