In HTML5 Video , How To Play A Small Clip From A Long Video?

Just browsing Stack Overflow? Help us improve your experience. Sign up for research
    1. Home
    2. Questions
    3. Tags
    4. Users
    5. Companies
    6. Labs
    7. Jobs
    8. Discussions
    9. Collectives
    10. Communities for your favorite technologies. Explore all Collectives

  1. Teams

    Ask questions, find answers and collaborate at work with Stack Overflow for Teams.

    Try Teams for free Explore Teams
  2. Teams
  3. Ask questions, find answers and collaborate at work with Stack Overflow for Teams. Explore Teams

Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Get early access and see previews of new features.

Learn more about Labs In HTML5 Video , how to play a small clip from a long video? Ask Question Asked 12 years, 8 months ago Modified 1 year, 7 months ago Viewed 24k times 18

I would like to show a small clip from a long video file that is over 10 minutes long. This segment of video would start at time offset /seek time of 90 seconds and would have a duration of 45 seconds . How can I do that ?

Share Improve this question Follow asked Mar 23, 2012 at 19:43 user193116's user avatar user193116user193116 3,5586 gold badges43 silver badges60 bronze badges 1
  • Be aware that this way you will be transferring the whole video (which might be rather big in terms of filesize) to your client's device. A user-friendly approach would probably solve this in editing already and supply a specific file that only covers the snippet. – m90 Commented Mar 27, 2012 at 11:21
Add a comment |

3 Answers 3

Sorted by: Reset to default Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first) 55

HTML5 video also supports the Media Fragment URI spec. This will allow you to specify only a segment of the video to play. Using it is fairly trivial:

<source src="video.mp4#t=30,45" type="video/mp4"/>

Will start the video at the 30 second mark and pause the video at the 45 second mark.

Share Improve this answer Follow answered Jun 7, 2013 at 20:29 Jim S.'s user avatar Jim S.Jim S. 1,1601 gold badge9 silver badges13 bronze badges 5
  • 9 Just a note that I noticed that Chrome continues to download the rest of the video. It pauses the video at 45 seconds, but a user can resume play from that point. If you're looking at this as a means to limit traffic or to cut off the rest of a video, this is not your solution. – villecoder Commented Dec 23, 2014 at 15:45
  • You'll need to work with the video player controls to prevent this. You could make the video player jump back to 30 seconds into the video if the play button is pressed when the video is at the 45 second mark. It would be pretty trivial to parse the media fragments out of the src to make that kind of functionality. – Jim S. Commented Apr 21, 2015 at 16:45
  • If this is widely supported in all major browsers then it should be the accepted answer. – TimHayes Commented Apr 7, 2016 at 17:34
  • 1 Browser support should not matter. This is part of a w3c spec that is approved, then it should be the top answer for technical merit. However the JS answer (also depends on browser implementation of DOM & JS); is a great way to show programmatic access to media via HTML5 – MrMesees Commented May 24, 2016 at 10:05
  • 1 Is it possible to have two different segments? so that it plays one and then the other? – tofutim Commented Jun 14, 2018 at 0:56
Add a comment | 14

Phillip Brown is right. you can solve this by controlling yout html-player via js. for example in this case, the video would autostart and will play the videofile should 00:10min to 00:40min

<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> <script type="text/javascript"> window.onload = playVideoTeaserFrom(10,40); //this event will call the function after page was loaded function playVideoTeaserFrom (startTime, endTime) { var videoplayer = document.getElementById("yourVideoplayer"); //get your videoplayer videoplayer.currentTime = starttime; //not sure if player seeks to seconds or milliseconds videoplayer.play(); //call function to stop player after given intervall var stopVideoAfter = (endTime - startTime) * 1000; //* 1000, because Timer is in ms setTimeout(function(){ videoplayer.stop(); }, stopVideoAfter); } </script>

there might be some bugs in it, but i guess you ll get the point

Share Improve this answer Follow answered Mar 23, 2012 at 23:37 LONGI's user avatar LONGILONGI 11.4k10 gold badges59 silver badges93 bronze badges 5
  • This function doesn't seem to work. Video is normally running full length. – imbenzene Commented Aug 29, 2014 at 17:24
  • That's because @longilong set window.onload to undefined, and other problems with the code. There are obvious bugs. – Sam Commented Jul 16, 2015 at 1:23
  • If the user fast forwards or rewinds with the mouse the timer would stop at a certain time different from the one expect it to. – jRam90 Commented Oct 15, 2015 at 17:13
  • 2 This is a bad solution - if the user interacts with the video (pause for example) it will completely distort the TO calculation. to solve this issue you should listen to timeupdate event, and pause the video if vid.currentTime is not between start to end – yonatanmn Commented Mar 15, 2016 at 18:10
  • Jim S answer should be marked as correct as it's part of the spec, but I think this answer is a great way to demonstrate programmatic access, so hat off to you! – MrMesees Commented May 24, 2016 at 10:03
Add a comment | 0

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <video id="myVideo" controls playsinline muted> <source src="https://sample-videos.com/video123/mp4/480/big_buck_bunny_480p_30mb.mp4" type="video/mp4"> </video> <script> let vid = document.getElementById("myVideo"); const delay = ms => new Promise(resolve => setTimeout(resolve,ms)); async function run(start,end,speed=1){ vid.playbackRate = speed while(true){ vid.currentTime = start; await vid.play() await delay((end - start)*1000) await vid.pause() } } window.onload = run(70,74) </script> </body> </html>

We can use asynchronies function to achieve this. This code is a modified version of @longi code

Share Improve this answer Follow edited Apr 6, 2023 at 14:18 answered Apr 6, 2023 at 14:06 Devadut S Balan's user avatar Devadut S BalanDevadut S Balan 561 silver badge3 bronze badges Add a comment | Highly active question. Earn 10 reputation (not counting the association bonus) in order to answer this question. The reputation requirement helps protect this question from spam and non-answer activity.

Not the answer you're looking for? Browse other questions tagged or ask your own question.

  • The Overflow Blog
  • Your docs are your infrastructure
  • Featured on Meta
  • More network sites to see advertising test [updated with phase 2]
  • We’re (finally!) going to the cloud!
Visit chat

Linked

8 How to reduce the length of a video using JavaScript? 2 Play subclip of video 0 Local / session storage - pause video and playback from where it has stoped - JavaScript 2 How do I play just a specific section of a video in a web page? 0 How do I play 2 videos in HTML5 back to back 12 HTML 5 Video, Streaming / Buffering only a certain portion of a longer video 17 HTML5 video - playing only a portion of a video 18 Play full HTML5 video and then loop a section of it 0 How can I get Chrome to load only a part of a video for a HTML5 video tag? 23 HTML5 video how to play two videos in one video element 0 How to play a second .mp4 video after one has ended? 0 HTML 5 Video: Playing multiple "clips" with javascript 1 Html5 video tag - load only the few first seconds

Hot Network Questions

  • How do I go about rebranding a fully deleted project that used to have a GNU General Public License v3.0 but is now fully inaccessible
  • p column type stops compilation in RevTeX
  • Is it possible to use NAS hard drives in a desktop?
  • What mechanism could cause a person not to cast a reflection?
  • Why is the chi-square test giving unintuitive results?
  • I can't put a plug into a new TR Leviton GFCI outlet
  • Arrange 3 red balls and 33 white balls randomly in a circle. What is the probability that there are no more than 13 consecutive white balls?
  • Polynomial.java - a Java class for dealing with polynomials with BigDecimal coefficients
  • What's a good way to append a nonce to ciphertext in Python for AES GCM in Python?
  • BIOS drive order is inverse of Windows'
  • If the hard problem of consciousness is unanswerable, is it a hard problem or just a bad question?
  • What exactly is the cornerstone that Mark 12:10 speaks of?
  • Walks in Nice (Nizza)
  • What's the Purpose of the IRQ on a 6502
  • Is Holy Terra Earth?
  • Should I ask for physical recommendation letters now to avoid future issues with professors' availability?
  • Why does Jesus tell the Jews to follow the Pharisees teaching while elsewhere He speaks of them creating unnecessary rules?
  • Can .zshrc be modified automatically?
  • Replacement chain looks different from factory chain
  • Hypothesis and theory
  • Does the 90 day window for VWP reset for extended stay in Mexico?
  • When did Storm Troopers stop being clones?
  • Sub panel location question
  • Can this strong directional blur at wide apertures still be explained by the usual arguments?
more hot questions Question feed Subscribe to RSS Question feed

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

lang-html

Từ khóa » Html5 Video Segment