上云无忧 > 文档中心 > 百度智能云短视频 SDK IOS开发 - 字幕
短视频 SDK
百度智能云短视频 SDK IOS开发 - 字幕

文档简介:
创建字幕轨道: 创建字幕轨道,使用字幕相关功能,需要创建字幕轨并添加到媒体轨道中心,并创建一个字幕片段,配合字幕UI逻辑使用,详见智能小视频源码,涉及组件BDHKVlogSubtitlesView(字幕位置),BDMVSubtitleInputAccessoryView(字幕输入框),BDMVInputEventBottomBar(字幕确认框)。
*此产品及展示信息均由百度智能云官方提供。免费试用 咨询热线:400-826-7010,为您提供专业的售前咨询,让您快速了解云产品,助您轻松上云! 微信咨询
  免费试用、价格特惠

创建字幕轨道

  • 创建字幕轨道,使用字幕相关功能,需要创建字幕轨并添加到媒体轨道中心,并创建一个字幕片段,配合字幕UI逻辑使用,详见智能小视频源码,涉及组件BDHKVlogSubtitlesView(字幕位置),BDMVSubtitleInputAccessoryView(字幕输入框),BDMVInputEventBottomBar(字幕确认框)
  • 代码示例如下:

//创建字幕轨道
		RMVPMediaVideoBlendItem *blendItem1 = [[RMVPMediaVideoBlendItem alloc]
 initWithBlendMode:RMVPMediaVideoBlendModeNormal];

        subtitleTrack = [[RMVPMediaVisibleTrack alloc] initWithBlendItem:blendItem1];

        subtitleTrack.trackId = kBDMVMediaVisibleSubtitleTrackId;

        [self.videoTrack addVideoTrack:subtitleTrack];
		//添加字幕片段
        RMVPMediaTextSegment *subtitleSegment = [[RMVPMediaTextSegment alloc] init];

        [subtitleTrack addSegment: subtitleSegment];


		//配置字幕与预览实时显示
		 CGFloat videoWidth = self.previewer.previewConfig.width;

   		 CGFloat videoHeight = self.previewer.previewConfig.height;

    	[self.videoTrack setupSubtitleStyleWithPreviewSize:CGSizeMake(videoWidth, videoHeight)];

添加多段字幕

  • 添加多段字幕,请参考智能小视频编辑预览BBAShortVideoPreviewViewController.mm文件中的字幕逻辑,字幕默认显示2S
  • 代码示例如下:
//获取字幕轨道中字幕片段
	RMVPMediaTextSegment *textSegment = [self.videoTrack subtitleSegment];
	
	//设置字幕内容,获取媒体轨道中心当前时间
    CMTime currentTime = self.previewer.cursor.cursorTime;


	//新增字幕
	if (!currentItem) {

        currentItem = [[RMVPMediaTextItem alloc] init];

        currentItem.mainText = self.tmpMainTitle;

        currentItem.subText = self.tmpSubTitle;

        RMVPMediaTextItem *nextTextItem = nil;

        NSArray *textAllItems = [textSegment textAllItem];

        for (RMVPMediaTextItem *tmpItem in textAllItems) {

            CMTimeRange timeRange = tmpItem.timeRange;
            if (CMTimeCompare(timeRange.start, currentTime) > 0) {
                nextTextItem = tmpItem;
                break;
            }
        }

        CGFloat newSubtitleDuration = [BDMVEditSubtitleSettings sharedInstance].addSubtitleDuration;

        if (newSubtitleDuration <= 0) newSubtitleDuration = 2;

        if (nextTextItem) {

            CMTime eventualDuration;

            CMTime nextItemStartTime = nextTextItem.timeRange.start;

            CMTime gapTime = CMTimeSubtract(nextItemStartTime, currentTime);

            if (CMTimeCompare(gapTime, CMTimeMake(newSubtitleDuration, 1)) >= 0) {
                eventualDuration = CMTimeMake(newSubtitleDuration, 1);
            } else {
                eventualDuration = gapTime;
            }

            currentItem.timeRange = CMTimeRangeMake(currentTime, eventualDuration);

            NSInteger index = [textSegment textIndexAtRefTime:nextItemStartTime];
			//插入字幕到指定位置
            [textSegment insertTextAtIndex:index item:currentItem];

            [self.subtitleEditManager.seekBarAera insertSubtitleAtIndex:index];

        } else {

            CMTime eventualDuration;

            CMTime mediaDuration = self.tracksCenter.userTrack.trackDuration;

            CMTime remainingDuration = CMTimeSubtract(mediaDuration, currentTime);

            if (CMTimeCompare(remainingDuration, CMTimeMake(newSubtitleDuration, 1)) >= 0) {

                eventualDuration = CMTimeMake(newSubtitleDuration, 1);

            } else {

                eventualDuration = remainingDuration;

            }

            currentItem.timeRange = CMTimeRangeMake(currentTime, eventualDuration);
			//追加字幕
            [textSegment addTextWithItem:currentItem];

            [self.subtitleEditManager.seekBarAera addSubtitle];

        }
  
        NSInteger index = [textSegment textIndexAtRefTime:self.previewer.cursor.cursorTime];
		//更新字幕显示位置,详见[3.3.4 设置字幕画面位置]
        [self updateSubtitleDisplayAndRealSubtitlePositonWithTransition:SubtitleInitTranslation itemIndex:index];

    } else { // 编辑字幕

        currentItem.mainText = self.tmpMainTitle;

        currentItem.subText = self.tmpSubTitle;

        NSInteger index = [textSegment textIndexAtRefTime:currentTime];

        [self.subtitleEditManager.seekBarAera editSubtitleAtIndex:index];
    }

设置字幕画面位置

  • 设置字幕画面位置,可以调整字幕轨道上某个字幕的显示位置
  • 代码示例如下:

//设置字幕显示位置
    currentFrame = self.displaySubtitleTextView.frame;//displayView的位置已经更新

    CGSize scale = [self currentVideoScale];

    BDMVPreviewTransform transform = self.subtitlePreviewTransform;

    CGFloat left = (CGRectGetMinX(currentFrame) - CGRectGetMinX(availabelRect) +
 Subtitle_InputView_OffsetX) / transform.scale;

    left += [self currentVideoInvisibleHorizontalPadding];

    CGFloat bottom = (CGRectGetMaxY(availabelRect) - CGRectGetMaxY(currentFrame) +
 Subtitle_InputView_OffsetX) / transform.scale;

    CGFloat right = (CGRectGetMaxX(availabelRect) - CGRectGetMaxX(currentFrame) 
+ Subtitle_InputView_OffsetX) / transform.scale;

    right += [self currentVideoInvisibleHorizontalPadding];

    RMVPMediaTextSegment *currentSegment = [self.tracksCenter subtitleSegment];

    NSDictionary *attributes = @{@"marginBottom":@(bottom * scale.height),

                                 @"marginLeft":@(left * scale.width),

                                 @"marginRight":@(right * scale.width)};

    [currentSegment updateStyleAtIndex:index styleAttributes:attributes];

相似文档
  • 编辑的相关接口在VideoProducer.framewrok中。 视频预览: 完成拍摄或选取本地视频,将实时预览窗口与创建的媒体轨道中心(MediaTrackCenter)连接,对已完成拍摄的视频或本地视频根据需要进行编辑,然后生成视频输出.mov格式的文件。
  • 录制的相关接口是在BDCloudAVStreamContext类里,包括采集预览(startPreview),录制(startRecording:),添加视频美颜(applyBeautyBaseVideoFx)等。 注意:百度云拍摄器SDK所有的类都是以“BDCloud”开头。
  • 百度云短视频创作专注移动端视音频场景研发,提供端到端的一站式视音频技术解决方案,不限于采集、录制、合成、上传、存储、分发,极大降低客户接入音视频产品的技术门槛。 运行环境:Android 4.1系统,API Level 16以上。
  • 百度云短视频产品(SDK)专注移动端视音频场景研发,提供端到端的一站式视音频技术解决方案,不限于采集、录制、合成、上传、存储、分发,极大降低客户接入音视频产品的技术门槛。
  • 请提交工单进行邀测申请,申请通过后,我们会有专人与您对接并将拍摄器SDK发送给您。 解压后将aar包放入项目libs目录: 在项目build.gradle添加库依赖: repositories { flatDir { dirs 'libs' } }
官方微信
联系客服
400-826-7010
7x24小时客服热线
分享
  • QQ好友
  • QQ空间
  • 微信
  • 微博
返回顶部