サーバーが POST 要求を受信し、指定された URL で OMXplayer プロセスを開始してビデオを再生する Raspberry Pi のプロジェクトを開発しています。これまでのところ、問題なくビデオの再生を開始できます。omxplayer
ただし、ビデオを再生/一時停止したり、前後にジャンプしたりできるように、プロセスと対話できるようにしたいと考えています。また、プロセスを強制終了できるようにしたいです。stdin.write()
これはプロセスを呼び出すことで実行できるはずですが、(適切なパス terminate()
をトリガーして)これらのメソッドを実行しようとすると、変数 video_process が play_video() に割り当てられていないことを示すエラーが表示され続けます。実行されます。GET
NoneType object has no attribute ...
video_process
の属性を作成するなど、これを機能させるために多くの方法を試しましConfigurationServer
たが、それらはすべて同じエラーを引き起こし、本当に明白なものが欠けているように感じます。私は他のSO投稿からサンプルコードを適応させようとしました:
from subprocess import Popen, PIPE
p = Popen(['omxplayer', filePath], stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True)
p.stdin.write(' ') # sends a space to the running process
p.stdin.flush() # if the above isn't enough, try adding a flush
しかし、何も機能しません。主なコードは以下です。
video_process = None
def play_video(url, orientation=0):
global video_process
p = subprocess.Popen(["omxplayer -o both --orientation {} --blank `youtube-dl -g -f best \"{}\"` &".format(orientation, url)], shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
video_process = p
def toggle_play_video():
video_process.stdin.write(' ')
video_process.stdin.flush()
def stop_video():
video_process.terminate()
video_process = None
class ConfigurationServer(http.server.BaseHTTPRequestHandler):
# when starting a video, the process is stored here so that commands (pause, play, arrows) can be sent to it when other requests are received.
def do_GET(self):
if self.path == '/togglePlayVideo':
self.send_response(200)
try:
toggle_play_video()
except Exception as e:
print(e)
return
elif self.path == '/jumpForwardsVideo':
self.send_response(200)
try:
self.video_process.stdin.write('^[[C')
self.video_process.stdin.flush()
except Exception as e:
print(e)
return
elif self.path == '/jumpBackwardsVideo':
self.send_response(200)
try:
self.video_process.stdin.write('^[[D')
self.video_process.stdin.flush()
except Exception as e:
print(e)
return
elif self.path == '/killVideo':
self.send_response(200)
try:
stop_video()
except Exception as e:
print(e)
return
else:
self.send_response(404)
data_to_send = ""
try:
with open(filePath, 'r') as file:
self.wfile.write(bytes(file.read(), 'utf-8'))
except:
self.wfile.write(bytes('', 'utf-8'))
return
def do_POST(self):
if self.path.endswith("/playVideo"):
content_length = int(self.headers['Content-Length']) # <--- Gets the size of data
youtube_link = self.rfile.read(content_length).decode('utf-8')
self.send_response(200)
play_video(youtube_link)
content_length = int(self.headers['Content-Length']) # <--- Gets the size of data
post_data = self.rfile.read(content_length) # <--- Gets the data itself
print("POST request,\nPath: {}\nHeaders:\n{}\n\nBody:\n{}\n".format(str(self.path), str(self.headers), post_data.decode('utf-8')))
self.send_response(200)
self.wfile.write("POST request for {}".format(self.path).encode('utf-8'))
ありがとう、私はこれで最後のわらまでいるので、助けていただければ幸いです.