项目实训(十)---对原始视频压缩处理

2021SC@SDUSC

目录

一.引言

二.代码流程

三.压缩效果


一.引言

后面场景划分的算法以及关键帧的算法执行时间比较长,为了缩短执行时间,我们进行了优化,决定使用压缩视频的方法,来加快算法处理流程。使用ffmpeg对用户上传的视频进行压缩。

二.代码流程

在后端接收到视频之后,对视频进行压缩,再将压缩结果存储到服务器。

 在上传后后端调用压缩算法的过程如下:


            //压缩
            try {

                String[] args1 = new String[]{"python", "G://demo//temp//compress.py", String.valueOf(name)};

                Process proc = Runtime.getRuntime().exec(args1);// 执行py文件

                BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));


                String line = null;

                while ((line = in.readLine()) != null) {

                    System.out.println(line);

                }

                in.close();
                proc.waitFor();

                boolean b=true;
                while(b) {
                    System.out.println("G://demo//temp//deal_result//" + name);
                    File file = new File("G://demo//temp//deal_result//" +id+"_"+ name);
                    File file1 = new File("G://demo//temp//deal_result//" +id+"_"+ name);
                    if (!file.exists() || !file.isFile()) {
                        System.out.println("文件不存在");
                    }else{                   
                            b=false;
                        }
                    }
                }
            } catch (IOException e) {

                e.printStackTrace();

            } catch (InterruptedException e) {

                e.printStackTrace();
            }

压缩方法:


# -*- coding: UTF-8 -*-

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

def transform(filename):
    root = os.getcwd()
    video_src = "deal_video\\"
    i_num = 0
    root_to = "G:\\demo\\temp\\del_video\\"

    for (dirname, subdir, subfile) in os.walk(root_to):
        for f in subfile:
            if f.find(filename) > -1:
                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(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)
                r=os.popen(cmd)
                end_time = time.time()
                print("Done in " + str(end_time - start_time) + " seconds")

存在问题:

这里有一个问题就是这个方法是异步执行的,就是java的代码已经执行结束了,但是命令行还是在对视频压缩中,这个时候如果不加判断对视频直接穿服务器,那么传的视频必然是错误的。

  r=os.popen(cmd)

解决方法:

这里使用的解决办法是用通过对文件重命名来实现的,如果文件还在压缩,那么对文件的重命名操作就不会成功,因为文件正在被占用,这个时候就可以利用这个特点来进行视频压缩处理是否结束的一个判断。

三.压缩效果

这是压缩前后视频大小的对比,视频压缩后体积几乎是原来的1/9。此时调用算法,速度比之前会快很多。

压缩后算法执行的时间

压缩后:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值