我试图在将文件读入内存后利用VLC python模块播放文件。我有以下代码将有效的视频文件读入内存。我现在需要直接从内存中播放视频。
import vlc
File1 = open('vid.webm','rb')
Axel = File1.read()
现在我需要播放Axel中的内容,我该怎么做。
答案 0 :(得分:1)
您可以参考下面的代码。
# -*- coding: utf-8 -*-
import ctypes
import io
import sys
import time
import vlc
MediaOpenCb = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_void_p, ctypes.POINTER(ctypes.c_void_p), ctypes.POINTER(ctypes.c_uint64))
MediaReadCb = ctypes.CFUNCTYPE(ctypes.c_ssize_t, ctypes.c_void_p, ctypes.POINTER(ctypes.c_char), ctypes.c_size_t)
MediaSeekCb = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_void_p, ctypes.c_uint64)
MediaCloseCb = ctypes.CFUNCTYPE(ctypes.c_void_p, ctypes.c_void_p)
def media_open_cb(opaque, data_pointer, size_pointer):
data_pointer.contents.value = opaque
size_pointer.contents.value = sys.maxsize
return 0
def media_read_cb(opaque, buffer, length):
stream=ctypes.cast(opaque,ctypes.POINTER(ctypes.py_object)).contents.value
new_data = stream.read(length)
for i in range(len(new_data)):
buffer[i]=new_data[i]
return len(new_data)
def media_seek_cb(opaque, offset):
stream=ctypes.cast(opaque,ctypes.POINTER(ctypes.py_object)).contents.value
stream.seek(offset)
return 0
def media_close_cb(opaque):
stream=ctypes.cast(opaque,ctypes.POINTER(ctypes.py_object)).contents.value
stream.close()
callbacks = {
'open': MediaOpenCb(media_open_cb),
'read': MediaReadCb(media_read_cb),
'seek': MediaSeekCb(media_seek_cb),
'close': MediaCloseCb(media_close_cb)
}
def MediaPlayerTimeChanged(event, userData):
print event.u.new_time
def main(stream):
instance = vlc.Instance()
player = instance.media_player_new()
vlc_events = player.event_manager()
vlc_events.event_attach(vlc.EventType.MediaPlayerTimeChanged, MediaPlayerTimeChanged, None)
media = instance.media_new_callbacks(callbacks['open'], callbacks['read'], callbacks['seek'], callbacks['close'], ctypes.cast(ctypes.pointer(ctypes.py_object(stream)), ctypes.c_void_p))
player.set_media(media)
player.play()
while True:
time.sleep(1)
if __name__ == '__main__':
try:
path = "d:/1.mp4"
stream = open(path, 'rb')
main(stream)
except IndexError:
print('Usage: {0} <path>'.format(__file__))
sys.exit(1)