Skip to content

Commit

Permalink
Added minFreeDiskSpaceLimit config. Fixed recordingSizeLimit. Example…
Browse files Browse the repository at this point in the history
…ViewController cleanup.
  • Loading branch information
NikKovIos committed Mar 18, 2021
1 parent 9bbe7ca commit 23158e1
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 24 deletions.
12 changes: 9 additions & 3 deletions Source/Configuration/YPImagePickerConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,15 @@ public struct YPConfigVideo {
/// Default is 60 seconds.
public var recordingTimeLimit: TimeInterval = 60.0

/// Defines the size limit in bytes for recording videos
/// For example: 1 GB
public var recordingSizeLimit: Double?
/// Defines the size limit in bytes for recording videos.
/// If this property is not nil, then the recording percentage line tracks buy this.
/// In bytes. 100000000 is 100 MB.
/// AVCaptureMovieFileOutput.maxRecordedFileSize.
public var recordingSizeLimit: Int64?

/// Minimum free space when recording videos.
/// AVCaptureMovieFileOutput.minFreeDiskSpaceLimit.
public var minFreeDiskSpaceLimit: Int64 = 1024 * 1024

/// Defines the time limit for videos from the library.
/// Defaults to 60 seconds.
Expand Down
6 changes: 2 additions & 4 deletions Source/Helpers/YPVideoProcessor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,11 @@ class YPVideoProcessor {
composition.addMutableTrack(withMediaType: .video, preferredTrackID: kCMPersistentTrackID_Invalid)

// Prevent crash if tracks is empty
if asset.tracks.isEmpty {
guard asset.tracks.isEmpty == false,
let clipVideoTrack = asset.tracks(withMediaType: .video).first else {
return
}

// input clip
let clipVideoTrack = asset.tracks(withMediaType: .video)[0]

// make it square
let videoComposition = AVMutableVideoComposition()
if YPConfig.onlySquareImagesFromCamera {
Expand Down
24 changes: 15 additions & 9 deletions Source/Pages/Video/YPVideoCaptureHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ class YPVideoCaptureHelper: NSObject {
public func startRecording() {

let outputURL = YPVideoProcessor.makeVideoPathURL(temporaryFolder: true, fileName: "recordedVideoRAW")

checkOrientation { [weak self] orientation in
guard let strongSelf = self else {
return
Expand Down Expand Up @@ -227,8 +227,12 @@ class YPVideoCaptureHelper: NSObject {
let maxDuration =
CMTimeMakeWithSeconds(self.videoRecordingTimeLimit, preferredTimescale: timeScale)
videoOutput.maxRecordedDuration = maxDuration
videoOutput.minFreeDiskSpaceLimit = 1024 * 1024
if YPConfig.video.fileType == .mp4 {
if let sizeLimit = YPConfig.video.recordingSizeLimit {
videoOutput.maxRecordedFileSize = sizeLimit
}
videoOutput.minFreeDiskSpaceLimit = YPConfig.video.minFreeDiskSpaceLimit
if YPConfig.video.fileType == .mp4,
YPConfig.video.recordingSizeLimit != nil {
videoOutput.movieFragmentInterval = .invalid // Allows audio for MP4s over 10 seconds.
}
if session.canAddOutput(videoOutput) {
Expand All @@ -244,18 +248,16 @@ class YPVideoCaptureHelper: NSObject {

@objc
func tick() {
let timeElapsed = Date().timeIntervalSince(self.dateVideoStarted)
var progress: Float = 0.0
let timeElapsed = Date().timeIntervalSince(dateVideoStarted)
var progress: Float
if let recordingSizeLimit = YPConfig.video.recordingSizeLimit {
progress = Float(Double(self.outputURL.fileSize) / recordingSizeLimit)
progress = Float(videoOutput.recordedFileSize) / Float(recordingSizeLimit)
} else {
progress = Float(timeElapsed) / Float(videoRecordingTimeLimit)
}
// VideoOutput configuration is responsible for stopping the recording. Not here.
DispatchQueue.main.async {
self.videoRecordingProgress?(progress, timeElapsed)
if progress > 0.99 {
self.stopRecording()
}
}
}

Expand Down Expand Up @@ -315,6 +317,10 @@ extension YPVideoCaptureHelper: AVCaptureFileOutputRecordingDelegate {
didFinishRecordingTo outputFileURL: URL,
from connections: [AVCaptureConnection],
error: Error?) {
if let error = error {
print(error)
}

if YPConfig.onlySquareImagesFromCamera {
YPVideoProcessor.cropToSquare(filePath: outputFileURL) { [weak self] url in
guard let _self = self, let u = url else { return }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class ExampleViewController: UIViewController {
/* Uncomment and play around with the configuration 👨‍🔬 🚀 */

/* Set this to true if you want to force the library output to be a squared image. Defaults to false */
// config.library.onlySquare = true
// config.library.onlySquare = true

/* Set this to true if you want to force the camera output to be a squared image. Defaults to true */
// config.onlySquareImagesFromCamera = false
Expand All @@ -89,17 +89,20 @@ class ExampleViewController: UIViewController {
// config.showsFilters = false

/* Manage filters by yourself */
// config.filters = [YPFilter(name: "Mono", coreImageFilterName: "CIPhotoEffectMono"),
// YPFilter(name: "Normal", coreImageFilterName: "")]
// config.filters.remove(at: 1)
// config.filters.insert(YPFilter(name: "Blur", coreImageFilterName: "CIBoxBlur"), at: 1)
// config.filters = [YPFilter(name: "Mono", coreImageFilterName: "CIPhotoEffectMono"),
// YPFilter(name: "Normal", coreImageFilterName: "")]
// config.filters.remove(at: 1)
// config.filters.insert(YPFilter(name: "Blur", coreImageFilterName: "CIBoxBlur"), at: 1)

/* Enables you to opt out from saving new (or old but filtered) images to the
user's photo library. Defaults to true. */
config.shouldSaveNewPicturesToAlbum = false

/* Choose the videoCompression. Defaults to AVAssetExportPresetHighestQuality */
config.video.compression = AVAssetExportPresetMediumQuality
config.video.compression = AVAssetExportPresetPassthrough

/* Choose the recordingSizeLimit. If not setted, then limit is by time. */
// config.video.recordingSizeLimit = 10000000

/* Defines the name of the album when saving pictures in the user's photo library.
In general that would be your App name. Defaults to "DefaultYPImagePickerAlbumName" */
Expand All @@ -114,7 +117,7 @@ class ExampleViewController: UIViewController {
config.screens = [.library, .photo, .video]

/* Can forbid the items with very big height with this property */
// config.library.minWidthForItem = UIScreen.main.bounds.width * 0.8
// config.library.minWidthForItem = UIScreen.main.bounds.width * 0.8

/* Defines the time limit for recording videos.
Default is 30 seconds. */
Expand Down Expand Up @@ -149,7 +152,7 @@ class ExampleViewController: UIViewController {

/* Disable scroll to change between mode */
// config.isScrollToChangeModesEnabled = false
// config.library.minNumberOfItems = 2
// config.library.minNumberOfItems = 2

/* Skip selection gallery after multiple selections */
// config.library.skipSelectionsGallery = true
Expand Down

0 comments on commit 23158e1

Please sign in to comment.