微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

iOS:在视频中裁剪视频奇怪的绿线左侧和底侧

– 如何删除视频中的绿线.
当播放视频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 );
                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整除.

检查这个discussion link

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

“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 1920×1080,an encoder appends 8 rows of black pixels to ht eimage array,to make the row count 1088.”

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐