Giter Club home page Giter Club logo

Comments (14)

LazerJesus avatar LazerJesus commented on August 10, 2024 9

I am using WebRTC (RecordRTC) and getUserMedia.
This is a little bit chaotic but it does the job.
I had to clean it up a little so dont expect it to work copy&paste out of the box.
the important function for you is setupVideo()

import RecordRTC from 'recordrtc'
import Video from '../../components/Video'

export default class Record extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      stream: null,
      url:'',
      videoRecorder:'',
      isRecording: false,
      isUploading: false,
      isPreview: false,
      isDone: false,
      pausing: false,
    }
 
  hasGetUserMedia() {
    return !!(navigator.getUserMedia || navigator.webkitGetUserMedia ||
      navigator.mozGetUserMedia || navigator.msGetUserMedia || navigator.oGetUserMedia);
  }
setupVideo(callback){
    navigator.getUserMedia  = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
    //check if stream has already been captured
    if (this.state.stream) {
      console.log(this.state.stream);
      this.setState({
        url: window.URL.createObjectURL(this.state.stream)
      }, callback);
    } else {
      navigator.getUserMedia({
          audio: true,
          video: {
            frameRate: { ideal: 20, max: 30 }},
            height:'480px', width:'640px'
        }, (stream) => {
          this.setState({
            stream: stream,
            url: window.URL.createObjectURL(stream)
          }, callback);
        },(err) => { console.log(err.name) })
    }
  }
  pauseRecording(){
    this.state.videoRecorder.pauseRecording()
    this.setState({
      pausing: true,
    });
  }
  startRecording(){
    if (this.state.pausing) {
      this.state.videoRecorder.resumeRecording()
      this.setState({
        pausing: false,
      });
      return
    }
    this.setState({
      videoRecorder:'',
      isUploading:  false,
      pausing:  false,
      isPreview: false,
      hasRecording: false,
    }, this.setupVideo.bind(this, () => {
      const videoRecorder = RecordRTC(this.state.stream, {
        type: 'video',
        video: {
          width: 640,
          height: 480
         },
         canvas: {
          width: 640,
          height: 480
        }
       })
      videoRecorder.startRecording()
      this.setState({
        videoRecorder: videoRecorder,
        isRecording: true,
      });
    }))
  }
  stopRecording(){
    window.dispatchEvent(new CustomEvent('stopRecording'));
    this.state.videoRecorder.stopRecording((url) => {
      this.setState({
        isRecording: false,
        pausing:  false,
        hasRecording:  true,
      });
    })
  }

startPreview(){
    this.state.videoRecorder.getDataURL((url) => {
      this.setState({
        url : url,
        isPreview: true
      });
    })
  }
  uploadFile(){
    this.setState({
      pausing:  false,
      isRecording: false,
      isUploading:  1,
      isPreview: false,
    });

    this.state.videoRecorder.getDataURL((data) => {

      var uploadData = {
        fileName: id
        file: data,
        isBase64: true,
        streams: 'dynamic',
        chunkSize: 'dynamic',
      }

      var uploadInstance = Videos.insert(uploadData, false);

      uploadInstance.on('progress', (progress, file) => {
        this.setState({
          isUploading:  progress,
        });
      });

      uploadInstance.on('end', (error, fileObj) => {
        if (error) {
          console.log('Error during upload: ' + error.reason);
        } else {
          console.log(fileObj.meta._id, fileObj.meta.try)
          this.setState({
            id: fileObj._id,
            isDone: true
          });
        }
      });
      uploadInstance.start()
    })
  }
  render(){
    return (  <Video
                ref='video'
                isPreview={this.state.isPreview}
                url={this.state.url}/>)
}

from react-webcam.

LazerJesus avatar LazerJesus commented on August 10, 2024 4

plain old html video tag.

import React from 'react'

export default class Video extends React.Component {
  render() {
    // console.log(this.props);
    return (
      <video
        id='video'
        style={{height:'480px', width:'640px'}}
        ref='video'
        preload='auto'
        controls={this.props.isPreview}
        autoPlay={!this.props.isPreview}
        src={this.props.url} />
    )
  }
}

from react-webcam.

Extra-lightwill avatar Extra-lightwill commented on August 10, 2024 2

@FinnFrotscher Great, that's massively helpful. Appreciate it!

from react-webcam.

LazerJesus avatar LazerJesus commented on August 10, 2024 1

ok ill look into my code tomorrow and upload the relevant bits.

from react-webcam.

Extra-lightwill avatar Extra-lightwill commented on August 10, 2024

+1

from react-webcam.

Extra-lightwill avatar Extra-lightwill commented on August 10, 2024

@FinnFrotscher Have you managed to integrate video capturing functionality??

from react-webcam.

LazerJesus avatar LazerJesus commented on August 10, 2024

I did but i am not sure whether i used this component.
also I saved the files to my own server. not aws. still interested?
@Extra-lightwill

from react-webcam.

Extra-lightwill avatar Extra-lightwill commented on August 10, 2024

@FinnFrotscher I am building a prototype so yes still interested...

from react-webcam.

Extra-lightwill avatar Extra-lightwill commented on August 10, 2024

@FinnFrotscher Have you got a link?

from react-webcam.

Extra-lightwill avatar Extra-lightwill commented on August 10, 2024

@FinnFrotscher Just out of interest, what does your video.js file look like? Assuming you're using react, there are some react-specific video repos, but I'm leaning towards going for something more established e.g. https://github.com/Selz/plyr or https://github.com/videojs/video.js/.. Wondering whether this is the right choice or not based on your experience?

from react-webcam.

a22munir avatar a22munir commented on August 10, 2024

Hey @FinnFrotscher I am trying to get your code to work but running into errors. Do you have a repo with a component that has all the code for turning this into a video capture component?

from react-webcam.

syam1123 avatar syam1123 commented on August 10, 2024

@mozmorris Have you managed to add the record video feature in react-webcam?

from react-webcam.

shane-hogan avatar shane-hogan commented on August 10, 2024

How can we capture video with react-webcam?

from react-webcam.

mozmorris avatar mozmorris commented on August 10, 2024

@shane-hogan see https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/captureStream. There's also a guide https://developer.mozilla.org/en-US/docs/Web/API/MediaStream_Recording_API/Recording_a_media_element

from react-webcam.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.