07_videoPlayer.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. #!/usr/bin/env python
  2. #version : 2023.04.04
  3. #language : ch
  4. import pyaudio, av, os,_thread
  5. from maix import display, camera, image
  6. # ffmpeg -r 30 -i badapple_240_60fps.mp4 -vf scale=240:240,setdar=1:1 output.mp4
  7. # adb push ./output.mp4 /root/
  8. path_to_video = '/root/preset/video/output_240_240.mp4'
  9. try:
  10. container = av.open(path_to_video)
  11. ai_stream = container.streams.audio[0]
  12. vi_stream = container.streams.video[0]
  13. fifo = av.AudioFifo()
  14. p = pyaudio.PyAudio()
  15. ao = p.open(format=pyaudio.paFloat32, channels=1, rate=44100, output=True)
  16. audio = [p, ao, fifo]
  17. def play_audio(audio):
  18. try:
  19. while len(audio):
  20. for frame in audio[2].read_many(4096):
  21. audio[1].write(frame.planes[0].to_bytes())
  22. except Exception as e:
  23. print(e)
  24. _thread.start_new_thread(play_audio, (audio, ) )
  25. for frame in container.decode(video=0, audio=0):
  26. if 'Audio' in repr(frame):
  27. frame.pts = None
  28. frame.time_base = None
  29. fifo.write(frame)
  30. if 'Video' in repr(frame):
  31. img = image.load(bytes(frame.to_rgb().planes[0]), (vi_stream.width, vi_stream.height))
  32. display.show(img)
  33. finally:
  34. ao.stop_stream()
  35. ao.close()
  36. p.terminate()
  37. audio = []