Saeed Areffard
Saeed Areffard

Reputation: 179

how to download a part of video in html5 and js

I want to download or save a part of a video that is playing in my HTML and I don't want to access webcam and microphone I just need to download or play a part of a video tag in another video tag or download it ... How can I do that?... anything I saw Was accessing the microphone and webcam

    <video id="yourVideoplayer" width="640" height="480" preload="auto"> //preload="auto" buffers the video if initialize. you cannot seek a video which isn t buffering already
  <source src="test.mp4" type="video/mp4" />
  <source src="test.ogv" type="video/ogg" /> 
  This browser is not compatible with HTML 5
</video>

I want a button to start recording from the video playing then I want to save it

Upvotes: 3

Views: 3510

Answers (1)

ecrb
ecrb

Reputation: 710

<!DOCTYPE html>
<html>
<body>    
<video id="original-video" width="320" height="240" controls>
  <source src="movie.mp4" type="video/mp4">
  <source src="movie.ogg" type="video/ogg">
  Your browser does not support the video tag.
</video>
<video width="320" height="240" data-from="original-video" data-start="5" data-end="10" data-autoreplay="true" autoplay>
</video>
<video width="320" height="240" data-from="original-video" data-start="5" data-end="10" data-autoreplay="false" autoplay>
</video>
<script>
const body = document.querySelector('body');
const videos = document.querySelectorAll('video[data-from]');

videos.forEach(video=>{
    const originalVideo = document.getElementById(video.dataset.from);
    [...originalVideo.children].forEach(child=>{
        video.appendChild(child.cloneNode())
    });

    video.duration = Number(video.dataset.end) - Number(video.dataset.start);
    video.currentTime = video.dataset.start;

    video.addEventListener('timeupdate', (e)=>{
      if(video.currentTime>=video.dataset.end){
          video.pause();
          video.currentTime=video.dataset.start;
          if(video.dataset.autoreplay==="true"){
            video.play();
          }
      }
    });
});

</script>
</body>

This is the closest I could get to your requirements. You use the data attributes to specify:

  • the original video
  • the start
  • the end
  • if it should replay non-stop

Upvotes: 2

Related Questions