python视频压缩算法_Python 到时间自动压缩视频

由于工作需要,每天有大量的视频需要压缩转码

某一天为了更好的需求,中午也要去,这尼玛 直接写一个脚本给我做,就行了,好歹我们也学了点代码之类的工具啥的,就是干

说干,我们就来先说说思路,首先是要在规定的时间,做事情( 运行代码压缩视频 )

#计算时间得到秒

def howManySecondsBefore(now , atTime):

d1 = datetime.datetime(now.tm_year, now.tm_mon, now.tm_mday, now.tm_hour, now.tm_min, now.tm_sec)

#根据输入的参数,返回一个datetime对象

d2 = datetime.datetime(atTime.tm_year, atTime.tm_mon, atTime.tm_mday, atTime.tm_hour, atTime.tm_min, atTime.tm_sec)

second = (d2 - d1).seconds

return second

#开始运行

def start():

#1.得到当前的详细时间

currentTime = time.localtime()

#2.根据当前的时间拿到想要的时间 为下午的一点钟

wantTime = "%d-%d-%d 12:07:00" %(currentTime.tm_year, currentTime.tm_mon, currentTime.tm_mday)

#3.目标执行的时间

targetTime = time.strptime(wantTime, '%Y-%m-%d %X')

#4.离运行时间的秒

waitTimeSecond = howManySecondsBefore(currentTime, targetTime)

#5.睡眠等到要执行的时间

time.sleep(waitTimeSecond)

#6.睡nmb, 起来high

High()

然后怎么High了,我们默认是将视频放在一个文件夹里面,路径当然是绝对的,但是为了以后运用,写一个相对的也可以

#开始high

def High():

#.检查有没有视频后缀为.mp4 ,搜索路径

videoPath = GetDesktopPath() + "/视频"

#得多所有视频的路径 这里得到的是一个元祖,并且第二个是一个字符串

tuple2 = GetFileWith(videoPath)

#拿到所有的路径,并且是list

allVideoPath = tuple2[1].split("\n")

if len(allVideoPath) > 0:

#转换

CompressionTranscoding(allVideoPath)

+废话不多说, 直接上全部代码

#coding=utf-8

import os

import sys

import subprocess

import commands

#时间

import time

import datetime

#根据一个路径获取路径下面有多少个视频路径

def GetFileWith(path):

command = "find %s -name *.mp4" %(path)

#执行shell 命令

allVideoPath = commands.getstatusoutput(command)

return allVideoPath

#得到当前用户的桌面路径

def GetDesktopPath():

return os.path.join(os.path.expanduser("~"), 'Desktop')

#创建文件夹 返回文件的路径

def createFolder():

currentTime = time.localtime()

#以当前日期创建文件夹

folderName = "%d%d压缩视频" %(currentTime.tm_mon, currentTime.tm_mday)

folderPath = GetDesktopPath() + '/' + folderName

createCommand = "mkdir %s" %(folderPath)

commands.getstatusoutput(createCommand)

return folderPath

#转换

def CompressionTranscoding(allVideoPath):

#创建文件夹,并得到路径

compressionVideoFolderPath = createFolder()

#遍历每个的路径,开始转换

for singlePath in allVideoPath:

if type(singlePath) is str:

#这里就开始进行转换了

#拿到本身的文件名 -1 是的到list的最后一个元素

videoName = singlePath.split("/")[-1]

# print videoName

#拼接压缩路径

videoCompressionPath = compressionVideoFolderPath + "/" + videoName

#shell 命令

command = "/usr/local/bin/ffmpeg -i %s -vcodec h264 -s 352*278 -r 6 %s " %(singlePath, videoCompressionPath)

#开启一个进程执行shell

p2 = subprocess.Popen(command,shell=True)

#等待

p2.wait()

#开始high

def High():

#.检查有没有视频后缀为.mp4 ,搜索路径

videoPath = GetDesktopPath() + "/视频"

#得多所有视频的路径 这里得到的是一个元祖,并且第二个是一个字符串

tuple2 = GetFileWith(videoPath)

#拿到所有的路径,并且是list

allVideoPath = tuple2[1].split("\n")

if len(allVideoPath) > 0:

#转换

CompressionTranscoding(allVideoPath)

#计算时间得到秒

def howManySecondsBefore(now , atTime):

d1 = datetime.datetime(now.tm_year, now.tm_mon, now.tm_mday, now.tm_hour, now.tm_min, now.tm_sec)

#根据输入的参数,返回一个datetime对象

d2 = datetime.datetime(atTime.tm_year, atTime.tm_mon, atTime.tm_mday, atTime.tm_hour, atTime.tm_min, atTime.tm_sec)

second = (d2 - d1).seconds

return second

#开始运行

def start():

#1.得到当前的详细时间

currentTime = time.localtime()

#2.根据当前的时间拿到想要的时间 为下午的一点钟

wantTime = "%d-%d-%d 13:09:00" %(currentTime.tm_year, currentTime.tm_mon, currentTime.tm_mday)

#3.目标执行的时间

targetTime = time.strptime(wantTime, '%Y-%m-%d %X')

print targetTime

#4.离运行时间的秒

runTimeSecond = howManySecondsBefore(currentTime, targetTime)

print runTimeSecond

#5.睡眠

time.sleep(runTimeSecond)

#6.睡nmb, 起来high

High()

if __name__ == "__main__":

start()

测试运行

好了,到下午一点,它就自己去转视频,我就不用去了,可以睡个午觉啥的

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值