Shiv
Shiv

Reputation: 1

part of the video from gridfs mongodb in java?

I have uploaded a video file .mp4(18MB) into gridfs . and trying to read it from java code .here are some points i am unable to move further

1) i can able to retrieve the whole video into byte array and able to play

2) for first Nbytes means starting from first chunk to n no of chunks also i can able to play using directly querying from fs.chunks ... as below and giving to servletOutputstream ..

DBCollection a= db.getCollection("fs.chunks");DBCursor cur1=a.find().limit(10);
    System.out.println(cur1);
    byte[] destination2 =new byte[2621440];
    int length2 = 0;
    while(cur1.hasNext()) {
        byte[] b2 = (byte[]) cur1.next().get("data");
        System.arraycopy(b2, 0, destination2, length2, b2.length);
        length2 += b2.length;
        System.out.println("##########");
        System.out.println(destination2.length);
    }

3) I was stuck here, while reading from middle of the chunks , means after skip(n) chunks in the find() operation , unable to play the video by windows media player.saying unable to codec and etc error.. am i trying in a right way ?

DBCollection a= db.getCollection("fs.chunks");
DBCursor cur1=a.find(new BasicDBObject("n",new BasicDBObject("$gt",9))).limit(10);
    System.out.println(cur1);
    byte[] destination2 =new byte[2621440];
    int length2 = 0;
    while(cur1.hasNext()) {
        byte[] b2 = (byte[]) cur1.next().get("data");
        System.arraycopy(b2, 0, destination2, length2, b2.length);
        length2 += b2.length;
        System.out.println("##########");
        System.out.println(destination2.length);
    }

...........

public void showVideos(Model model,HttpServletResponse response) throws IOException {............response.setHeader("Content-Type", "video/quicktime");
    response.setHeader("Content-Disposition", "inline; filename=\"" + filename + "\"");//byte[] bytearray =destination2 
    //response.s
    ServletOutputStream out = response.getOutputStream();
    System.out.println("hello");
    int n=0;
        //while(is.read(bytes, 0, 4096) != -1)
        {
        System.out.println(n++);
            out.write(bytearray);
            }

please suggest me for retrieving the part of the video file and play it from grid fs?

Upvotes: 0

Views: 716

Answers (1)

TeTeT
TeTeT

Reputation: 2094

I'd use the GridFS classes for this purpose. Pseudo code below. myFS points to the bucket and findOne looks for the id of the file.

    GridFS myFS = null;
    if (bucket.isPresent()) {
        myFS = new GridFS(m.getDb(), bucket.get());
    } else {
        myFS = new GridFS(m.getDb());
    }
    return Optional.fromNullable(myFS.findOne(id));

Upvotes: 0

Related Questions