Skip to content

Instantly share code, notes, and snippets.

@daimon99
Last active November 25, 2019 11:15
Show Gist options
  • Select an option

  • Save daimon99/342916bdf56ef621eb84db86ed0b82c5 to your computer and use it in GitHub Desktop.

Select an option

Save daimon99/342916bdf56ef621eb84db86ed0b82c5 to your computer and use it in GitHub Desktop.
subprocess 实时打印输出,并检测命令行的返回值
# coding: utf-8
from .celery import app
# 关注下这个库,比较方便写 linux 命令行工具
# https://docs.python.org/3/library/shlex.html
@app.task(bind=True, max_retries=2, acks_late=True)
def convert_video(self):
print('====== start =======')
import subprocess
import os
try:
cmd = ['ffmpeg','-i', 'zy 1.rmvb', '-c:v', 'libx265', 'zy 1.mp4', '-y']
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True, errors='ignore')
while p.poll() is None:
output = p.stdout.readline()
if output:
print(output.strip())
if p.poll() == 255:
msg = 'ctrl + c detected'
print(msg)
raise Exception(msg)
elif p.poll() == 1:
print('wrong file format')
elif p.poll() == 0:
print('====== end =======')
else:
print('error detected, ffmpeg returned with code: ', p.poll())
except Exception as e:
print('exception here, retry task next time.', e)
raise self.retry(exec=e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment