Nhận thời lượng của tệp/luồng video với ffprobe
$ npm install --save get-video-duration
const { getVideoDurationInSeconds } = require['get-video-duration'] // From a local path... getVideoDurationInSeconds['video.mov'].then[[duration] => { console.log[duration] }] // From a URL... getVideoDurationInSeconds[ '//clips.vorwaerts-gmbh.de/big_buck_bunny.mp4' ].then[[duration] => { console.log[duration] }] // From a readable stream... const fs = require['fs'] const stream = fs.createReadStream['video.mov'] getVideoDurationInSeconds[stream].then[[duration] => { console.log[duration] }] // If you need to customize the path to ffprobe... getVideoDurationInSeconds['video.mov', '/path/to/ffprobe'].then[[duration] => { console.log[duration] }]Giấy phép
MIT. Dựa trên get-video-dimensions
Gói npm get-video-duration nhận được tổng cộng 6.460 lượt tải xuống mỗi tuần. Do đó, chúng tôi cho rằng mức độ phổ biến của thời lượng tải video là Nhỏ
Dựa trên số liệu thống kê dự án từ kho lưu trữ GitHub cho gói npm get-video-duration, chúng tôi thấy rằng nó đã được gắn dấu sao 110 lần và 11 dự án khác trong hệ sinh thái phụ thuộc vào nó
Số lượt tải xuống được tính là trung bình động trong khoảng thời gian 12 tháng qua, không bao gồm các ngày cuối tuần và các điểm dữ liệu bị thiếu đã xác định
Giá trị dấu phẩy động có độ chính xác kép cho biết thời lượng của phương tiện tính bằng giây. Nếu không có sẵn dữ liệu phương tiện, giá trị NaN
được trả về. Nếu phương tiện của phần tử không có thời lượng đã biết—chẳng hạn như đối với luồng phương tiện trực tiếp—giá trị của duration
là +Infinity
Video HTML5 rất thú vị, nếu chỉ vì lý do chúng tôi không còn cần Flash hoặc plugin của bên thứ ba khác để hiển thị phương tiện. Chúng tôi cũng có thể tạo các điều khiển và màn hình tùy chỉnh xung quanh video và một trong những màn hình đó phải là thời lượng của video. Để tôi chỉ cho bạn cách lấy và hiển thị thời lượng video
JavaScript
Phần tử video
có thuộc tính duration
đại diện cho số giây trong video. Để hiển thị thời lượng một cách đẹp mắt, bạn cần phải sử dụng parseInt
và mô-đun [
const { getVideoDurationInSeconds } = require['get-video-duration'] // From a local path... getVideoDurationInSeconds['video.mov'].then[[duration] => { console.log[duration] }] // From a URL... getVideoDurationInSeconds[ '//clips.vorwaerts-gmbh.de/big_buck_bunny.mp4' ].then[[duration] => { console.log[duration] }] // From a readable stream... const fs = require['fs'] const stream = fs.createReadStream['video.mov'] getVideoDurationInSeconds[stream].then[[duration] => { console.log[duration] }] // If you need to customize the path to ffprobe... getVideoDurationInSeconds['video.mov', '/path/to/ffprobe'].then[[duration] => { console.log[duration] }]0]
// Assume "video" is the video node var i = setInterval[function[] { if[video.readyState > 0] { var minutes = parseInt[video.duration / 60, 10]; var seconds = video.duration % 60; // [Put the minutes and seconds in the display] clearInterval[i]; } }, 200];
Điều quan trọng là sử dụng
const { getVideoDurationInSeconds } = require['get-video-duration'] // From a local path... getVideoDurationInSeconds['video.mov'].then[[duration] => { console.log[duration] }] // From a URL... getVideoDurationInSeconds[ '//clips.vorwaerts-gmbh.de/big_buck_bunny.mp4' ].then[[duration] => { console.log[duration] }] // From a readable stream... const fs = require['fs'] const stream = fs.createReadStream['video.mov'] getVideoDurationInSeconds[stream].then[[duration] => { console.log[duration] }] // If you need to customize the path to ffprobe... getVideoDurationInSeconds['video.mov', '/path/to/ffprobe'].then[[duration] => { console.log[duration] }]1 và kiểm tra
const { getVideoDurationInSeconds } = require['get-video-duration'] // From a local path... getVideoDurationInSeconds['video.mov'].then[[duration] => { console.log[duration] }] // From a URL... getVideoDurationInSeconds[ '//clips.vorwaerts-gmbh.de/big_buck_bunny.mp4' ].then[[duration] => { console.log[duration] }] // From a readable stream... const fs = require['fs'] const stream = fs.createReadStream['video.mov'] getVideoDurationInSeconds[stream].then[[duration] => { console.log[duration] }] // If you need to customize the path to ffprobe... getVideoDurationInSeconds['video.mov', '/path/to/ffprobe'].then[[duration] => { console.log[duration] }]2 của video phòng trường hợp video chưa tải vào thời điểm bạn cố gắng kéo dài thời lượng.
parseInt
được dùng để lấy số phút và mô-đun được dùng để lấy số thứ hai