简体   繁体   English

如何在 django 中进行实时视频/音频流传输

[英]how to do real time video/audio streaming in django

i want to stream the video and audio (and some real time data which i will get from precessing every fram) from surveillance camera into a django website... i found this code that help me send frames to the client我想 stream 将监控摄像头中的视频和音频(以及我将从处理每一帧中获得的一些实时数据)到 django 网站...我发现这个代码可以帮助我向客户端发送帧

''' '''

 from django.shortcuts import render
from django.http import HttpResponse, StreamingHttpResponse
import cv2
import time
from django.views.decorators import gzip


class VideoCamera(object):
    def __init__(self):
        self.video = cv2.VideoCapture('./streaming/video.mp4')

    def __del__(self):
        self.video.release()

    def get_frame(self):
        ret, image = self.video.read()
        ret, jpeg = cv2.imencode('.jpg', image)
        return jpeg.tobytes()


def gen(camera):
    while True:
        frame = camera.get_frame()
        yield(b'--frame\r\n'
              b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')


@gzip.gzip_page
def index(request):
    try:
        return StreamingHttpResponse(gen(VideoCamera()), content_type="multipart/x-mixed-replace;boundary=frame")
    except HttpResponseServerError as e:
        print("aborted")

but i dont know how to handle the audio and data and the synchronization.但我不知道如何处理音频和数据以及同步。 i want to know what technology i have to use and if there any tutorial or ideas about it.我想知道我必须使用什么技术,以及是否有任何关于它的教程或想法。 i really don't know what to read and how to start (i'm using django).我真的不知道该读什么以及如何开始(我正在使用 django)。

Follow for more details:关注更多详情:

Github: https://github.com/JRodrigoF/AVrecordeR Github: https://github.com/JRodrigoF/AVrecordeR

class AudioRecorder():


    # Audio class based on pyAudio and Wave
    def __init__(self):

        self.open = True
        self.rate = 44100
        self.frames_per_buffer = 1024
        self.channels = 2
        self.format = pyaudio.paInt16
        self.audio_filename = "temp_audio.wav"
        self.audio = pyaudio.PyAudio()
        self.stream = self.audio.open(format=self.format,
                                      channels=self.channels,
                                      rate=self.rate,
                                      input=True,
                                      frames_per_buffer = self.frames_per_buffer)
        self.audio_frames = []


    # Audio starts being recorded
    def record(self):

        self.stream.start_stream()
        while(self.open == True):
            data = self.stream.read(self.frames_per_buffer) 
            self.audio_frames.append(data)
            if self.open==False:
                break


    # Finishes the audio recording therefore the thread too    
    def stop(self):

        if self.open==True:
            self.open = False
            self.stream.stop_stream()
            self.stream.close()
            self.audio.terminate()

            waveFile = wave.open(self.audio_filename, 'wb')
            waveFile.setnchannels(self.channels)
            waveFile.setsampwidth(self.audio.get_sample_size(self.format))
            waveFile.setframerate(self.rate)
            waveFile.writeframes(b''.join(self.audio_frames))
            waveFile.close()

        pass

    # Launches the audio recording function using a thread
    def start(self):
        audio_thread = threading.Thread(target=self.record)
        audio_thread.start()

Actully I don't have much idea with open cv2.实际上,我对 open cv2 没有太多想法。 But I think we cannot record video with audio simultaneously.但我认为我们不能同时录制视频和音频。 You have to capture audio and video in thread.您必须在线程中捕获音频和视频。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM