[iOS]录制或从相册选择视频

2024-08-20 22:58
文章标签 视频 选择 ios 录制 相册

本文主要是介绍[iOS]录制或从相册选择视频,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

写一个“添加视频”的功能
内容:
1.从相册选择视频
2.录制视频
3.将mov格式视频转为mp4
4.使用封装的SLPlayer播放器播放视频

GitHub:https://github.com/Gamin-fzym/GAVideoRecordDemo
Demo:https://download.csdn.net/download/u012881779/12116007

示意图:

详见demo,这里放部分代码水一下。

#import "HomeViewController.h"
#import "GSRecordVideoController.h"
#import "GSRecordEngine.h"
#import <CoreServices/CoreServices.h>
#import "UIViewController+NoSlideBack.h"
#import "GSPSVideoModel.h"@interface HomeViewController () <UITableViewDelegate, UITableViewDataSource, GSPSVideoCellDelegate, GSRecordVideoControllerDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate>@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) UIImagePickerController *moviePicker; // 视频选择器
@property (strong, nonatomic) GSRecordEngine *recordEngine;
@property (strong, nonatomic) GSPSVideoModel *videoModel;@end@implementation HomeViewController- (void)viewDidLoad {[super viewDidLoad];self.tableView.estimatedRowHeight = 100;self.tableView.rowHeight = UITableViewAutomaticDimension;[self.tableView registerNib:[UINib nibWithNibName:GSPSVideoCellIdentifier bundle:nil] forCellReuseIdentifier:GSPSVideoCellIdentifier];}- (IBAction)tapAddVideoAction:(id)sender {UIAlertController *alertCtrl = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];[alertCtrl addAction:[UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {[self presentViewController:self.moviePicker animated:YES completion:nil];}]];[alertCtrl addAction:[UIAlertAction actionWithTitle:@"拍摄" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {GSRecordVideoController *con = GSRecordVideoController.new;con.hidesBottomBarWhenPushed = YES;con.delegate = self;[self presentViewController:con animated:YES completion:nil];}]];[alertCtrl addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];[self presentViewController:alertCtrl animated:YES completion:nil];
}#pragma mark - 懒加载
- (GSRecordEngine *)recordEngine {if (_recordEngine == nil) {_recordEngine = [[GSRecordEngine alloc] init];}return _recordEngine;
}- (UIImagePickerController *)moviePicker {if (_moviePicker == nil) {_moviePicker = [[UIImagePickerController alloc] init];_moviePicker.delegate = self;[_moviePicker configNoSlideBack];_moviePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;_moviePicker.mediaTypes = @[(NSString *)kUTTypeMovie];_moviePicker.allowsEditing = YES;_moviePicker.videoMaximumDuration = GS_Video_Limit_Seconds;}return _moviePicker;
}#pragma mark - UITableViewDelegate, UITableViewDataSource- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {return 1;
}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {GSPSVideoCell *videoCell = [tableView dequeueReusableCellWithIdentifier:GSPSVideoCellIdentifier forIndexPath:indexPath];videoCell.videoModel = self.videoModel;videoCell.delegate = self;return videoCell;
}#pragma mark - GSPSVideoCellDelegate- (void)GSPSVideoCell_PlayClickWithPath:(NSString *)playPath {}#pragma mark - GSRecordVideoControllerDelegate
// 录像
- (void)handleWithRecordPath:(NSString *)recordPath withFirstImage:(UIImage *)firstImage withTotalTimeFormat:(NSString *)totalTimeFormat {GSPSVideoModel *videoModel = GSPSVideoModel.new;videoModel.videoLocalPath = recordPath;videoModel.videoFirstImg = firstImage;videoModel.videoTimeFormat = totalTimeFormat;self.videoModel = videoModel;[self.tableView reloadData];
}#pragma mark - UIImagePickerControllerDelegate
// 选择视频
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {if ([[info objectForKey:UIImagePickerControllerMediaType] isEqualToString:(NSString*)kUTTypeMovie]) {// 获取视频的名称NSString *videoPath = [NSString stringWithFormat:@"%@",[info objectForKey:UIImagePickerControllerMediaURL]];// 如果视频是mov格式的则转为MP4的if ([videoPath containsString:@".MOV"]) {NSURL *videoUrl = [info objectForKey:UIImagePickerControllerMediaURL];CGFloat timeSecs = [self getVideoDuration:videoUrl];NSString *timeFormat = [self Timeformat2FromSeconds:timeSecs];WEAKSELF[self.recordEngine changeMovToMp4:videoUrl dataBlock:^(UIImage *movieImage) {[weakSelf.moviePicker dismissViewControllerAnimated:YES completion:^{[self handleWithRecordPath:weakSelf.recordEngine.videoPath withFirstImage:movieImage withTotalTimeFormat:timeFormat];}];}];}}
}- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {[picker.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}#pragma mark - 其它
// 获取视频时间
- (CGFloat)getVideoDuration:(NSURL *)URL {NSDictionary *opts = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO]forKey:AVURLAssetPreferPreciseDurationAndTimingKey];AVURLAsset *urlAsset = [AVURLAsset URLAssetWithURL:URL options:opts];float second = 0;second = urlAsset.duration.value/urlAsset.duration.timescale;return second;
}// 获取视频 大小
- (NSInteger)getFileSize:(NSString *)path {NSFileManager * filemanager = [[NSFileManager alloc]init];if([filemanager fileExistsAtPath:path]){NSDictionary * attributes = [filemanager attributesOfItemAtPath:path error:nil];NSNumber *theFileSize;if ( (theFileSize = [attributes objectForKey:NSFileSize]) ) {return  [theFileSize intValue]/1024;} else {return -1;}} else {return -1;}
}// 秒数格式化
- (NSString *)Timeformat2FromSeconds:(NSInteger)seconds {//format of hourNSString *str_hour = [NSString stringWithFormat:@"%02ld",seconds/3600];//format of minuteNSString *str_minute = [NSString stringWithFormat:@"%02ld",(seconds%3600)/60];//format of secondNSString *str_second = [NSString stringWithFormat:@"%02ld",seconds%60];//format of timeNSString *format_time;if ([str_hour isEqualToString:@"00"]) {format_time = [NSString stringWithFormat:@"%@:%@",str_minute,str_second];} else {format_time = [NSString stringWithFormat:@"%@:%@:%@",str_hour,str_minute,str_second];}return format_time;
}@end

 

这篇关于[iOS]录制或从相册选择视频的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1091413

相关文章

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

Python使用OpenCV实现获取视频时长的小工具

《Python使用OpenCV实现获取视频时长的小工具》在处理视频数据时,获取视频的时长是一项常见且基础的需求,本文将详细介绍如何使用Python和OpenCV获取视频时长,并对每一行代码进行深入解析... 目录一、代码实现二、代码解析1. 导入 OpenCV 库2. 定义获取视频时长的函数3. 打开视频文

Android与iOS设备MAC地址生成原理及Java实现详解

《Android与iOS设备MAC地址生成原理及Java实现详解》在无线网络通信中,MAC(MediaAccessControl)地址是设备的唯一网络标识符,本文主要介绍了Android与iOS设备M... 目录引言1. MAC地址基础1.1 MAC地址的组成1.2 MAC地址的分类2. android与I

exfat和ntfs哪个好? U盘格式化选择NTFS与exFAT的详细区别对比

《exfat和ntfs哪个好?U盘格式化选择NTFS与exFAT的详细区别对比》exFAT和NTFS是两种常见的文件系统,它们各自具有独特的优势和适用场景,以下是关于exFAT和NTFS的详细对比... 无论你是刚入手了内置 SSD 还是便携式移动硬盘或 U 盘,都需要先将它格式化成电脑或设备能够识别的「文

基于Python和MoviePy实现照片管理和视频合成工具

《基于Python和MoviePy实现照片管理和视频合成工具》在这篇博客中,我们将详细剖析一个基于Python的图形界面应用程序,该程序使用wxPython构建用户界面,并结合MoviePy、Pill... 目录引言项目概述代码结构分析1. 导入和依赖2. 主类:PhotoManager初始化方法:__in

用js控制视频播放进度基本示例代码

《用js控制视频播放进度基本示例代码》写前端的时候,很多的时候是需要支持要网页视频播放的功能,下面这篇文章主要给大家介绍了关于用js控制视频播放进度的相关资料,文中通过代码介绍的非常详细,需要的朋友可... 目录前言html部分:JavaScript部分:注意:总结前言在javascript中控制视频播放

Python基于wxPython和FFmpeg开发一个视频标签工具

《Python基于wxPython和FFmpeg开发一个视频标签工具》在当今数字媒体时代,视频内容的管理和标记变得越来越重要,无论是研究人员需要对实验视频进行时间点标记,还是个人用户希望对家庭视频进行... 目录引言1. 应用概述2. 技术栈分析2.1 核心库和模块2.2 wxpython作为GUI选择的优

Python实战之屏幕录制功能的实现

《Python实战之屏幕录制功能的实现》屏幕录制,即屏幕捕获,是指将计算机屏幕上的活动记录下来,生成视频文件,本文主要为大家介绍了如何使用Python实现这一功能,希望对大家有所帮助... 目录屏幕录制原理图像捕获音频捕获编码压缩输出保存完整的屏幕录制工具高级功能实时预览增加水印多平台支持屏幕录制原理屏幕

基于Python实现多语言朗读与单词选择测验

《基于Python实现多语言朗读与单词选择测验》在数字化教育日益普及的今天,开发一款能够支持多语言朗读和单词选择测验的程序,对于语言学习者来说无疑是一个巨大的福音,下面我们就来用Python实现一个这... 目录一、项目概述二、环境准备三、实现朗读功能四、实现单词选择测验五、创建图形用户界面六、运行程序七、

前端知识点之Javascript选择输入框confirm用法

《前端知识点之Javascript选择输入框confirm用法》:本文主要介绍JavaScript中的confirm方法的基本用法、功能特点、注意事项及常见用途,文中通过代码介绍的非常详细,对大家... 目录1. 基本用法2. 功能特点①阻塞行为:confirm 对话框会阻塞脚本的执行,直到用户作出选择。②