admin管理员组

文章数量:1406951

I am working on one project in which I need to show the timestamp of video in actual time format as "01:12:50". Below is my code which return the current time of video as frame rate ie "12.526". Can anyone help me how I can change it to actual time or is there any way that I can directly get the video timestamp using HTML and JavaScript.

<video id="myVideo" width="740px" height="600px"></video>
<div id="timer"> </div>

<script>
    document.getElementById("myVideo").addEventListener('timeupdate', function() {
        document.getElementById("timer").innerHTML = this.currentTime;
        currentTime = this.currentTime;
    });
</script>

I am working on one project in which I need to show the timestamp of video in actual time format as "01:12:50". Below is my code which return the current time of video as frame rate ie "12.526". Can anyone help me how I can change it to actual time or is there any way that I can directly get the video timestamp using HTML and JavaScript.

<video id="myVideo" width="740px" height="600px"></video>
<div id="timer"> </div>

<script>
    document.getElementById("myVideo").addEventListener('timeupdate', function() {
        document.getElementById("timer").innerHTML = this.currentTime;
        currentTime = this.currentTime;
    });
</script>
Share Improve this question edited Feb 28, 2023 at 3:24 imxitiz 3,9873 gold badges12 silver badges35 bronze badges asked Jul 6, 2021 at 17:08 UsmanUsman 2,0292 gold badges18 silver badges30 bronze badges 1
  • 1 currentTime returns current time in seconds. So "12.526" (in your question) is not framerate but current timing of the video in seconds. – Shyam Commented Jul 6, 2021 at 17:24
Add a ment  | 

1 Answer 1

Reset to default 4

You can just do a simple math!

document.getElementById("myVideo").addEventListener('timeupdate', function() {
    var hours=parseInt(video.currentTime/(60*60),10);
    var minutes = parseInt(video.currentTime / 60, 10);
    var seconds = video.currentTime % 60;
    if (hours==0) { 
        documentgetElementById("timer").innerHTML=minutes+":"+seconds.toFixed(0)
    } else { 
        document.getElementById("timer").innerHTML=hours+":"+minutes+":"+seconds.toFixed(0)
    }
});

You may or may not need if else code. You can change as you want.

本文标签: javascriptHow to get video timestamp in HTMLStack Overflow