Coding-python使用命令行命令

示例,command变量是命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import subprocess
import os
import time
# 指定视频数据的源目录
video_source_dir = "videos_source"

# 输出目录
output_dir = "api_out"

temp_dir = 'videos_temp'

api_dir = 'videos_apiout'

# 遍历视频数据目录中的所有文件
for video_file in os.listdir(video_source_dir):
timestamp = int(time.time())
# 检查是否为视频文件(这里假设视频文件扩展名为.mp4)
if video_file.endswith(".mp4"):

video_path = os.path.join(video_source_dir, video_file)

# 构建要执行的命令
command = [
"python",
"main_yolo_dected_video.py",
"--source",
temp_dir+'/'+str(timestamp),
"--out_dir",
api_dir+'/'+str(timestamp),
"--video_path",
video_path
]

# 使用subprocess.run()执行命令
result = subprocess.run(command)

# 检查命令执行状态
if result.returncode == 0:
print(f"Processed {video_file} successfully.")
else:
print(f"Failed to process {video_file}. Error code: {result.returncode}")