Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Capture Video #30

Closed
LazerJesus opened this issue Aug 22, 2016 · 14 comments
Closed

Capture Video #30

LazerJesus opened this issue Aug 22, 2016 · 14 comments

Comments

@LazerJesus
Copy link

I want to capture a video from the webcam and sent the file to aws s3.
Can I do this with this component and how would you suggest I go about this?

@Extra-lightwill
Copy link

+1

@Extra-lightwill
Copy link

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

@LazerJesus
Copy link
Author

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

@Extra-lightwill
Copy link

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

@LazerJesus
Copy link
Author

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

@Extra-lightwill
Copy link

@FinnFrotscher Have you got a link?

@LazerJesus
Copy link
Author

LazerJesus commented Dec 6, 2016

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}/>)
}

@Extra-lightwill
Copy link

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

@Extra-lightwill
Copy link

Extra-lightwill commented Dec 9, 2016

@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?

@LazerJesus
Copy link
Author

LazerJesus commented Dec 9, 2016

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} />
    )
  }
}

@a22munir
Copy link

a22munir commented Mar 5, 2017

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?

@syam1123
Copy link

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

@shane-hogan
Copy link

How can we capture video with react-webcam?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants