Extract frames from HTTP streamed video without downloading entire file

5,767

ffmpeg should be able to read from URLs as well. Just specify the offset in seconds or HH:MM:SS.msec format for the -ss option:

ffmpeg -i http://example.com/video.mp4 -ss 5 -frames:v 1 thumbnail.png

This works with other HTML5 containers as well.

If you're on a Linux distro, make sure that you don't install ffmpeg via apt-get install ffmpeg, but compile it yourself or download a static build. The versions in official packages are always a little outdated, and—depending on the Ubuntu version—not even "real" ffmpeg.

Share:
5,767

Related videos on Youtube

Ortix92
Author by

Ortix92

Updated on September 18, 2022

Comments

  • Ortix92
    Ortix92 over 1 year

    I want to extract a frame from a specific location in a video file. The file is located on a remote server and I don't want to download the entire thing. How can I extract a frame from an http stream programmatically?

    I can do what I want with VLC with the GUI, but I don't know how to do it in the CLI for example. Perhaps a way to send range headers with the VLC cli?

    I'm trying to do this on a headless ubuntu machine

  • Ortix92
    Ortix92 over 9 years
    So far this works! I need to do some further testing! EDIT: I removed my previous answer because i think i made a bad mistake then ;)
  • Ortix92
    Ortix92 over 9 years
    Yup, I can confirm that this works. I put -ss up front for input seeking and somehow it worked. I'm running this with peerflix (torrent streaming app in node) and it simply works. No sure if it sends range headers or waits for the chunk to be downloaded, but nevertheless it does what it should without downloading the entire file!
  • user779159
    user779159 over 7 years
    This still downloads the whole file up to that point. If you use -ss 5, it takes a few seconds as it downloads the first 5 seconds of the video, but if you use -ss 500, it takes a few minutes as it downloads the first 500 seconds. Is there a way to have it download only the necessary portion of the video?
  • slhck
    slhck over 7 years
    @user779159 Usually, if you put the -ss before -i, ffmpeg will skip to the requested part without decoding first. But with web downloads, I don't think it's feasible. The problem is: there is no direct relationship between a file offset (in bytes) and the corresponding time, so ffmpeg has to download the entire file first before it can figure out where to skip (in terms of byte offset) for a certain timestamp. Or it has to decode until the point that was requested.
  • slhck
    slhck over 7 years
    With HTTP adaptive streaming video, this may be easier, as the file is segmented and the metadata tells ffmpeg which segment to load, but I'm not sure if that is implemented either.
  • user779159
    user779159 over 7 years
    @slhck, I tried the -ss before -i and it does work much quicker. The speedup is surprising considering what you said about there being no direct relationship between file offset and time.
  • slhck
    slhck over 7 years
    @user779159 Now that you mention it, there's one thing. Maybe it works faster if the file has its streaming metadata (the so-called MOOV atom for MP4 files) at the beginning. This is not always the case, i.e. not every file has that metadata at the beginning, and I wasn't sure if ffmpeg would use it for web downloads. But I'm glad that it worked!