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()

测试运行

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,744评论 25 709
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,761评论 0 17
  • 张小北站在车水马龙的十字路口,有点恍惚,突然忘记了自己应该往那个方向走,他默默站在那里,感觉自己渺小的如同一只蚂蚁...
    冬夜阅读 229评论 0 1
  • 却说九幽飞魂,须臾间亦死了。吾已无敌也!大叫:众神亦有何术?还不扶我上宝座,抬我上尊台! 众神擎来八抬大轿,将我扶...
    圣人乙阅读 443评论 2 1
  • 做事要循序渐进才是王道,细水长流才是最好呢,饭要一口一口吃。 睡饱了才能有精力坚持。每天有进步 才是真的。 加油。
    刘韧阅读 277评论 0 0