rFlex / SCRecorder

iOS camera engine with Vine-like tap to record, animatable filters, slow motion, segments editing
Apache License 2.0
3.06k stars 583 forks source link

Cannot get watermarking to work #306

Closed jln19 closed 8 years ago

jln19 commented 8 years ago

Couldn't find any documentation or a working example of watermarking videos. The below is what I could piece together. Can you tell me what is wrong with this? The video gets exported, but the watermark never appears. The watermark is a valid .png file (my_watermark@2x.png) in the app bundle.

AVAsset *asset = self.recordSession.assetRepresentingSegments;
    NSURL *tempUrl = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:@"videomarked.mov"]];

    SCAssetExportSession *assetExportSession = [[SCAssetExportSession alloc] initWithAsset:asset];
    assetExportSession.outputUrl = tempUrl;
    assetExportSession.outputFileType = AVFileTypeMPEG4;
    assetExportSession.videoConfiguration.preset = SCPresetMediumQuality;
    assetExportSession.videoConfiguration.watermarkImage = [UIImage imageNamed:@"my_watermark"];
    assetExportSession.videoConfiguration.watermarkFrame = CGRectMake(0, 0, 320, 480);
    [assetExportSession exportAsynchronouslyWithCompletionHandler: ^{
        if (assetExportSession.error == nil) {
            // I get success here, but the video does not have a watermark

        } else {
            // Something bad happened

        }
    }];

I've confirmed that generatedWatermarkImage is valid in SCAssetExportSession:_buildWatermarkFilterForVideoSize

rFlex commented 8 years ago

Do you have a small project that you can share so I can investigate? That would be so awesome :)

jln19 commented 8 years ago

I will try to whip one up. In the meantime though can you confirm that watermarking has worked for you?

jln19 commented 8 years ago

@rFlex Here is the simple project showing that watermarking doesn't work, at least from what I can tell. Thanks for investigating :)

SCRecorderWatermarkTest.zip

Klucherev commented 8 years ago

@rFlex Hi! I think problem is in - (SCFilter *)_generateRenderingFilterForVideoSize:(CGSize)videoSize method. if (renderingFilter.isEmpty) { renderingFilter = nil; } alway returns nil if no custom filter is setted ignoring watermark

rFlex commented 8 years ago

Thanks a lot for the help!. @Klucherev yeah good call, that was what the problem was :).

jln19 commented 8 years ago

@rFlex I tried making this update. Now there is a crash at line 317 of SCFilter.m. -[CIImage imageByCompositingOverImage:]: unrecognized selector sent to instance 0x14648c40

Try running the test project with your commit added.

rFlex commented 8 years ago

Are you running on iOS 7?

jln19 commented 8 years ago

Yes, that device was iOS7. I tested again on iOS9 and didn't get the crash, which makes sense. However, why does adding the watermark change the orientation of the video? before after

rFlex commented 8 years ago

This is another issue where your input video has a orientation metadata that isn't being respected by SCRecorder. You may be able to fix that by setting keepInputAffineTransform to false in SCVideoConfiguration

jln19 commented 8 years ago
`assetExportSession.videoConfiguration.keepInputAffineTransform = NO;`

results in this:

img_7943

This is the desired result: desired

rFlex commented 8 years ago

Ok, I will check it out some time today

jln19 commented 8 years ago

@rFlex any updates on this? I've invested a lot of time implementing SCRecorder as a solution to our video recording/playback needs but if watermarking doesn't work I can't use it... and explain to my company why I have to start over :(

rFlex commented 8 years ago

SCRecorderWatermarkTest 3.zip

Here is a working example based of your project.

jln19 commented 8 years ago

Thanks. What did you change? ViewController.m looks the same. Did you make changes in the SCRecorder files?

rFlex commented 8 years ago

I added a videoComposition that will take care of transforming your video so the output video doesn't have any special rotation metadata.

jln19 commented 8 years ago

I see. Thank you.

UlanNurmatov commented 4 years ago

It seems that I can't add watermark without adding a filter. How do I add a watermark without using a filter?

Here's my code:

guard self.asset != nil else { return }
            let assetExportSession = SCAssetExportSession.init(asset: self.asset)
            assetExportSession.delegate = self
            assetExportSession.inputAsset = self.asset
            assetExportSession.contextType = SCContextType.auto
            assetExportSession.outputUrl = self.recordSession.outputUrl
            assetExportSession.outputFileType = AVFileType.mp4.rawValue
            assetExportSession.videoConfiguration.filter = SCFilter.init(ciFilterName: "CIPhotoEffectFade")
            assetExportSession.videoConfiguration.watermarkImage = UIImage(named: "twitter")
            assetExportSession.videoConfiguration.watermarkFrame = CGRect(x: 0, y: 0, width: 200, height: 40)
            assetExportSession.videoConfiguration.preset = SCPresetHighestQuality
            assetExportSession.exportAsynchronously {
                if assetExportSession.error == nil {
                    self.saveVideo(withURL: assetExportSession.outputUrl!)
                }
            }