私はビデオを記録するはずのPython 3コードを持っています。残念ながら、私はそれが欲しいのではありません.h264、私はそれを変換する必要があります.mp4。他のStackOverflowスレッドをテンプレート(具体的にはthis)として使用すると、これを行う最も簡単な方法はsubprocess.Popen
を使用してMP4Box -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"
それは、 "トレースバック(最新の呼び出しの最後): ファイル "と言う" 出力= subprocess.check_output(コマンド、標準エラー出力= subprocess.STDOUT) ファイルで、ライン62、" /home/pi/Detector.py "['' MP4Box '、' -add '、'/'、'、 '、'、 '、'、 '、'、 '、'、 '、' '2017-07-11_15.34.49.h264.h264'、 '2017-07-11_15.34.49.h264.mp4'] '' 0以外の終了ステータス1を返しました。 )とカンマの後も? –
NeonCop
私は最新のエラーと更新された.pyスクリプトで編集しました。私はまた、.h264が繰り返されないように別のファイル名を使用して古いコードを試しましたが、ベイルにはしませんでした。 – NeonCop
「No module name 'CalledProcessError'」と表示されます。別のモジュールからモジュール名をインポートする必要がありますか? – NeonCop