2017-07-11 132 views
1

我有一个应该记录视频的Python 3代码。不幸的是,我不希望它在.h264中,我需要它转换为.mp4。使用其他StackOverflow线程作为模板(特别是this之一),我认为最简单的方法是使用subprocess.PopenMP4Box -add filename.h264 filename.mp4插入终端,并让它自动执行。不幸的是,Python脚本没有做任何事情,我也没有收到任何错误消息,所以我不知道发生了什么问题。 .h264文件出现在我想要的文件夹中,如果手动将命令放入终端,.mp4出现,但是当我让它运行时什么也不会发生。剧本的其余部分就像一个魅力。该代码是在这里:(python 3)从.h264到.mp4的自动转换

#!/usr/bin/python 

from gpiozero import MotionSensor 
from gpiozero import Motor 
from picamera import PiCamera 
import subprocess 
import os.path 
import shlex 
import datetime as dt 
from time import sleep 

camera = PiCamera() 
pir = MotionSensor(4, 1, 100, .6, False) 
motor = Motor(3,14) #first number is forwards, second is backwards 
startupTime = 1 
recordingTime = 1 
collectionTime = 3 
resetTime = 30 



while True: 
    sleep(startupTime) #delay a bit so installation can take place 

    #wait for motion, then move the motor back and forth 
    pir.wait_for_motion() 
    print("Motion Detected") 
    #moves motor forward for 3 seconds at 25% speed 
    motor.forward(.25) 
    print("Strip Extending") 
    sleep(3) 
    motor.stop() 
    #leaves strip out for given amount of time 
    print("Collecting Sample") 
    sleep(collectionTime) 
    #moves motor backward for 3 seconds at 50% speed 
    motor.backward(.5) 
    print("Strip Retracting") 
    sleep(3) 
    motor.stop() 

    #Prep file for correct saving 
    filename = dt.datetime.now().strftime("%Y-%m-%d_%H.%M.%S.h264") #saves file as a date 
    save_path= "/home/pi/ANALYSIS" 
    completed_video= os.path.join(save_path, filename) 

    #Start recording 
    camera.start_recording(completed_video) #starts recording and saves it as filename 
    print("Camera Recording") 
    camera.annotate_text = dt.datetime . now() . strftime("%Y-%m-%d_%H.%M.%S") 
    start=dt.datetime.now() 

    #Keep recording until time runs out, annotate to make sure we have reference frame 
    while (dt.datetime.now() - start).seconds < recordingTime: 
     camera.annotate_text = dt.datetime.now(). strftime("%Y-%m-%d_%H.%M.%S") 
     camera.wait_recording(.2) 
    camera.stop_recording() 

    #Conversion to usable file format 
    print("Camera finished recording... Beginning Analysis") 
    from subprocess import CalledProcessError 
    command = shlex.split("MP4Box -add {} {}.mp4".format(completed_video, os.path.splitext(filename)[0])) 
    try: 
     output = subprocess.check_output(command, stderr=subprocess.STDOUT, shell=True) 
    except CalledProcessError as e: 
     print('FAIL:\ncmd:{}\noutput:{}'.format(e.cmd, e.output)) 

    #starts detecting again after given time 
    sleep(resetTime) 
    print("Ready for next sample") 

>Traceback (most recent call last): 
> 
    File "/home/pi/Detector.py", line 62, in <module> output = 
     subprocess.check_output(command, stderr=subprocess.STDOUT) 
    File "/usr/lib/python3.4/subprocess.py", line 620, in check_output raise 
     CalledProcessError(retcode, process.args, output=output) 
     subprocess.CalledProcessError: 
     Command '['MP4Box', '-add', '2017-07-11_15.34.49.h264.h264', '2017-07-11_15.34.49.h264.mp4']' 
> 
Returned non-zero exit status 1" 

回答

1

Comment: 2017-07-11_15.34.49.h264.h264

你的文件路径/文件名是错误的,做以下修改:

Note: The .mp4 is saved in to the Current Directory the Subprocess is executed,
the Directory the Python Script is started. This could be different as the .h264 are saved!
Think about to change that also to be a absolute Path.

command = "MP4Box -add {} {}.mp4".format(completed_video, os.path.splitext(filename)[0]) 
try: 
    output = subprocess.check_output(command, stderr=subprocess.STDOUT, shell=True) 
except subprocess.CalledProcessError as e: 
    print('FAIL:\ncmd:{}\noutput:{}'.format(e.cmd, e.output)) 

os.remove(path, *, dir_fd=None)

Remove (delete) the file path.


Question: I don't get any error messages, so I don't know what's going wrong.

尝试以下操作:
要还的结果捕捉标准错误,使用stderr=subprocess.STDOUT

command = shlex.split("MP4Box -add {f}.h264 {f}.mp4".format(f=filename)) 
output = subprocess.check_output(command, stderr=subprocess.STDOUT) 
print(output) 

我假设你需要shell=True,你不给FULLPATH到MP4Box所以shell环境需要找到MP4Box

+0

它说: “回溯(最近通话最后一个): 文件 ”/home/pi/Detector.py“ 62行,在 输出= subprocess.check_output(命令,标准错误= subprocess.STDOUT) 文件” /usr/lib/python3.4/subprocess.py“,第620行,check_output raise CalledProcessError(retcode,process.args,output = output) subprocess.CalledProcessError:Command'['MP4Box',' - add' '2017-07-11_15.34.49.h264.h264','2017-07-11_15.34.49.h264.mp4']'返回非零退出状态1“我会在(f = filename )和逗号后? – NeonCop

+0

好吧,我用最新的错误和我更新的.py脚本编辑它。我也尝试了使用不同文件名的旧代码,所以.h264没有重复,但没有vail。 – NeonCop

+0

它说“没有模块名称CalledProcessError'”,我必须从另一个模块导入它吗? – NeonCop