調用外部程序的兩種方法:os.system和subprocess
python實現錄屏功能
# coding=utf8 import time,os # 輸出視頻文件 outputfile = 'd:/data/bandicam/tmp/' + time.strftime('%Y%m%d_%H%M%S', time.localtime()) + '.mp4' # 工具目錄 ffmpegDir = r'd:\data\bandicam\tmp\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([ffmpegDir]+settings) # 查看命令內容 print(recordingCmdLine) # 執行命令錄制視頻 # os.system(recordingCmdLine)