[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

相关文章

Python+FFmpeg实现视频自动化处理的完整指南

《Python+FFmpeg实现视频自动化处理的完整指南》本文总结了一套在Python中使用subprocess.run调用FFmpeg进行视频自动化处理的解决方案,涵盖了跨平台硬件加速、中间素材处理... 目录一、 跨平台硬件加速:统一接口设计1. 核心映射逻辑2. python 实现代码二、 中间素材处

Vue3视频播放组件 vue3-video-play使用方式

《Vue3视频播放组件vue3-video-play使用方式》vue3-video-play是Vue3的视频播放组件,基于原生video标签开发,支持MP4和HLS流,提供全局/局部引入方式,可监听... 目录一、安装二、全局引入三、局部引入四、基本使用五、事件监听六、播放 HLS 流七、更多功能总结在 v

Python屏幕抓取和录制的详细代码示例

《Python屏幕抓取和录制的详细代码示例》随着现代计算机性能的提高和网络速度的加快,越来越多的用户需要对他们的屏幕进行录制,:本文主要介绍Python屏幕抓取和录制的相关资料,需要的朋友可以参考... 目录一、常用 python 屏幕抓取库二、pyautogui 截屏示例三、mss 高性能截图四、Pill

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选择的优