Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a online video's duration without downloading the full video?

To get a video's duration and resolution, I've got this function:

def getvideosize(url, verbose=False):
try:
    if url.startswith('http:') or url.startswith('https:'):
        ffprobe_command = ['ffprobe', '-icy', '0', '-loglevel', 'repeat+warning' if verbose else 'repeat+error', '-print_format', 'json', '-select_streams', 'v', '-show_streams', '-timeout', '60000000', '-user-agent', BILIGRAB_UA, url]
    else:
        ffprobe_command = ['ffprobe', '-loglevel', 'repeat+warning' if verbose else 'repeat+error', '-print_format', 'json', '-select_streams', 'v', '-show_streams', url]
    logcommand(ffprobe_command)
    ffprobe_process = subprocess.Popen(ffprobe_command, stdout=subprocess.PIPE)
    try:
        ffprobe_output = json.loads(ffprobe_process.communicate()[0].decode('utf-8', 'replace'))
    except KeyboardInterrupt:
        logging.warning('Cancelling getting video size, press Ctrl-C again to terminate.')
        ffprobe_process.terminate()
        return 0, 0
    width, height, widthxheight, duration = 0, 0, 0, 0
    for stream in dict.get(ffprobe_output, 'streams') or []:
        if dict.get(stream, 'duration') > duration:
            duration = dict.get(stream, 'duration')
        if dict.get(stream, 'width')*dict.get(stream, 'height') > widthxheight:
            width, height = dict.get(stream, 'width'), dict.get(stream, 'height')
    if duration == 0:
        duration = 1800
    return [[int(width), int(height)], int(float(duration))+1]
except Exception as e:
    logorraise(e)
    return [[0, 0], 0]

But some online videos comes without duration tag. Can we do something to get its duration?

like image 671
David Zhuang Avatar asked Nov 17 '14 14:11

David Zhuang


2 Answers

If you have direct link to the video itself, like http://www.dl.com/xxx.mp4, you can direct use ffprobe to get the duration of this video by using:

ffprobe -i some_video_direct_link -show_entries format=duration -v quiet -of csv="p=0"
like image 193
einverne Avatar answered Oct 05 '22 07:10

einverne


import cv2
data = cv2.VideoCapture('https://v.buddyku.id/ugc/m3YXvl-61837b3d8a0706e1ee0ab139.mp4')
frames = data.get(cv2.CAP_PROP_FRAME_COUNT)
fps = int(data.get(cv2.CAP_PROP_FPS))
seconds = int(frames / fps)
print("duration in seconds:", seconds)
like image 41
Mukul Kirti Verma Avatar answered Oct 05 '22 09:10

Mukul Kirti Verma