CarnegieTechnologies / gallery_saver

Flutter plugin that saves images and videos to devices gallery
Apache License 2.0
157 stars 316 forks source link

GallerySaver.saveVideo() fails when the video is encoded with x.265 on IOS #190

Open rlueders opened 1 year ago

rlueders commented 1 year ago

GallerySaver.saveVideo() silently fails when the video is encoded with x.265 on IOS. No errors in the log, but the call returns false. The same video encoded with x.264 works fine IOS and both 265 and 264 work fine in Android. Anyone run into this?

Ketul3012 commented 1 year ago

+1

xieenming commented 1 year ago

+1

This is not the problem of the third-party library ( GallerySaver ) we use, but a fact that iOS does not support hev1 codec tagged MP4. iOS supports hvc1 codec tagged MP4, only hvc1 tagged MP4 works in iOS 11 beta and above.

H.265 and HEVC (High Efficiency Video Coding) is the same thing.

When using ffmpeg to mux H.265/HEVC to MP4, we can use codec tag hvc1 or hev1 (or others), default when codec tag not set, it will use hev1.

hev1 and hvc1 are two codec tags, indicating different packaging methods of the HEVC stream in the MP4 container. Quicktime Player and iOS do not support hev1 tagged mp4.

hvc1 codec tagged MP4 info

hev1 codec tagged MP4 info

Reference: http://ffmpeg.org/pipermail/ffmpeg-devel/2017-June/212577.html https://stackoverflow.com/questions/32152090/encode-h265-to-hvc1-codec


So if a video is muxed from HEVC to MP4 with codec tag not being hvc1, when GallerySaver calls Swift code PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: url) to save the MP4 video file to iOS Gallery, it will throw an error : error Error? domain: "PHPhotosErrorDomain" - code: 3302 , see https://developer.apple.com/documentation/photokit/phphotoserror/code/invalidresource

error code 3302 means invalidResource : An error that indicates the asset resource validation fails.

2023-03-13 19 02 04

If you use the command mentioned in https://stackoverflow.com/questions/32152090/encode-h265-to-hvc1-codec ffmpeg -i input-hev1.mp4 -c:v copy -tag:v hvc1 -c:a copy output-hvc1.mp4 to convert your input-hev1.mp4 to output-hvc1.mp4, the output-hvc1.mp4 file will be able to be put in Gallery of iOS and be played with QuickTime Player.

2023-03-14 14 26 28