Skip to content

Commit

Permalink
fix(ios): Fix trim calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
dragermrb committed Mar 27, 2022
1 parent 38e42ad commit 2a7d129
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 25 deletions.
44 changes: 32 additions & 12 deletions ios/Plugin/TrimSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ enum TrimSettingsError: Error {

public class TrimSettings: NSObject
{
private var startsAt: Double = 0;
private var endsAt: Double = 0;

init(startsAt: Double, endsAt: Double) throws
private var startsAt: CLong = 0;
private var endsAt: CLong = 0;

/**
* startsAt: startsAt in miliSeconds
* endsAt: endsAt in miliSeconds
*/
init(startsAt: CLong, endsAt: CLong) throws
{
if (startsAt < 0)
{
Expand All @@ -22,27 +26,43 @@ public class TrimSettings: NSObject
self.startsAt = startsAt;
self.endsAt = endsAt;
}

func getStartsAt()->Double

/**
* Get startsAt in miliSeconds
* @return startsAt in miliSeconds
*/
func getStartsAt()->CLong
{
return self.startsAt;
}

func setStartsAt(_ startsAt: Double) throws

/**
* Set startsAt in miliSeconds
* startsAt: startsAt in miliSeconds
*/
func setStartsAt(_ startsAt: CLong) throws
{
if (startsAt < 0)
{
throw TrimSettingsError.invalidArgument(message: "Parameter startsAt cannot be negative");
}
self.startsAt = startsAt;
}

func getEndsAt()->Double

/**
* Get endsAt in miliSeconds
* @return endsAt in miliSeconds
*/
func getEndsAt()->CLong
{
return self.endsAt;
}

func setEndsAt(_ endsAt: Double) throws

/**
* Set endsAt in miliSeconds
* endsAt: endsAt in miliSeconds
*/
func setEndsAt(_ endsAt: CLong) throws
{
if (endsAt < 0)
{
Expand Down
4 changes: 2 additions & 2 deletions ios/Plugin/VideoEditor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ import UIKit
let targetVideoSize:CGSize = calculateTargetVideoSize(sourceVideoSize: mediaSize, transcodeSettings: transcodeSettings);

// Trim
let start = CMTimeMakeWithSeconds(Float64(trimSettings.getStartsAt() * 1000), preferredTimescale: 1)
let start = CMTimeMake(value: Int64(trimSettings.getStartsAt()), timescale: 1000)
let end = trimSettings.getEndsAt() > 0
? CMTimeMakeWithSeconds(Float64(trimSettings.getEndsAt() * 1000), preferredTimescale: 1)
? CMTimeMake(value: Int64(trimSettings.getEndsAt()), timescale: 1000)
: avAsset.duration
let duration = min((end - start), (avAsset.duration - start))
let range = CMTimeRangeMake(start: start, duration: duration)
Expand Down
4 changes: 2 additions & 2 deletions ios/Plugin/VideoEditorPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public class VideoEditorPlugin: CAPPlugin {

do {
let trimSettings = try TrimSettings(
startsAt: (trim["startsAt"] ?? 0.0) as! Double,
endsAt: (trim["endsAt"] ?? 0.0) as! Double
startsAt: (trim["startsAt"] ?? 0) as! CLong,
endsAt: (trim["endsAt"] ?? 0) as! CLong
);

let transcodeSettings = try TranscodeSettings(
Expand Down
28 changes: 19 additions & 9 deletions src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,25 @@ export interface VideoEditorPlugin {

export interface EditOptions {
path: string;
trim?: {
startsAt?: number;
endsAt?: number;
};
transcode?: {
height?: number;
width?: number;
keepAspectRatio?: boolean;
};
trim?: TrimOptions;
transcode?: TranscodeOptions;
}

export interface TrimOptions {
/**
* StartsAt in milliseconds
*/
startsAt?: number;
/**
* EndsAt in milliseconds
*/
endsAt?: number;
}

export interface TranscodeOptions {
height?: number;
width?: number;
keepAspectRatio?: boolean;
}

export interface ThumbnailOptions {
Expand Down

0 comments on commit 2a7d129

Please sign in to comment.