Latest 1.2
Homepage https://github.com/Insfgg99x/FGVideoEditor
License MIT
Platforms ios 8.0, requires ARC
Dependencies SnapKit, FGHUD, FGToolKit
Frameworks Foundation, UIKit, AVFoundation, CoreMedia, Photos
Authors

FGVideoEditor

🎉🚀📅🌎👍🎉

史上首款跟微信朋友圈视频裁剪相似的视频裁剪开源工具

Version
License
Platform
Language

  • [x]视频时长裁剪处理
  • [x]视频裁剪UI
  • [x]视频裁剪预览

Feathures

FGVideoEditor
FGVideoEditor
FGVideoEditor
FGVideoEditor
FGVideoEditor
FGVideoEditor
FGVideoEditor
FGVideoEditor

GIF演示

FGVideoEditor

观看视频演示
Vedio

使用 Usage

微信样式视频裁剪只需要简单的使用(Simply use follow interface to crop a video in WeChat-style-lkie)

public convenience init(max duration:CGFloat, vedio url:URL, finishHandler:((FGVideoPreViewController, FGVideoInfo) -> ())?)

或者只裁剪不要UI(or just using crop function)

public func cropVideo(url: URL, cropRange:CMTimeRange, completion:((_ newUrl: URL, _ newDuration:CGFloat,_ result:Bool) -> ())?)

或者只用微信样式的视频裁剪UI(or just WeChat-style video croping UI)

public convenience init(frame: CGRect, url:URL, imgw:CGFloat, maxduration:CGFloat)
var cropStart:CGFloat {get} //获取当前裁剪区域的左边界对应的时间(相对于视频开始播放的位置为0s)
var cropDuration:CGFloat {get} //获取当前裁剪区域对应的时长
var cropRange:CMTimeRange {get} //当前裁剪区域的CMTime范围
var cropWidth:CGFloat {get} //当前裁剪区域的宽度
var slidingBeginHandler:(() -> ())? {get, set} //开始滑动视频裁剪区域的左/右边界的回调
var slidingHandler:((FGSlideDirection) -> ())? {get, set} //滑动视频裁剪区域的左/右边界的回调
var slidingEndHandler:(() -> ())? {get, set} //结束左/右边界的滑动
]var contentDidScrollHandler:(() -> ())? {get, set} //滑动视频帧图片横向列表的回调
var dragWillBeginHandler:(() -> ())? {get, set} //将要滑动视频帧图片横向列表的回调
var dragDidEndHandler:(() -> ())? {get, set} //结束滑动视频帧图片横向列表的回调

你可以使用shouldSaveCropedVideoToPhotoLibrary来控制裁剪视频后是否自动保存至相册,默认是true (save croped video to photo library. default is true, set false to disable auto save to photo library)

示例(Example)

需要先选取一个视频

let picker = UIImagePickerController.init()
picker.sourceType = .photoLibrary
picker.mediaTypes = [kUTTypeMovie as String]
picker.delegate = wkself
picker.allowsEditing = false
present(picker, animated: true, completion: nil)

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    guard let url = info[UIImagePickerControllerMediaURL] as? URL else {
        picker.dismiss(animated: true, completion: nil)
        showHUD(.error("获取不到资源"))
        return
    }
    crop(video: url)
    picker.dismiss(animated: true, completion: nil)
}

利用预览视图裁剪,内置最小最大时长判断(croping and ui)

private func crop(video url:URL) {
    weak var wkself = self
    let preview = FGVideoPreViewController.init(max: 10, vedio: url) { (edit, info) in
        wkself?.cropedUrl = info.url
        wkself?.navigationController?.popViewController(animated: true)
        wkself?.playCropedVideo()
    }
    navigationController?.pushViewController(preview, animated: true)
}

private func playCropedVideo() {
    guard let url = cropedUrl else {
        return
    }
    pickBtn.isHidden = true

    player = AVPlayer.init(url: url)
    previewLayer = AVPlayerLayer.init(player: player)
    previewLayer?.backgroundColor = UIColor.clear.cgColor
    previewLayer?.videoGravity = .resizeAspectFill
    previewLayer?.frame = view.bounds
    view.layer.insertSublayer(previewLayer!, at: 0)
    player?.play()
}

仅使用视频裁剪能力(croping only)

FGVideoEditor.shared.cropVideo(url: url, cropRange: range, completion: { (newUrl, newDuration, result) in
    guard result else {
        self.showHUD(.error("剪切失败"))
        return
    }
    print("裁剪成功,裁剪后的路径:(newUrl),时长:%.1f",newDuration)
})

仅使用视频裁剪UI(ui only)

let editFrame = CGRect.init(x: 50, y: screenheight - 100, width: screenwidth - 100, height: 50)
slider = FGVideoEditSliderView.init(frame: editFrame, url: url, imgw: imgw, maxduration: 10)
view.addSubview(slider)
weak var wkself = self
slider.slidingBeginHandler = {
    wkself?.player?.pause()
    wkself?.playing = false
}
let tolerance = CMTimeMake(1, timesclae)
slider.slidingHandler = { (direction) in
    let range = (wkself ?? self).slider.cropRange
    wkself?.currentRange = range
    if direction == .left {
        wkself?.player?.seek(to: range.end)
    } else {
        wkself?.player?.seek(to: range.start,
                             toleranceBefore: tolerance,
                             toleranceAfter: tolerance)
        wkself?.player?.seek(to: range.start)
    }
}
slider.slidingEndHandler = {
    wkself?.player?.seek(to: (wkself ?? self).currentRange.start)
    wkself?.player?.play()
    wkself?.playing = true
}
slider.contentDidScrollHandler = {
    let range = (wkself ?? self).slider.cropRange
    wkself?.currentRange = range
    wkself?.player?.seek(to: range.start)
}
slider.dragWillBeginHandler = {
    wkself?.player?.pause()
    wkself?.playing = false
}
slider.dragDidEndHandler = {
    wkself?.player?.play()
    wkself?.playing = true
}

安装 Installation

pod "FGVideoEditor", "~>1.2"
import FGVideoEditor

若手动安装,请添加依赖:"SnapKit", "FGHUD", "~>2.4", "pod "FGToolKit", "~>2.0"

环境 Required

  • [x] Xocde 9
  • [x] Swift 4.x

万水千山总是情,给个star行不行
欢迎pull request,欢迎isuue!

Latest podspec

{
    "name": "FGVideoEditor",
    "version": "1.2",
    "summary": "FGVideoEditor convience video edit toolkit",
    "homepage": "https://github.com/Insfgg99x/FGVideoEditor",
    "license": "MIT",
    "authors": {
        "CGPointZero": "[email protected]"
    },
    "source": {
        "git": "https://github.com/Insfgg99x/FGVideoEditor.git",
        "tag": "1.2"
    },
    "frameworks": [
        "Foundation",
        "UIKit",
        "AVFoundation",
        "CoreMedia",
        "Photos"
    ],
    "platforms": {
        "ios": "8.0"
    },
    "source_files": "FGVideoEditor/FGVideoEditor/*.swift",
    "requires_arc": true,
    "dependencies": {
        "SnapKit": [],
        "FGHUD": [
            "2.4"
        ],
        "FGToolKit": []
    }
}

Pin It on Pinterest

Share This