Skip to content

Instantly share code, notes, and snippets.

@dasl-
Last active July 2, 2019 05:38
Show Gist options
  • Save dasl-/17910d9acc752f1209e354f89e041436 to your computer and use it in GitHub Desktop.
Save dasl-/17910d9acc752f1209e354f89e041436 to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
import argparse
from lightness.settings import Settings
from lightness.videoplayer import VideoPlayer
from lightness.videoprocessor import VideoProcessor
import numpy as np
import sys
import cv2
import time
import io
import datetime
import subprocess
from picamera.array import PiRGBArray
from picamera import PiCamera
def parseArgs():
parser = argparse.ArgumentParser(description='convert a video.')
parser.add_argument('--url', dest='url', action='store', default='https://www.youtube.com/watch?v=xmUZ6nCFNoU',
help='youtube video url. default: The Smashing Pumpkins - Today.')
parser.add_argument('--display-width', dest='display_width', action='store', type=int, default=28, metavar='N',
help='Number of pixels / units')
parser.add_argument('--display-height', dest='display_height', action='store', type=int, default=18, metavar='N',
help='Number of pixels / units')
parser.add_argument('--color', dest='is_color', action='store_true', default=False,
help='color output? (default is black and white)')
parser.add_argument('--flip-x', dest='flip_x', action='store_true', default=False,
help='flip X direction output')
parser.add_argument('--flip-y', dest='flip_y', action='store_true', default=False,
help='flip Y direction output')
parser.add_argument('--brightness', dest='brightness', action='store', type=int, default=3, metavar='N',
help='Global brightness value. Max of 31.')
args = parser.parse_args()
return args
args = parseArgs()
video_settings = Settings(args)
video_player = VideoPlayer(video_settings)
video_player.clearScreen()
# # video_processor = VideoProcessor(video_settings, None)
# initialize the camera
camera = PiCamera(resolution=(80,60), framerate=24)
camera.resolution = (80, 60)
# camera.zoom = (0, 0, 1, 1)
camera.vflip = True
rawCapture = PiRGBArray(camera, size=(28,18))
time.sleep(0.1)
for frame in camera.capture_continuous(rawCapture, format="rgb", resize=(28,18), use_video_port=True):
image = frame.array
# instead should send to playRGBImage or something, and allow it to swap the g/b
video_player.playFrame(image)
rawCapture.truncate(0)
print(datetime.datetime.now())
# # need to use this for black and white probably
# # note: ffmpeg can use arrays as input, should work faster than video
# pix_fmt = 'gray'
# ffmpeg = subprocess.Popen([
# 'ffmpeg', '-i', '-',
# '-filter:v', 'scale=28x18',
# '-c:a', 'copy', # don't process the audio at all
# '-f', 'rawvideo', '-pix_fmt', pix_fmt, # output in numpy compatible byte format
# 'pipe:1' # output to stdout
# ], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
# # initialize the camera
# # camera = PiCamera(resolution=(80,40), framerate=25)
# camera = PiCamera(resolution=(80,40), framerate=25)
# camera.resolution = (80, 60)
# camera.zoom = (.15, .15, .85, .85)
# camera.vflip = True
# # start recording to ffmpeg's stdin
# # camera.start_recording(ffmpeg.stdin, format='h264', bitrate=2000000) #works, but slow
# # camera.start_recording(ffmpeg.stdin, format='h264', bitrate=100000, quality = 35)
# display_width = 28
# display_height = 18
# bytes_per_frame = display_width * display_height
# np_array_shape = [display_height, display_width]
# # trash all the data
# time.sleep(5)
# print("FLUSHING BUFFER")
# camera.start_recording(ffmpeg.stdin, format='h264', bitrate=100000, quality = 35)
# ffmpeg.stdout.flush()
# print("PLAY")
# firstRead = True
# while True:
# print("READ")
# in_bytes = ffmpeg.stdout.read(bytes_per_frame)
# while in_bytes != "":
# print("A")
# use_bytes = in_bytes
# in_bytes = ffmpeg.stdout.read(bytes_per_frame)
# # print("A")
# avg_color_frame = np.frombuffer(use_bytes, np.uint8).reshape(np_array_shape)
# # print("B")
# video_player.playFrame(avg_color_frame)
# # print("C")
# # if (firstRead):
# # fmpeg.stdout.read(-1)
# # # ffmpeg.stdout.flush()
# # # in_bytes = ffmpeg.stdout.read()
# # # in_bytes = ffmpeg.stdout.flushInput()
# # firstRead = False
# camera.stop_recording()
# # pix_fmt = 'gray'
# bytes_per_frame = 28*18 * 3
# # in_pix_fmt = 'rgb24'
# pix_fmt = 'rgb24'
# np_array_shape = [28, 18, 3]
# ffmpeg = subprocess.Popen([
# 'ffmpeg',
# # '-pix_fmt', in_pix_fmt, # input format from camera
# '-i', '-',
# '-filter:v', 'scale=28x18',
# '-c:a', 'copy', # don't process the audio at all
# '-f', 'rawvideo', '-pix_fmt', pix_fmt, # output in numpy compatible byte format
# 'pipe:1' # output to stdout
# ], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
# # trash all the data
# time.sleep(.5)
# print("FLUSHING BUFFER")
# # camera.start_recording(ffmpeg.stdin, format='h264', bitrate=50000, quality = 35)
# camera.start_recording(ffmpeg.stdin, format='rgb', resize=(28,18))
# # ffmpeg.stdout.flush()
# print("PLAY")
# while True:
# print("READ")
# in_bytes = ffmpeg.stdout.read(bytes_per_frame)
# print("A")
# avg_color_frame = np.frombuffer(in_bytes, np.uint8).reshape(np_array_shape)
# # print(avg_color_frame)
# # print("B")
# video_player.playFrame(avg_color_frame)
# # print("C")
# camera.stop_recording()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment