iOS:在视频中裁剪视频奇怪的绿线左侧和底侧 [英] iOS : Crop video weird green line left and bottom side in video

查看:207
本文介绍了iOS:在视频中裁剪视频奇怪的绿线左侧和底侧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

- 如何删除视频中的绿线。
当裁剪视频时,此时2或3次显示绿色或混合绿色 - 红色闪烁线在视频中,左侧或视频中的底部或左侧和底部。

-How to remove green line on the video. when crop a video 2 or 3 times at that time show green or mix green-red blink line in the video, left or bottom or both left and bottom side in the video.

视频裁剪方法。

-(void)cropButton
{
        CGRect cropFrame = self.cropView.croppedImageFrame;

        //load our movie Asset
        AVAsset *asset;
            asset = [AVAsset assetWithURL:[NSURL fileURLWithPath:[self.videoDataArr objectAtIndex:self.selectedIndex-1]]];

        //create an avassetrack with our asset
        AVAssetTrack *clipVideoTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];

        //create a video composition and preset some settings
        AVMutableVideoComposition* videoComposition = [AVMutableVideoComposition videoComposition];
        videoComposition.frameDuration = CMTimeMake(1, 30);

        //create a video instruction
        AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
        instruction.timeRange = CMTimeRangeMake(kCMTimeZero, asset.duration);

        AVMutableVideoCompositionLayerInstruction* transformer = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:clipVideoTrack];

        UIImageOrientation videoOrientation = [self getVideoOrientationFromAsset:asset];

        CGAffineTransform t1 = CGAffineTransformIdentity;
        CGAffineTransform t2 = CGAffineTransformIdentity;

        switch (videoOrientation)
        {
            case UIImageOrientationUp:
                t1 = CGAffineTransformMakeTranslation(clipVideoTrack.naturalSize.height - cropFrame.origin.x, 0 - cropFrame.origin.y);
                t2 = CGAffineTransformRotate(t1, M_PI_2);
                break;
            case UIImageOrientationDown:
                t1 = CGAffineTransformMakeTranslation(0 - cropFrame.origin.x, clipVideoTrack.naturalSize.width - cropFrame.origin.y ); // not fixed width is the real height in upside down
                t2 = CGAffineTransformRotate(t1, - M_PI_2);

                break;
            case UIImageOrientationRight:
                t1 = CGAffineTransformMakeTranslation(0 - cropFrame.origin.x, 0 - cropFrame.origin.y);
                t2 = CGAffineTransformRotate(t1, 0 );
                break;
            case UIImageOrientationLeft:
                t1 = CGAffineTransformMakeTranslation(clipVideoTrack.naturalSize.width - cropFrame.origin.x, clipVideoTrack.naturalSize.height -  cropFrame.origin.y );
                t2 = CGAffineTransformRotate(t1, M_PI);
                break;
            default:
                NSLog(@"no supported orientation has been found in this video");
                break;
        }

        CGAffineTransform finalTransform = t2;
        videoComposition.renderSize = CGSizeMake(cropFrame.size.width,cropFrame.size.height);

        [transformer setTransform:finalTransform atTime:kCMTimeZero];

        //add the transformer layer instructions, then add to video composition
        instruction.layerInstructions = [NSArray arrayWithObject:transformer];
        videoComposition.instructions = [NSArray arrayWithObject: instruction];

        //Create an Export Path to store the cropped video
        NSString * documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        __block NSString *exportPath = [documentsPath stringByAppendingFormat:@"/CroppedVideo.mp4"];
        NSURL *exportUrl = [NSURL fileURLWithPath:exportPath];

        //Remove any prevouis videos at that path
        [[NSFileManager defaultManager]  removeItemAtURL:exportUrl error:nil];
        AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetHighestQuality] ;
        exporter.videoComposition = videoComposition;
        exporter.outputURL = exportUrl;
        NSLog(@"exported url : %@",exportUrl);
        exporter.outputFileType = AVFileTypeQuickTimeMovie;

        [exporter exportAsynchronouslyWithCompletionHandler:^
         {
             dispatch_async(dispatch_get_main_queue(), ^{
                 switch ([exporter status]) {
                     case  AVAssetExportSessionStatusCompleted:
                     {
                         self.navigationController.toolbarHidden = YES;
                         NSError *error = nil;
                         NSString *targetPath;
                             targetPath = [self.videoDataArr objectAtIndex:self.selectedIndex-1];

                         [FILEMANAGER removeItemAtPath:targetPath error:&error];
                         if(error)
                         {
                             NSLog(@"Error is : %@",error);
                         }
                         error = nil;
                         [FILEMANAGER moveItemAtPath:exportPath toPath:targetPath error:&error];
                         if(error)
                         {
                             NSLog(@"Error is : %@",error);
                         }
                         self.mySAVideoRangeSlider.videoUrl = self.videourl;
                         [self.mySAVideoRangeSlider getMovieFrame];

                     }
                         break;
                  }
                     case AVAssetExportSessionStatusFailed:
                         NSLog(@"Export failed: %@", [[exporter error] localizedDescription]);
                         break;
                     case AVAssetExportSessionStatusCancelled:
                         NSLog(@"Export canceled");
                         break;
                     default:
                         NSLog(@"NONE");
                         dispatch_async(dispatch_get_main_queue(), ^{
                         });
                         break;
                 }
             });
         }];
    }

- 视频裁剪后看到的视频裁剪绿线,如何解决它。

推荐答案

您的视频渲染宽度应均匀或可被4整除。

Your video rendersize width should be even or divisible by 4.

检查此讨论链接


请注意。如果您选择的分辨率不能被16,8或4整除,则最终可能会在帧的底部或右侧出现1px绿色边框。我已经看到了这个问题

Be aware. If you choose a resolution which is not divisible by 16, 8 or 4, you might end up with a 1px green border at either bottom or right side of your frame. I have seen this problem with

如果水平或垂直尺寸不能被16整除,那么编码器会用适当数量的黑色悬垂样本填充图像在解码时丢弃这些样本。例如,当在1920x1080编码HDTV时,编码器将8行黑色像素附加到ht eimage阵列,使行数为1088.

"If the horizontal or vertical size is not divisible by 16, then the encoder pads the image with a suitable number of black "overhang" samples at the right edge or bottom edge. These samples are discarded upon decoding. For example when coding HDTV at 1920x1080, an encoder appends 8 rows of black pixels to ht eimage array, to make the row count 1088."

这篇关于iOS:在视频中裁剪视频奇怪的绿线左侧和底侧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆