保存流,而使用LibVLC玩它 [英] Saving a stream while playing it using LibVLC

查看:1782
本文介绍了保存流,而使用LibVLC玩它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 LibVLC ,我试图挽救流同时播放它。这是Python code:

Using LibVLC, I'm trying to save a stream while playing it. This is the 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 is 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

它的视频流保存到一个文件 example.mpg 。按<一个href=\"http://wiki.videolan.org/Documentation%3aStreaming_HowTo/Receive_and_Save_a_Stream#Save_a_stream_with_VLC\">this文档的命令来保存流是这样的:

It saves the video stream to a file example.mpg. As per this doc, the command to save a stream is this :

--sout=file/ps:example.mpg

我已经创建的实例时使用 vlc.Instance

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

但问题是,它的的保存流,它不同时播放的流

But the problem is that it only saves the stream, it doesn't play the stream simultaneously.

有什么办法(以LibVLC)我可以保存流(本地文件),同时支付了吗?

虽然,我正在寻找在的Python 3.3.1 一个解决方案,但它是好的,如果有任何C或C ++的解决方案。

Although, I'm looking for a solution in Python 3.3.1 but it is fine if there is any C or C++ solution.

我创建了一个类似的,但不能复制,<一个href=\"http://stackoverflow.com/questions/16506504/writing-video-file-and-simultaneously-playing-it\">topic昨天

推荐答案

其基本思想是很简单的。您可以复制的输出流,并将其重定向到一个文件。这样做,因为 Maresh 正确地指出,使用 SOUT =#重复{...} 指令。

Idea:

The basic idea is simple enough. You have to duplicate the output stream and redirect it to a file. This is done, as Maresh correctly pointed out, using the sout=#duplicate{...} directive.

以下解决方案适用于我的机器™。我测试过它在Ubuntu 12.10使用VLC V2.0.3(TwoFlower)和Python 2.7.1。我想这也应该在Python 3的工作,因为最繁重的工作是由libVlc反正做的。

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.

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 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

有用的链接


  • 命令行帮助是必不可少的破译的VLC多如牛毛
    命令行选项。

  • VLC流方法文档的第3章。解释了流输出,其指令的结构和各种可用模块的描述。 第4章显示了一些例子。

  • 如果你想改变媒体选项在 LibVLC API文档
    运行

  • Helpful Links

    • The Command-Line help was essential to decipher the plethora of VLCs command line options.
    • Chapter 3 of VLC streaming HowTo. Explains the structure of the stream output, its directives and describes of the various available modules. Chapter 4 shows some examples.
    • LibVLC API documentation in case you want to change media option at runtime
    • 以上code不与YouTube发挥好。我四处搜查,发现一个额外的跨code 指令可用于YouTube的视频流转换为常规视频格式。我用#跨code {V codeC = MP4V,一个codeC = MPGA,VB = 800,AB = 128,逐行扫描}

      The above code doesn't play nice with YouTube. I searched around and discovered that an additional transcode directive can be used to convert YouTube's video stream to a regular video format. I used #transcode{vcodec=mp4v,acodec=mpga,vb=800,ab=128,deinterlace}


      • V codeC = MP4V是要带code视频格式(MP4V是MPEG-4,mpgv是MPEG-1,并且还有H263,DIV1,DIV2,DIV3,I420, I422,I444,RV24,YUY2)。

      • 一个codeC = MPGA是要带code音频格式(MPGA是MPEG音频层2,A52是A52即AC3声音)。

      • VB = 800是千比特/秒的视频比特率。

      • AB = 128是千比特/秒的音频比特率。

      • 反交错告诉VLC反交错动态视频。

      更新code是这样的:

      The updated code looks like 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 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
      

      一对夫妇的要点:

      我使用MPEG音频和视频codeCS在跨code指令。它似乎用于输出文件的匹配的扩展(MPG在这种情况下)是重要的。否则VLC打开保存的文件进行播放时,就会犯糊涂。记住这一点,如果你决定要切换到另一种视频格式。

      A couple of important points:

      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.

      您不能添加普通YouTube URL作为文件路径。相反,你必须指定视频本身的位置。这就是为什么我使用的文件路径看起来很神秘的原因。这文件路径对应的视频在 http://www.youtube.com/watch?v=GQe3JxJHpxQ 。 VLC本身能够提取从给定的URL的YouTube视频的位置,但libVLC不这样做开箱。你必须写自己的解析器来做到这一点。 看到这个相关的SO质疑。我遵循这一方针手动解决视频位置我的测试。

      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 http://www.youtube.com/watch?v=GQe3JxJHpxQ. 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. See this related SO question. I followed this approach to manually resolve the video location for my tests.

      这篇关于保存流,而使用LibVLC玩它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆