像wget可以下載文件
ffmpeg可以切割、合並、轉換、錄制視頻
free命令可以查看linux內存使用信息
python提供了庫來調用外部程序、命令?》
最常見的兩種方法:
①os.system os庫里面的system參數
②subprocess subprocess庫 里面的對和函數
如:
import os
os.system('notepad')
print('記事本已關閉')
注意:調用外部程序沒有退出時,python程序會一直停在那里
對於有參數的命令,os.system()參數 直接把參數放在字符串中一起傳入即可
向記事本傳遞參數,打開python.txt文件?》
如:
import os
os.sys('notepad c:\python.txt')
=====================================================
python實現錄屏功能?》
import os
# 輸出視頻文件
import time
outputfile = 'C:\shipin' + time.strftime('%Y%m%d_%H%M%S', time.localtime()) + '.mp4'
# 工具目錄
ffmpeg = r'C:\FFmpeg\安裝包\ffmpeg-4.1-win64-static\bin\ffmpeg.exe'
settings = [
'-y -rtbufsize 100M -f gdigrab -framerate 10', # 幀率等
'-offset_x 1000 -offset_y 0 -video_size 640x480', # 錄制指定屏幕區域
'-draw_mouse 1 -i desktop -c:v libx264', # 視頻編碼格式
'-r 20 -preset medium -tune zerolatency -crf 35', # 視頻壓縮參數
'-pix_fmt yuv420p -fs 100M -movflags +faststart "%s"' % outputfile # 大小限制 等
]
# 將參數組合起來
recordingCmdLine = ' '.join([ffmpeg]+settings)
# 查看命令內容
print(recordingCmdLine)
# 執行命令錄制視頻
os.system(recordingCmdLine)
======================================================================================
# 返回值
# 不管windows還是linux,如果一個程序它的退出碼為0就是表示成功的話
# 如果我們只需要判斷調用是否成功,也就是是否為0
import os
import subprocess
# ret = os.system('ls')
# if ret == 0:
# print(ret)
# os.system提供了簡單的調用其他程序的功能
# 而python里面有另一個模塊subprocess,提供了更為強大的功能
# 使用subprocess最常見的目的就是檢查應用的輸出
# 因為os.system只能獲得返回碼,輸出到屏幕的內容並不能獲取
# 如果想獲取被調用的命令或工具輸出到終端的信息,並進行處理,可以使用subprocess庫里面的check_output函數
# 這個方法需要等到被動月供程序退出,才能返回
# shell=True 表示使用終端shell執行程序;參數encoding:指定哪種解碼方式,
# 不填,是缺省值None,需要我們自己去解碼
# ret = subprocess.check_output('dir', shell=True, encoding='gbk')
# print(ret)
# print(ret.decode('gbk'))# 如果我們需要被調用程序還沒退出時,就獲取其輸出的信息;# 或者在被調用程序運行的過程中,輸入一些信息給被調用程序
# 需要使用subprocess庫里面的Popen類
# Popen可以非阻塞式調用外部程序,也就是說,無需等待被調用程序結束。腳本的代碼可以繼續往下執行
from subprocess import PIPE,Popen
# popen = Popen(args='notepad c:\python.txt', shell=True)
# print('done')
# Popen同樣可以獲取外部程序的輸出
# stdout=PIPE 表示將被調用的程序的標准輸出信息獲取到管道文件中
# 要獲取程序的輸出信息,就要這樣指定。
popen = Popen('dir c:',
stdout=PIPE,
shell=True,
encoding='gbk')
output, err = popen.communicate()
print(output)
print('----------------------------------------------')
print(err)