SilenceLove / HXPhotoPicker

图片/视频选择器 - 支持LivePhoto、GIF图片选择、3DTouch预览、在线下载iCloud上的资源、编辑图片/视频、浏览网络图片 功能 Imitation wx photo/image picker - support for LivePhoto, GIF image selection, 3DTouch preview, Download the resources on iCloud online, browse the web image function
https://github.com/SilenceLove/HXPhotoPicker
MIT License
2.99k stars 635 forks source link

Processing failed on some videos #560

Open AEM120 opened 11 months ago

AEM120 commented 11 months ago

Video File: https://we.tl/t-pAhUdQeXmG

Open Safari, paste the link and download the video. Go to Files. Save Video to Photos. Using the Demo try to edit the video by cropping "square for example" only Then hit finish "Processing failed"

simulator_screenshot_31B7FF9B-AE61-4DBB-B61B-F3B11C81D0B4

AEM120 commented 11 months ago

Some videos work fine, the video example that you have does not produce any issues. While the video example that I provided you have some issues when editing.

Example,

  1. select the video, and crop it to square. click finish, you get processing failed.
  2. select the video, and trim from the start. click finish, you get processing failed.

Please watch the following demo:

https://github.com/SilenceLove/HXPhotoPicker/assets/139702862/aee2d12b-c03f-463c-b4de-74e874b46360

SilenceLove commented 11 months ago

ok i will try to fix it

AEM120 commented 11 months ago

I been using different instruments to see where is the error. But it seems something to do with the EditorVideoCompositor. I also found out that some videos have full cpu usage when processing the edit

Screenshot 2023-07-17 at 6 43 01 AM Screenshot 2023-07-17 at 6 43 21 AM
SilenceLove commented 11 months ago

I found the problem, this is just a temporary solution

EditorVideoTool.swift

func insertAudioTrack(
        duration: CMTime,
        timeRang: CMTimeRange,
        audioTracks: [AVAssetTrack]
    ) throws 

Modify as follows

var audioInputParams: [AVMutableAudioMixInputParameters] = []
for audioTrack in audioTracks {
    guard let track = mixComposition.addMutableTrack(withMediaType: .audio, preferredTrackID: kCMPersistentTrackID_Invalid) else {
        continue
    }
    let audioTimeRange: CMTimeRange = .init(start: .zero, duration: .init(seconds: audioTrack.timeRange.duration.seconds - 0.5, preferredTimescale: audioTrack.timeRange.duration.timescale))
    try track.insertTimeRange(audioTimeRange, of: audioTrack, at: .zero)
    track.preferredTransform = audioTrack.preferredTransform
    let audioInputParam = AVMutableAudioMixInputParameters(track: track)
    audioInputParam.setVolumeRamp(fromStartVolume: factor.volume, toEndVolume: factor.volume, timeRange: audioTimeRange)
    audioInputParam.trackID = track.trackID
    audioInputParams.append(audioInputParam)
}