Question

I am currently using:

some_fs = gridfs.GridFS(db, "some.col")
fs_file = some_fs.get(index)

to get a <class 'gridfs.grid_file.GridOut'> object.

How do I get a file object instead or how do I convert this to a python file object? Do I have to save as a temp file to do this?

Edit:

This is the full code I am using:

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

Output:

[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
Was it helpful?

Solution

Edit: Maybe the GridOut isn't a proper implementation of python file objects. My last suggestion is to try using a memory file with 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()

OTHER TIPS

Based on this documentation you need to use .read() method.

I think that some_fs.get(index).read() will give you what you need.

This worked for me

FFMPEG_BIN = "ffmpeg.exe"
some_fs = gridfs.GridFS(db, "some.col")
vid_id = ObjectId("5339e3b5b322631b544b2338")

vid_file = some_fs.get(vid_id)

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=sp.PIPE, stdout=sp.PIPE
)
pipe.stdin=vid_file.read()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top