1

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

3 Answers 3

2

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()
Sign up to request clarification or add additional context in comments.

4 Comments

infile is not set to anything... what should I use as input?
That gives me: TypeError: must be convertible to a buffer, not GridOut
@Jeff, it looks like GridOut isn't a proper implementation of python file-like objects. Try creating a memory file to read from. I've updated my answer.
That gives: File "C:\Python27\lib\subprocess.py", line 665, in init errread, errwrite) = self._get_handles(stdin, stdout, stderr) File "C:\Python27\lib\subprocess.py", line 772, in _get_handles p2cread = msvcrt.get_osfhandle(stdin.fileno()) AttributeError: StringIO instance has no attribute 'fileno'
0

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

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

2 Comments

@Jeff can you please tell what kind of string is there and what do you expect to be in a file? It sounds like your <class ...> is actually a fileobject and read() gives you what is inside of that file.
I edited my question... I hope this doesn't change my question too much!
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.