由于工作需要,每天有大量的視頻需要壓縮轉(zhuǎn)碼
- 某一天為了更好的需求,中午也要去,這尼瑪 直接寫一個腳本給我做,就行了,好歹我們也學(xué)了點(diǎn)代碼之類的工具啥的,就是干
- 說干,我們就來先說說思路,首先是要在
規(guī)定的時間
溉跃,做事情( 運(yùn)行代碼壓縮視頻 )
#計算時間得到秒
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)
#根據(jù)輸入的參數(shù),返回一個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
#開始運(yùn)行
def start():
#1.得到當(dāng)前的詳細(xì)時間
currentTime = time.localtime()
#2.根據(jù)當(dāng)前的時間拿到想要的時間 為下午的一點(diǎn)鐘
wantTime = "%d-%d-%d 12:07:00" %(currentTime.tm_year, currentTime.tm_mon, currentTime.tm_mday)
#3.目標(biāo)執(zhí)行的時間
targetTime = time.strptime(wantTime, '%Y-%m-%d %X')
#4.離運(yùn)行時間的秒
waitTimeSecond = howManySecondsBefore(currentTime, targetTime)
#5.睡眠等到要執(zhí)行的時間
time.sleep(waitTimeSecond)
#6.睡nmb, 起來high
High()
- 然后怎么High了告抄,我們默認(rèn)是將視頻放在一個文件夾里面撰茎,路徑當(dāng)然是絕對的,但是為了以后運(yùn)用打洼,寫一個相對的也可以
#開始high
def High():
#.檢查有沒有視頻后綴為.mp4 ,搜索路徑
videoPath = GetDesktopPath() + "/視頻"
#得多所有視頻的路徑 這里得到的是一個元祖龄糊,并且第二個是一個字符串
tuple2 = GetFileWith(videoPath)
#拿到所有的路徑,并且是list
allVideoPath = tuple2[1].split("\n")
if len(allVideoPath) > 0:
#轉(zhuǎn)換
CompressionTranscoding(allVideoPath)
+廢話不多說, 直接上全部代碼
#coding=utf-8
import os
import sys
import subprocess
import commands
#時間
import time
import datetime
#根據(jù)一個路徑獲取路徑下面有多少個視頻路徑
def GetFileWith(path):
command = "find %s -name *.mp4" %(path)
#執(zhí)行shell 命令
allVideoPath = commands.getstatusoutput(command)
return allVideoPath
#得到當(dāng)前用戶的桌面路徑
def GetDesktopPath():
return os.path.join(os.path.expanduser("~"), 'Desktop')
#創(chuàng)建文件夾 返回文件的路徑
def createFolder():
currentTime = time.localtime()
#以當(dāng)前日期創(chuàng)建文件夾
folderName = "%d%d壓縮視頻" %(currentTime.tm_mon, currentTime.tm_mday)
folderPath = GetDesktopPath() + '/' + folderName
createCommand = "mkdir %s" %(folderPath)
commands.getstatusoutput(createCommand)
return folderPath
#轉(zhuǎn)換
def CompressionTranscoding(allVideoPath):
#創(chuàng)建文件夾,并得到路徑
compressionVideoFolderPath = createFolder()
#遍歷每個的路徑募疮,開始轉(zhuǎn)換
for singlePath in allVideoPath:
if type(singlePath) is str:
#這里就開始進(jìn)行轉(zhuǎn)換了
#拿到本身的文件名 -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)
#開啟一個進(jìn)程執(zhí)行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:
#轉(zhuǎn)換
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)
#根據(jù)輸入的參數(shù),返回一個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
#開始運(yùn)行
def start():
#1.得到當(dāng)前的詳細(xì)時間
currentTime = time.localtime()
#2.根據(jù)當(dāng)前的時間拿到想要的時間 為下午的一點(diǎn)鐘
wantTime = "%d-%d-%d 13:09:00" %(currentTime.tm_year, currentTime.tm_mon, currentTime.tm_mday)
#3.目標(biāo)執(zhí)行的時間
targetTime = time.strptime(wantTime, '%Y-%m-%d %X')
print targetTime
#4.離運(yùn)行時間的秒
runTimeSecond = howManySecondsBefore(currentTime, targetTime)
print runTimeSecond
#5.睡眠
time.sleep(runTimeSecond)
#6.睡nmb, 起來high
High()
if __name__ == "__main__":
start()
測試運(yùn)行
好了阿浓,到下午一點(diǎn)他嚷,它就自己去轉(zhuǎn)視頻,我就不用去了芭毙,可以睡個午覺啥的