sbis04 / video_trimmer

Flutter video trimmer package
https://pub.dev/packages/video_trimmer
MIT License
434 stars 263 forks source link

Unhandled Exception: PlatformException(VideoError, Failed to load video: Cannot Open, null, null) #200

Closed heyallieson closed 1 year ago

heyallieson commented 1 year ago

Hey @sbis04! Thanks for making this awesome package. I'm excited about the opportunities video_trimmer offers! I just started to use it but when I try to load a video, I hit an error that isn't too descriptive: 'Unhandled Exception: PlatformException(VideoError, Failed to load video: Cannot Open, null, null).'

I'm hoping you can help. It may be due to the file structure I'm passing. I'm using a package called photo_manager instead of file_picker as you have shown in the example. It provides an Asset Entity so I converted it into a File like this:

var now = DateTime.now();
final tempDir = await getTemporaryDirectory();
File file = File('${tempDir.path}/$now');
file.writeAsBytesSync(snapshot.data!);

The file it creates is something like this path. I thought it could have been due to the suffix, so I tried with a .mp4 and .mov too.: File: '/var/mobile/Containers/Data/Application/C6CBC30B-39CE-4120-AA1F-358D477BFDD8/Library/Caches/2023-03-09 10:44:19.049726'

Then like normal, I navigate to VideoTrim(file: file). My VideoTrim file is pretty simple as I'm just trying to load the video. Here it is just to show:

class VideoTrim extends StatefulWidget {
  final File file;
  const VideoTrim({super.key, required this.file});

  @override
  State<VideoTrim> createState() => _VideoTrimState();
}

class _VideoTrimState extends State<VideoTrim> {
  final Trimmer _trimmer = Trimmer();

  void _loadVideo() {
    _trimmer.loadVideo(videoFile: widget.file);
  }

  @override
  void initState() {
    super.initState();
    _loadVideo();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Builder(
        builder: (context) => SizedBox(
          height: 316.0,
          child: VideoViewer(trimmer: _trimmer),
        ),
      ),
    );
  }
}

Thanks so much for your extra eyes. Thanks so much for creating this package. I'm really excited about it!

heyallieson commented 1 year ago

Resolution: Found the fix but not sure why. I figured out a way to get a file path and convert that instead. Thanks.

ChristineWasike commented 1 year ago

Hey, @heyallieson noticing that you faced the same exact issue I had. I'm also using the photo_manager package. Can you walk me through at what point you made the path conversation and how you did it? I'd really appreciate it. Thanks!

heyallieson commented 1 year ago

hey @ChristineWasike! It's been a bit, so let me know if any of the details seem off.

I used the photo_manager package to display images and videos in a grid. Then I got the asset's data with snapshot.data and got the media url using .getMediaUrl(); This returns the asset's path.

FutureBuilder(
  future: index.thumbnailDataWithSize(),
  builder: ((context,snapshot) {
     if (asset == AssetType.video) {
         var asset = snapshot.data
         var mediaSelected = asset.getMediaUrl();
         return Widget
     }
   })
 )

I wanted to compress it before going to the video trimmer, so I compressed it and then sent that path to the video trimmer. I also found this reduced any weird coloring the video trimmer showed when the video wasn't compressed.

Hope that helps!