Paso 8: Recompilar FFMPEG
Una utilidad llamada FFMPEG hará el streaming de vídeo. Raspbian viene con FFMPEG instalado, sin embargo, la versión previamente instalada no está compilada para usar el controlador nativo de GPU para que FFMPEG necesita ser recompilado para usarlo. Encontré las instrucciones aquí funcionan bien, tenga en cuenta que las secciones de la crtmpserver en ese sitio no son necesarios porque estamos streaming video por internet a los servidores de SEGfL. Los siguientes pasos desde el enlace mencionado serán recompilar FFMPEG. Una nota de PRECAUCIÓN: cuando escribe en 'make' sugeriría dejarlo correr durante la noche porque tarda horas para volver a compilar!
cd /usr/src
sudo chown `whoami`:users ffmpeg
git clone git://source.ffmpeg.org/ffmpeg.git ffmpegcd ffmpeg<br>
./configure
make
sudo make install
sudo raspivid -t -1 -w 960 -h 540 -fps 25 -b 500000 -vf -o - | ffmpeg -i - -vcodec copy -an -f flv rtmp://<username>:<password>
FFMPEG está ahora listo para ser utilizado. Puedes probar con la página de streaming de SEGfL los siguientes pasos:
- Abrir el fmeProfile.xml de archivo de configuración XML que descargó desde el sitio web SEGfL
- Busque la etiqueta XML 'url', que debe estar bajo la etiqueta de 'rtmp'. Será algo como: rtmp://broadcast.e2bn.org/microsites2fme
- Justo debajo de este hallazgo el XML de la etiqueta 'corriente' que es una larga secuencia de dígitos y caracteres
- También necesitarás el usuario y contraseña que usted anotado anteriormente
FFMPEG y la página web de SEGfL Birdbox pueden ser probados usando raspivid como sigue:
import smtplib import email import mimetypes import StringIO import subprocess import os import time import sys import time import picamera from datetime import datetime from PIL import Image from email.MIMEMultipart import MIMEMultipart from email.Utils import COMMASPACE from email.MIMEBase import MIMEBase from email.parser import Parser from email.MIMEImage import MIMEImage from email.MIMEText import MIMEText from email.MIMEAudio import MIMEAudio</p><p># Original code written by brainflakes and modified to exit # image scanning for loop as soon as the sensitivity value is exceeded. # this can speed taking of larger photo if motion detected early in scan # Motion detection settings: # PGM maded changes to read values dynamically via command line parameters. # -------------------------- # Threshold - (how much a pixel has to change by to be marked as "changed") # Sensitivity - (how many changed pixels before capturing an image) needs to be higher if noisy view # ForceCapture - (whether to force an image to be captured every forceCaptureTime seconds) # filepath - location of folder to save photos # filenamePrefix - string that prefixes the file name for easier identification of files. threshold = 10 sensitivity = 180 forceCapture = True forceCaptureTime = 60 * 60 # Once an hour filepath = "/home/pi/images/" filenamePrefix = "pgm" fileType = "jpg" # File photo size settings saveWidth = 800 saveHeight = 600 diskSpaceToReserve = 40 * 1024 * 1024 # Keep 40 mb free on disk sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0) camera = picamera.PiCamera()</p><p># Email variables user = 'mattwood262 smtp_host = 'smtp.gmail.com' smtp_port = 587 # server = smtplib.SMTP() # server.connect(smtp_host,smtp_port) # server.ehlo() # server.starttls() # server.login(user,'') fromaddr = 'matt tolist = 'matt sub = 'Subject: birdbox activity'</p><p># Capture a small test image (for motion detection) def captureTestImage(): imageData = StringIO.StringIO() # try: camera.capture(imageData, format='bmp', use_video_port=True) # command = "raspistill -w %s -h %s -t 1 -n -e bmp -o -" % (100, 75) # except: # camera.capture(imageData, format='bmp', use_video_port=True) imageData.seek(0) im = Image.open(imageData) buffer = im.load() imageData.close() return im, buffer</p><p># Save a full size image to disk def saveImage(width, height, diskSpaceToReserve): keepDiskSpaceFree(diskSpaceToReserve) time = datetime.now() filename = filenamePrefix + "-%04d_%02d_%02d-%02d%02d%02d" % (time.year, time.month, time.day, time.hour, time.minute, time.second)+ "." + fileType fullfilename = filepath + filename # camera.resolution = (saveWidth, saveHeight) camera.capture(fullfilename, format='jpeg', use_video_port=True) # subprocess.call("echo %s > temp.out" % filename, shell=True) # subprocess.call("/home/pi/Dropbox-Uploader/dropbox_uploader.sh upload %s %s" % (fullfilename, filename), shell=True) # msg = email.MIMEMultipart.MIMEMultipart() # msg['From'] = fromaddr # msg['To'] = tolist # msg['Subject'] = sub # msg.attach(MIMEText('\nsent via python', 'plain')) # server.sendmail(user,tolist,msg.as_string()) # print "Captured image: %s" % filename camera.wait_recording(60)</p><p># Keep free space above given level def keepDiskSpaceFree(bytesToReserve): if (getFreeSpace() < bytesToReserve): for filename in sorted(os.listdir(".")): if filename.startswith(filenamePrefix) and filename.endswith("." + fileType): os.remove(filename) print "Deleted %s to avoid filling disk" % filename if (getFreeSpace() > bytesToReserve): return</p><p># Get available disk space def getFreeSpace(): st = os.statvfs(".") du = st.f_bavail * st.f_frsize return du</p><p>#--------------------------------------------------------- </p><p># Start capturing video camera.resolution = (640, 480) camera.framerate = 30 # camera.resolution = (1024, 768) camera.start_recording(sys.stdout, format='h264')</p><p># Get first image image1, buffer1 = captureTestImage()</p><p># Reset last capture time lastCapture = time.time()</p><p># added this to give visual feedback of camera motion capture activity. Can be removed as required os.system('clear') f = open('./logfile','w') f.write(' Motion Detection Started') f.write(' ------------------------') # print "Pixel Threshold (How much) = " + str(threshold) # print "Sensitivity (changed Pixels) = " + str(sensitivity) # print "---------- Motion Capture File Activity --------------"</p><p>while (True):</p><p> # Get comparison image try: image2, buffer2 = captureTestImage() except: subprocess.call("echo 'timeout' > temp.out", shell=True) image2, buffer2 = captureTestImage()</p><p> # Count changed pixels changedPixels = 0 for x in xrange(0, 100): # Scan one line of image then check sensitivity for movement for y in xrange(0, 75): # Just check green channel as it's the highest quality channel pixdiff = abs(buffer1[x,y][1] - buffer2[x,y][1]) if pixdiff > threshold: changedPixels += 1</p><p> # Changed logic - If movement sensitivity exceeded then # Save image and Exit before full image scan complete if changedPixels > sensitivity: lastCapture = time.time() saveImage(saveWidth, saveHeight, diskSpaceToReserve) break continue</p><p> # Check force capture if forceCapture: if time.time() - lastCapture > forceCaptureTime: changedPixels = sensitivity + 1 # Swap comparison buffers image1 = image2 buffer1 = buffer2 #------------------------------------------------------------
Si vas a la página de Webcam en vivo para su escuela en el sitio web del proyecto Birdbox debería ver el video en vivo.
Tenga en cuenta que he probado muchas configuraciones para mejorar la calidad de vídeo, aumentar la resolución y aumentar lo fotogramas por segundo pero creo que la configuración de servidor tienen prioridad.