Skip to content

Commit

Permalink
feat: [CC-518] Support Video Trimming
Browse files Browse the repository at this point in the history
  • Loading branch information
will-ltk committed Jan 16, 2024
1 parent 6e9952b commit fa7083e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
14 changes: 8 additions & 6 deletions Source/Filters/Video/YPVideoFiltersVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,13 @@ open class YPVideoFiltersVC: UIViewController, IsMediaFilterVC {

// MARK: - Actions

private func completeSave(thumbnail:UIImage, videoUrl:URL, asset:PHAsset?) {
private func completeSave(thumbnail: UIImage, videoUrl: URL, asset: PHAsset?, startTime: CMTime?, endTime: CMTime?) {
guard let didSave = didSave else { return ypLog("Don't have saveCallback") }

let resultVideo = YPMediaVideo(thumbnail: thumbnail, videoURL: videoUrl, asset: asset)
resultVideo.cropRect = inputVideo.cropRect
resultVideo.startTime = startTime
resultVideo.endTime = endTime
didSave(YPMediaItem.video(v: resultVideo))
setupRightBarButtonItem()

Expand Down Expand Up @@ -290,9 +292,11 @@ open class YPVideoFiltersVC: UIViewController, IsMediaFilterVC {
view.isUserInteractionEnabled = false

// if the view is in cover image selection mode, just pass the asset straight through because it's not transforming the asset in any way
let startTime = trimmerView.startTime ?? CMTime.zero
let endTime = trimmerView.endTime ?? inputAsset.duration
if vcType == .Cover {
if let coverImage = self.coverImageView.image {
self.completeSave(thumbnail: coverImage, videoUrl: self.inputVideo.url, asset: self.inputVideo.asset)
self.completeSave(thumbnail: coverImage, videoUrl: self.inputVideo.url, asset: self.inputVideo.asset, startTime: startTime, endTime: endTime)
} else {
ypLog("YPVideoFiltersVC -> Don't have coverImage.")
self.resetView()
Expand All @@ -303,8 +307,6 @@ open class YPVideoFiltersVC: UIViewController, IsMediaFilterVC {

do {
let asset = AVURLAsset(url: inputVideo.url)
let startTime = trimmerView.startTime ?? CMTime.zero
let endTime = trimmerView.endTime ?? inputAsset.duration

// check if any trimming and cropping is involved - wc
let untrimmed = CMTimeCompare(startTime, CMTime.zero) == 0 && CMTimeCompare(endTime, inputAsset.duration) == 0
Expand All @@ -328,7 +330,7 @@ open class YPVideoFiltersVC: UIViewController, IsMediaFilterVC {
if untrimmed && !cropped && !rotated && !shouldMute {
// if video remains untrimmed and uncropped, use existing video url to eliminate video transcoding effort
// we will be selecting a cover image next, use generic uiimage for now
self.completeSave(thumbnail: self.coverImageView.image ?? UIImage(), videoUrl: self.inputVideo.url, asset: self.inputVideo.asset)
self.completeSave(thumbnail: self.coverImageView.image ?? UIImage(), videoUrl: self.inputVideo.url, asset: self.inputVideo.asset, startTime: startTime, endTime: endTime)

return
}
Expand All @@ -339,7 +341,7 @@ open class YPVideoFiltersVC: UIViewController, IsMediaFilterVC {
mediaManager.fetchVideoUrlAndCrop(for: inputVideo.asset!, cropRect: inputVideo.cropRect!, timeRange: timeRange, shouldMute: shouldMute) { [weak self] (url) in
DispatchQueue.main.async {
if let url = url {
self?.completeSave(thumbnail: self?.coverImageView.image ?? UIImage(), videoUrl: url, asset: self?.inputVideo.asset)
self?.completeSave(thumbnail: self?.coverImageView.image ?? UIImage(), videoUrl: url, asset: self?.inputVideo.asset, startTime: startTime, endTime: endTime)
} else {
ypLog("YPVideoFiltersVC -> Invalid asset url.")
self?.resetView()
Expand Down
2 changes: 2 additions & 0 deletions Source/Models/YPMediaItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public class YPMediaVideo {
// used to defer clipping and cropping until necessary
public var timeRange: CMTimeRange?
public var cropRect: CGRect?
public var startTime: CMTime?
public var endTime: CMTime?

public init(thumbnail: UIImage, videoURL: URL, fromCamera: Bool = false, asset: PHAsset? = nil) {
self.originalThumbnail = thumbnail
Expand Down

0 comments on commit fa7083e

Please sign in to comment.