opencv_dart icon indicating copy to clipboard operation
opencv_dart copied to clipboard

VideoWriter from file results in 0 bytes file

Open letisoft opened this issue 3 months ago • 3 comments

VideoWriter.fromFile seems not working?

following code: ` Future<String> processVideo(String filePath,String destFolder) async{ cv.VideoCapture cap = cv.VideoCapture.fromFile(filePath); double width = cap.get(cv.CAP_PROP_FRAME_WIDTH); double height = cap.get(cv.CAP_PROP_FRAME_HEIGHT); double fps = cap.get(cv.CAP_PROP_FPS);

String newFilePath = PathUtils.join(destFolder,"processed_${PathUtils.basename(filePath)}");
File f = File(newFilePath);

if(!f.existsSync()){
  f.createSync(recursive: true);
}

cv.VideoWriter writer = cv.VideoWriter.fromFile(newFilePath,"MP4V", fps, (width.toInt(),height.toInt()));

while(cap.isOpened){
  var (success, frame) = cap.read();
  if(!success){
    break;
  }

  if(frame.isEmpty){
    print("empty frame");
  }
  else {
    await writer.writeAsync(frame);
  }
}

writer.release();

if(f.existsSync()){
  int len = await f.length();
  print(len);
}

return newFilePath;

} ` file should not be empty, I have checked that await writer.writeAsync(frame); is called and frames are not empty

Android 14

Any idea?

letisoft avatar Sep 23 '25 08:09 letisoft

@letisoft Which version of opencv_dart and Flutter do you use? I need more info to reproduce.

Also, for android, you should be careful to permissions, check whether the newFilePath is valid.

rainyl avatar Sep 23 '25 13:09 rainyl

Hello,

thanks for your quick response, really appreciated!

afeter setting log level I found the issue and the solution:

  1. VideoWriter.fromFile MP4V is not supported on android, so far so good, the problem is that this will not give any signes it failed, unless you enable logging
  2. I found the following workaround: VideoWriter.fromFile(newFilePath,"MP4V" if(!writer.isOpened){ writer.open(newFilePath,"MJPG", fps, (width.toInt(),height.toInt())); }

so I have sorted it, and it have anything to do with dart port

letisoft avatar Sep 23 '25 19:09 letisoft

All right, glad that you solved it.

But we have nothing to do to inform users about supported fourcc, opencv c++ doesn't provide an API to get it.

Will add if opencv itself support it.

rainyl avatar Sep 24 '25 00:09 rainyl