c++ - Saving a stream while playing it using LibVLC -


using libvlc, i'm trying save stream while playing it. python code:

import os import sys import vlc  if __name__ == '__main__':     filepath = <either-some-url-or-local-path>     movie = os.path.expanduser(filepath)     if 'http://' not in filepath:         if not os.access(movie, os.r_ok):             print ( 'error: %s file not readable' % movie )             sys.exit(1)     instance = vlc.instance("--sub-source marq --sout=file/ps:example.mpg")     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 

it saves video stream file example.mpg. per this doc, command save stream :

--sout=file/ps:example.mpg 

which i've using when creating instance of vlc.instance:

instance = vlc.instance("--sub-source marq --sout=file/ps:example.mpg") 

but problem only saves stream, doesn't play stream simultaneously.

is there way (in libvlc) can save stream (to local file) while paying it?

although, i'm looking solution in python 3.3.1 fine if there c or c++ solution.


i've created similar, not duplicate, topic yesterday.

idea:

the basic idea simple enough. have duplicate output stream , redirect file. done, maresh correctly pointed out, using sout=#duplicate{...} directive.

working solution:

the following solution works on machine ™. i've tested on ubuntu 12.10 vlc v2.0.3 (twoflower) , python 2.7.1. think should work on python 3 since of heavy lifting done libvlc anyway.

import os import sys import vlc  if __name__ == '__main__':     #filepath = <either-some-url-or-local-path>     movie = os.path.expanduser(filepath)     if 'http://' not in filepath:         if not os.access(movie, os.r_ok):             print ( 'error: %s file 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 

helpful links


update - saving youtube videos:

the above code doesn't play nice youtube. searched around , discovered additional transcode directive can used convert youtube's video stream regular video format. used #transcode{vcodec=mp4v,acodec=mpga,vb=800,ab=128,deinterlace}

  • vcodec=mp4v video format want encode in (mp4v mpeg-4, mpgv mpeg-1, , there h263, div1, div2, div3, i420, i422, i444, rv24, yuy2).
  • acodec=mpga audio format want encode in (mpga mpeg audio layer 2, a52 a52 i.e. ac3 sound).
  • vb=800 video bitrate in kbit/s.
  • ab=128 audio bitrate in kbit/s.
  • deinterlace tells vlc deinterlace video on fly.

the updated code looks this:

import os import sys import vlc  if __name__ == '__main__':     #filepath = <either-some-url-or-local-path>     filepath = "http://r1---sn-nfpnnjvh-1gil.c.youtube.com/videoplayback?source=youtube&newshard=yes&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&sver=3&expire=1373237257&mt=1373214031&mv=m&ratebypass=yes&id=1907b7271247a714&ms=au&ipbits=48&sparams=cp%2cid%2cip%2cipbits%2citag%2cratebypass%2csource%2cupn%2cexpire&itag=45&key=yt1&ip=2a02%3a120b%3ac3c6%3a7190%3a6823%3af2d%3a732c%3a3577&upn=z3zzcrvpc0u&cp=u0hwsfjovv9kuunonl9ksfldomt4y3dewfo3ddfu&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 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 

a couple of important points:

i've used mpeg audio , video codecs in transcode directive. seems important use matching extensions output file (mpg in case). otherwise vlc gets confused when opening saved file playback. keep in mind if decide switch video format.

you cannot add regular youtube url filepath. instead have specify location of video itself. that's reason why filepath i've used looks cryptic. filepath corresponds video @ http://www.youtube.com/watch?v=gqe3jxjhpxq. vlc able extract video location given youtube url, libvlc doesn't out of box. you'll have write own resolver that. see related question. followed approach manually resolve video location tests.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -