【问题标题】:iOS : Crop video weird green line left and bottom side in videoiOS:在视频的左侧和底部裁剪视频奇怪的绿线
【发布时间】:2016-10-17 10:59:28
【问题描述】:

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

视频裁剪方法。

-(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;
                 }
             });
         }];
    }

-视频裁剪后出现绿线,如何解决。

【问题讨论】:

    标签: ios xcode video crop avassetexportsession


    【解决方案1】:

    您的视频渲染大小宽度应为偶数或可被 4 整除。

    检查这个discussion link

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

    "如果水平或垂直尺寸不能被 16 整除,则编码器在图像的右边缘或底部边缘填充适当数量的黑色“悬垂”样本。这些样本在解码时被丢弃。例如,当以 1920x1080 编码 HDTV,编码器将 8 行黑色像素附加到 ht eimage 数组,使行数为 1088。"

    【讨论】:

    • @El Captain 谢谢,这对我有帮助。
    • 我认为这已更改为偶数控制。在iOS11及以后的版本中,当你取render size时,宽度和高度均匀的问题已经解决了。
    猜你喜欢
    • 1970-01-01
    • 2011-09-06
    • 1970-01-01
    • 1970-01-01
    • 2014-09-08
    • 2014-11-05
    • 1970-01-01
    • 2012-01-13
    • 1970-01-01
    相关资源
    最近更新 更多