Skip to content

Commit 1528103

Browse files
author
Shahen Hovhannisyan
committed
fix(Compress): Fixed compression
Fixed compression bug, this happens in some video dimensions closes out #39
1 parent 49e3815 commit 1528103

1 file changed

Lines changed: 36 additions & 19 deletions

File tree

ios/RNVideoProcessing/RNVideoTrimmer/SDAVAssetExportSession.m

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,14 @@ - (void)exportAsynchronouslyWithCompletionHandler:(void (^)())handler
129129
{
130130
[self.writer addInput:self.videoInput];
131131
}
132+
id pixelFormat = self.videoInputSettings[(id)kCVPixelBufferPixelFormatTypeKey];
133+
if (!pixelFormat) {
134+
pixelFormat = @(kCVPixelFormatType_32BGRA);
135+
}
132136
NSDictionary *pixelBufferAttributes = @
133137
{
134-
(id)kCVPixelBufferPixelFormatTypeKey: @(kCVPixelFormatType_32BGRA),
138+
(id)kCVPixelBufferPixelFormatTypeKey : pixelFormat,
139+
// (id)kCVPixelBufferPixelFormatTypeKey: @(kCVPixelFormatType_32BGRA),
135140
(id)kCVPixelBufferWidthKey: @(self.videoOutput.videoComposition.renderSize.width),
136141
(id)kCVPixelBufferHeightKey: @(self.videoOutput.videoComposition.renderSize.height),
137142
@"IOSurfaceOpenGLESTextureCompatibility": @YES,
@@ -308,32 +313,44 @@ - (AVMutableVideoComposition *)buildDefaultVideoComposition
308313
videoComposition.frameDuration = CMTimeMake(1, trackFrameRate);
309314
CGSize targetSize = CGSizeMake([self.videoSettings[AVVideoWidthKey] floatValue], [self.videoSettings[AVVideoHeightKey] floatValue]);
310315
CGSize naturalSize = [videoTrack naturalSize];
316+
311317
CGAffineTransform transform = videoTrack.preferredTransform;
318+
videoComposition.frameDuration = CMTimeMake(1, trackFrameRate);
319+
312320
CGFloat videoAngleInDegree = atan2(transform.b, transform.a) * 180 / M_PI;
313321
if (videoAngleInDegree == 90 || videoAngleInDegree == -90) {
314322
CGFloat width = naturalSize.width;
315323
naturalSize.width = naturalSize.height;
316324
naturalSize.height = width;
317325
}
318-
videoComposition.renderSize = naturalSize;
319-
// center inside
320-
{
321-
float ratio;
322-
float xratio = targetSize.width / naturalSize.width;
323-
float yratio = targetSize.height / naturalSize.height;
324-
ratio = MIN(xratio, yratio);
325-
326-
float postWidth = naturalSize.width * ratio;
327-
float postHeight = naturalSize.height * ratio;
328-
float transx = (targetSize.width - postWidth) / 2;
329-
float transy = (targetSize.height - postHeight) / 2;
330-
331-
CGAffineTransform matrix = CGAffineTransformMakeTranslation(transx / xratio, transy / yratio);
332-
matrix = CGAffineTransformScale(matrix, ratio / xratio, ratio / yratio);
333-
transform = CGAffineTransformConcat(transform, matrix);
334-
}
335326

336-
// Make a "pass through video track" video composition.
327+
if(transform.a == 0 && transform.b == 1.0 && transform.c == -1.0 && transform.d == 0) {
328+
// portrait, home button at bottom (upright)
329+
if( (transform.tx == 0) && (transform.ty == 0) ) {
330+
transform.tx = videoTrack.naturalSize.height;
331+
transform.ty = 0;
332+
}
333+
}
334+
else if(transform.a == 0 && transform.b == -1.0 && transform.c == 1.0 && transform.d == 0) {
335+
// portrait, home button at top (upside down)
336+
if( (transform.tx == 0) && (transform.ty == 0) ) {
337+
transform.tx = 0;
338+
transform.ty = videoTrack.naturalSize.width;
339+
}
340+
}
341+
else if(transform.a == 1.0 && transform.b == 0 && transform.c == 0 && transform.d == 1.0) {
342+
// landscape, home button on right
343+
// do nothing
344+
}
345+
else if(transform.a == -1.0 && transform.b == 0 && transform.c == 0 && transform.d == -1.0) {
346+
// landscape, home button on left
347+
if( (transform.tx == 0) && (transform.ty == 0) ) {
348+
transform.tx = videoTrack.naturalSize.width;
349+
transform.ty = videoTrack.naturalSize.height;
350+
}
351+
}
352+
///////
353+
videoComposition.renderSize = naturalSize;
337354
AVMutableVideoCompositionInstruction *passThroughInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
338355
passThroughInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, self.asset.duration);
339356

0 commit comments

Comments
 (0)