我目前正在使用:
some_fs = gridfs.GridFS(db, "some.col")
fs_file = some_fs.get(index)
得到一个<class 'gridfs.grid_file.GridOut'>
对象。
如何获取文件对象,或者如何将其转换为python文件对象?为此,我是否需要另存为临时文件?
编辑:
这是我正在使用的完整代码:
FFMPEG_BIN = "ffmpeg.exe"
some_fs = gridfs.GridFS(db, "some.col")
vid_id = ObjectId("5339e3b5b322631b544b2338")
vid_file = some_fs.get(vid_id)
raw = vid_file.read()
print type(vid_file), type(raw)
with open(raw, "rb") as infile:
pipe = sp.Popen([FFMPEG_BIN,
# "-v", "quiet",
"-y",
"-i", "-",
"-vcodec", "copy", "-acodec", "copy",
"-ss", "00:00:00", "-t", "00:00:10", "-sn",
"test.mp4" ]
,stdin=infile, stdout=sp.PIPE
)
pipe.wait()
输出:
[2014-03-31 19:03:00] Connected to DB.
<class 'gridfs.grid_file.GridOut'> <type 'str'>
Traceback (most recent call last):
File "C:/dev/proj/src/lib/ffmpeg/win/test.py", line 19, in <module>
with open(raw, "rb") as infile:
TypeError: file() argument 1 must be encoded string without NULL bytes, not str
编辑:也许GridOut
不是python文件对象的正确实现。我的最后建议是尝试将内存文件与StringIO一起使用。
import StringIO
FFMPEG_BIN = "ffmpeg.exe"
some_fs = gridfs.GridFS(db, "some.col")
vid_id = ObjectId("5339e3b5b322631b544b2338")
vid_file = some_fs.get(vid_id)
# Should be a proper file-like object
infile = StringIO.StringIO(vid_file.read())
pipe = sp.Popen([FFMPEG_BIN,
# "-v", "quiet",
"-y",
"-i", "-",
"-vcodec", "copy", "-acodec", "copy",
"-ss", "00:00:00", "-t", "00:00:10", "-sn",
"test.mp4" ]
,stdin=infile, stdout=sp.PIPE
)
pipe.wait()
...
infile.close()
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句