Writing video file and simultaneously playing it

2019-02-24 02:49发布

问题:

In my fun project, I'm downloading video file from youtube, and writing to a file on local disk. Simultaneously I want to play it. The objective is to cache the file on local disk, so that when I want to see the video again, the app can play it locally, thereby saving bandwidth.

I'm using Python 3.3.1, PyQt4/Phonon and LibVLC. So far, I'm able to do the following things:

  • Given a youtube watch url, I can download the video file and then play it using both PyQt4/Phonon and LibVLC, independently. It is not streaming.

  • Since LibVLC supports streaming, I'm able to play the given url through streaming.

The second is very close to what I want to do, but since it doesn't save the file on disk, next time I cannot play the same video locally.

I'm looking for some guidelines as to how to proceed from here. In particular, how to play a video from an incomplete file which is still being written into.

I'm completely fine with any API (that does the job) as long as it is:

  • Python 3.3.1 (preferably)
  • C
  • C++.

And I'm looking for alternative approaches also, if my current approach is not correct or makes the problem more difficult than it actually is.

回答1:

VLC supports playback of incomplete files, so if you're up for a bit of non-blocking I/O and/or parallel code, you should be able to start the download and after a sufficient amount has been written, use LibVLC to start playback. Depending on what compression algorithm is used, you may need to buffer enough so that there's always several seconds of data left in the buffer -- if I recall correctly, some of the more modern algorithms record deltas and index information going forward and backward.

You may get a few warnings / error messages / Exceptions, but I would not assume that they're fatal -- let the playback quality be your guide!

This is somewhat similar to some of the suggestions from the comments above, and is also related to a lot of what @abarnert said, to a lesser extent some of the exchange with @StackedCrooked.