【问题标题】:(python 3) Auto-Conversion from .h264 to .mp4(python 3) 从 .h264 到 .mp4 的自动转换
【发布时间】:2017-12-15 20:28:59
【问题描述】:

我有一个用来录制视频的 Python 3 代码。不幸的是,我不希望它在 .h264 中,我需要将其转换为 .mp4。使用其他 StackOverflow 线程作为模板(特别是 this 一个),我认为最简单的方法是使用 subprocess.PopenMP4Box -add filename.h264 filename.mp4 插入终端并让它自动为我完成。不幸的是,Python 脚本什么也没做,我也没有收到任何错误消息,所以我不知道出了什么问题。 .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"

【问题讨论】:

    标签: python terminal raspberry-pi command raspbian


    【解决方案1】:

    评论:2017-07-11_15.34.49.h264.h264

    你的文件路径/文件名错误,改成如下:

    注意.mp4 保存在子进程执行的当前目录中,
    Python 脚本启动的目录。这可能会有所不同,因为 .h264 已保存!
    考虑将其更改为绝对路径。

    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)

    移除(删除)文件路径。


    问题:我没有收到任何错误消息,所以我不知道出了什么问题。

    尝试以下方法:
    要同时捕获结果中的标准错误,请使用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,因为您没有提供到 MP4Box 的完整路径,因此需要 Shell 环境才能找到 MP4Box

    【讨论】:

    • 它说“回溯(最近一次调用最后一次):文件“/home/pi/Detector.py”,第 62 行,在 output = subprocess.check_output(command, stderr=subprocess. STDOUT)文件“/usr/lib/python3.4/subprocess.py”,第 620 行,在 check_output 中引发 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) 和逗号后面?
    • 好的,我使用最新的错误和更新的 .py 脚本对其进行了编辑。我还尝试了使用不同文件名的旧代码,因此 .h264 没有重复,但没有成功。
    • 它说“没有模块名称'CalledProcessError'”,我必须从另一个模块导入它吗?
    • 糟糕!对不起这是我的错。我真的很感谢你的帮助,你太棒了!我更新了最新的代码和错误,我想现在已经很接近了。
    • 是的!!!!有效!非常感谢,我已经疯了一个星期了。有一个额外的括号,但除此之外你的代码是黄金。
    猜你喜欢
    • 1970-01-01
    • 2013-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 2017-05-14
    • 2010-11-06
    相关资源
    最近更新 更多