Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>Idea:</h2> <p>The basic idea is simple enough. You have to duplicate the output stream and redirect it to a file. This is done, as <a href="https://stackoverflow.com/a/16758264/961781">Maresh</a> correctly pointed out, using the <strong>sout=#duplicate{...}</strong> directive.</p> <h2>Working Solution:</h2> <p>The following solution works on my machine ™. I've tested it on Ubuntu 12.10 with VLC v2.0.3 (TwoFlower) and Python 2.7.1. I think it should also work on Python 3 since most of the heavy lifting is done by libVlc anyway.</p> <pre><code>import os import sys import vlc if __name__ == '__main__': #filepath = &lt;either-some-url-or-local-path&gt; movie = os.path.expanduser(filepath) if 'http://' not in filepath: if not os.access(movie, os.R_OK): print ( 'Error: %s file is not readable' % movie ) sys.exit(1) instance = vlc.Instance("--sout=#duplicate{dst=file{dst=example.mpg},dst=display}") try: media = instance.media_new(movie) except NameError: print ('NameError: % (%s vs Libvlc %s)' % (sys.exc_info()[1], vlc.__version__, vlc.libvlc_get_version())) sys.exit(1) player = instance.media_player_new() player.set_media(media) player.play() #dont exit! while(1): continue </code></pre> <h2>Helpful Links</h2> <ul> <li>The <a href="http://wiki.videolan.org/VLC_command-line_help" rel="nofollow noreferrer">Command-Line help</a> was essential to decipher the plethora of VLCs command line options.</li> <li><a href="http://www.videolan.org/doc/streaming-howto/en/ch03.html" rel="nofollow noreferrer">Chapter 3 of VLC streaming HowTo</a>. Explains the structure of the stream output, its directives and describes of the various available modules. <a href="http://www.videolan.org/doc/streaming-howto/en/ch04.html" rel="nofollow noreferrer">Chapter 4</a> shows some examples.</li> <li><a href="http://www.videolan.org/developers/vlc/doc/doxygen/html/group__libvlc__media.html" rel="nofollow noreferrer">LibVLC API documentation</a> in case you want to change media option at runtime</li> </ul> <hr> <h1>Update - Saving YouTube videos:</h1> <p>The above code doesn't play nice with YouTube. I searched around and discovered that an additional <code>transcode</code> directive can be used to convert YouTube's video stream to a regular video format. I used <code>#transcode{vcodec=mp4v,acodec=mpga,vb=800,ab=128,deinterlace}</code></p> <ul> <li>vcodec=mp4v is the video format you want to encode in (mp4v is MPEG-4, mpgv is MPEG-1, and there is also h263, DIV1, DIV2, DIV3, I420, I422, I444, RV24, YUY2).</li> <li>acodec=mpga is the audio format you want to encode in (mpga is MPEG audio layer 2, a52 is A52 i.e. AC3 sound).</li> <li>vb=800 is the video bitrate in Kbit/s.</li> <li>ab=128 is the audio bitrate in Kbit/s.</li> <li>deinterlace tells VLC to deinterlace the video on the fly.</li> </ul> <p>The updated code looks like this:</p> <pre><code>import os import sys import vlc if __name__ == '__main__': #filepath = &lt;either-some-url-or-local-path&gt; filepath = "http://r1---sn-nfpnnjvh-1gil.c.youtube.com/videoplayback?source=youtube&amp;newshard=yes&amp;fexp=936100%2C906397%2C928201%2C929117%2C929123%2C929121%2C929915%2C929906%2C929907%2C929125%2C929127%2C925714%2C929917%2C929919%2C912512%2C912515%2C912521%2C906838%2C904485%2C906840%2C931913%2C904830%2C919373%2C933701%2C904122%2C932216%2C936303%2C909421%2C912711%2C907228%2C935000&amp;sver=3&amp;expire=1373237257&amp;mt=1373214031&amp;mv=m&amp;ratebypass=yes&amp;id=1907b7271247a714&amp;ms=au&amp;ipbits=48&amp;sparams=cp%2Cid%2Cip%2Cipbits%2Citag%2Cratebypass%2Csource%2Cupn%2Cexpire&amp;itag=45&amp;key=yt1&amp;ip=2a02%3A120b%3Ac3c6%3A7190%3A6823%3Af2d%3A732c%3A3577&amp;upn=z3zzcrvPC0U&amp;cp=U0hWSFJOVV9KUUNONl9KSFlDOmt4Y3dEWFo3dDFu&amp;signature=D6049FD7CD5FBD2CC6CD4D60411EE492AA0E9A77.5D0562CCF4E10A6CC53B62AAFFF6CB3BB0BA91C0" movie = os.path.expanduser(filepath) savedcopy = "yt-stream.mpg" if 'http://' not in filepath: if not os.access(movie, os.R_OK): print ( 'Error: %s file is not readable' % movie ) sys.exit(1) instance = vlc.Instance("--sout=#transcode{vcodec=mp4v,acodec=mpga,vb=800,ab=128,deinterlace}:duplicate{dst=file{dst=%s},dst=display}" % savedcopy) try: media = instance.media_new(movie) except NameError: print ('NameError: % (%s vs Libvlc %s)' % (sys.exc_info()[1], vlc.__version__, vlc.libvlc_get_version())) sys.exit(1) player = instance.media_player_new() player.set_media(media) player.play() #dont exit! while(1): continue </code></pre> <h2>A couple of important points:</h2> <p>I've used MPEG audio and video codecs in the transcode directive. It seems to be important to use a matching extensions for the output file (mpg in this case). Otherwise VLC gets confused when opening the saved file for playback. Keep that in mind if you decide to switch to another video format.</p> <p>You cannot add a regular YouTube URL as filepath. Instead you have to specify the location of the video itself. That's the reason why the filepath that I've used looks so cryptic. That filepath corresponds to video at <code>http://www.youtube.com/watch?v=GQe3JxJHpxQ</code>. VLC itself is able to extract the video location from a given YouTube URL, but libVLC doesn't do that out of the box. You'll have to write your own resolver to do that. <a href="https://stackoverflow.com/questions/11436864/vlc-python-doesnt-play-youtube-video">See this related SO question</a>. I <a href="http://www.youtube.com/watch?v=GQe3JxJHpxQ" rel="nofollow noreferrer">followed this approach</a> to manually resolve the video location for my tests.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload