【问题标题】:How to stream video from different files?如何从不同的文件流式传输视频?
【发布时间】:2014-08-26 19:18:41
【问题描述】:

我有一个包含视频的远程文件夹。视频正在不断上传中。

例如:

[1].avi
[2].avi
[3].avi
[4].avi

我希望能够尽可能接近实时地流式传输这些视频(使用 rtsp 或类似的东西)

在这种情况下,我可以开始流式传输 2,然后是 3,依此类推...并检查文件夹是否有更新。

我已经完成了一个 python 脚本,它允许我使用 OpenCV (cv2) 一个接一个地重现所有这些视频,而不会注意到它们之间的变化。但我仍然不知道如何制作流媒体。

import numpy as np
import collections
import cv
import os


__FRAMES_PER_SECOND = 10.0

def find_between( s, first, last ):
    try:
        start = s.index( first ) + len( first )
        end = s.index( last, start )
        return s[start:end]
    except ValueError:
        return ""

def play(file):
    vidFile = cv.CaptureFromFile(file)
    nFrames = int(  cv.GetCaptureProperty( vidFile, cv.CV_CAP_PROP_FRAME_COUNT ) )
    fps = cv.GetCaptureProperty( vidFile, cv.CV_CAP_PROP_FPS )
    waitPerFrameInMillisec = int( 1/__FRAMES_PER_SECOND * 1000/1 )
    for f in xrange( nFrames ):
      frameImg = cv.QueryFrame( vidFile )
      cv.ShowImage( "My Video Window",  frameImg )
      cv.WaitKey( waitPerFrameInMillisec  )



directory = "vids/"
dictFiles = {}
arrFiles = []
for root, dirs, files in os.walk(directory):
    #print root
    #print dirs
    arrFiles = files    
for file in arrFiles:
    number = find_between(file, "[", "]")
    #print "----------",file
    dictFiles[number] = file

orderedDictFiles = collections.OrderedDict(sorted(dictFiles.items()))
for file in orderedDictFiles:
    play(directory + orderedDictFiles[file])

【问题讨论】:

    标签: python node.js streaming video-streaming live-streaming


    【解决方案1】:

    如果您使用的是 Linux,则可以使用 pyinotify 3rd 方模块在上传视频时播放视频。

    当视频文件完全上传时,以下代码将打印"video complete: <pathname>"。可以修改播放视频文件。

    来源

    import os, sys
    
    import pyinotify
    
    class VideoComplete(pyinotify.ProcessEvent):
        def process_IN_CLOSE_WRITE(self, event):
            sys.stdout.write(
                'video complete: {}\n'.format(event.pathname)
            )
            sys.stdout.flush()
    
    def main():
        wm = pyinotify.WatchManager()
        notifier = pyinotify.Notifier(
            wm, default_proc_fun=VideoComplete(),
            )
        mask = pyinotify.ALL_EVENTS
        path = os.path.expanduser('~/Downloads/incoming')
        wm.add_watch(path, mask, rec=True, auto_add=True)
        notifier.loop()
    
    if __name__=='__main__':
        main()
    

    输出

    video complete: /home/johnm/Downloads/incoming/a.txt
    

    【讨论】:

    • 优秀。有没有办法为网络做到这一点?
    • @JuanmaCrescente:上面的代码可以将 URL 推送到连接到用户浏览器的 WebSocket。然后浏览器将获取并播放视频 URL。有点工作,但看起来很酷:)
    猜你喜欢
    • 2015-08-22
    • 2010-10-25
    • 1970-01-01
    • 1970-01-01
    • 2016-07-28
    • 1970-01-01
    • 2015-06-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多