简体   繁体   English

是他们从 python 在 linux 中运行多进程终端命令的方法

[英]is their a way to run multiprocces of terminal command in linux from python

I tried to run this code in order to change the file type of a folder of.MOV files to.mp4 files I am trying to do this in multiprocessing because the file type changing is really CPU consuming.:我尝试运行此代码以将 .MOV 文件的文件夹的文件类型更改为 .mp4 文件我试图在多处理中执行此操作,因为文件类型的更改确实很消耗 CPU。:

the code编码

def change_video_file_type_for_multy_proccecing(file_path):
   os.system('ffmpeg -i ' + file_path+ ' -vcodec h264 -acodec mp2 -q:v 0 ' + file_path.replace(".MOV",".mp4"))

input_for_after_trim = "/home/oem/Documents/2021/MOV/after_trim"
output_for_after_trim = "/home/oem/Documents/2021/mp4/after_trim"
file_path_list = []
for filename in os.listdir(input_for_after_trim):
   file_path_list.append(input_for_after_trim+"/"+filename)
with concurrent.futures.ProcessPoolExecutor() as executor:
   # other way
   results = executor.map(change_video_file_type_for_multy_proccecing,file_path_list)  # will return the result of the function by calling order

the problem:问题:

but when I run it like that but somehow some files are created but stop in early-stage and it saves a 23-byte file means the command stoped at the beginning and some other files change their file type very good.但是当我这样运行它但不知何故创建了一些文件但在早期阶段停止并且它保存了一个 23 字节的文件意味着命令在开始时停止并且其他一些文件很好地改变了它们的文件类型。

it is not because a problem in the terminal command because when i did it synchronize (without multithreading it went fine but really slow)这不是因为终端命令中的问题,因为当我同步时(没有多线程它运行良好但非常慢)

my computer details:我的电脑详情:

Linux Ubuntu 21.04 kernel 5.11.0-41-generic Linux Ubuntu 21.04 kernel 5.11.0-41-通用

Does this fix your issues?这能解决您的问题吗?

from multiprocessing import Pool
import os

def change_video_file_type_for_multy_proccecing(file_path):
   os.system('ffmpeg -i ' + file_path+ ' -vcodec h264 -acodec mp2 -q:v 0 ' + file_path.replace(".MOV",".mp4"))

input_for_after_trim = "/home/oem/Documents/2021/MOV/after_trim"
output_for_after_trim = "/home/oem/Documents/2021/mp4/after_trim"
file_path_list = []
for filename in os.listdir(input_for_after_trim):
   file_path_list.append(input_for_after_trim+"/"+filename)

num_processes = os.cpu_count() #just an example, set to what you want
with Pool(num_processes) as p:
   results = list(p.map(change_video_file_type_for_multy_proccecing,file_path_list))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM