【iOS】 視頻壓縮上傳

之前寫(xiě)過(guò)圖片上傳PHP服務(wù)器,今天把接口稍微改了一下,把視頻上傳的代碼貼出來(lái),目前上傳功能已經(jīng)調(diào)通,視頻的壓縮代碼上似乎并不完善,后續(xù)會(huì)完善壓縮部分的代碼;

- (void)convertVideoWithURL:(NSURL *)url
{
    NSDate *date = [NSDate date];
    NSDateFormatter *dateformatter = [[NSDateFormatter alloc]init];
    [dateformatter setDateFormat:@"YYYY-MM-dd-HH-mm-ss"];
    NSString *dateName = [dateformatter stringFromDate:date];
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
    NSString *pathName = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp4",dateName]];
    NSLog(@"沙盒:%@",pathName);
    //轉(zhuǎn)碼配置
    AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
    AVAssetExportSession *exportSession= [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetMediumQuality];
    exportSession.shouldOptimizeForNetworkUse = YES;
    exportSession.outputURL = [NSURL fileURLWithPath:pathName];
    exportSession.outputFileType = AVFileTypeMPEG4;
    [exportSession exportAsynchronouslyWithCompletionHandler:^{
        int exportStatus = exportSession.status;
        switch (exportStatus)         {
            case AVAssetExportSessionStatusFailed:
            {
                // log error to text view
                NSError *exportError = exportSession.error;
                NSLog (@"AVAssetExportSessionStatusFailed: %@", exportError);
                [SVProgressHUD showErrorWithStatus:@"視頻壓縮失敗"];
                [SVProgressHUD dismissWithDelay:1.0];
                break;
            }
            case AVAssetExportSessionStatusCompleted:
            {
                self.videoData = [NSData dataWithContentsOfFile:pathName];
                
                [[NetTool shareDL]upLoadVideoWithURL:@"http://192.168.1.102/php/image.php" paremeter:nil data:self.videoData videoName:[NSString stringWithFormat:@"%@.mp4",dateName] progress:^(NSProgress * _Nonnull uploadProgress) {
                    
                    [SVProgressHUD showProgress:1.0*uploadProgress.completedUnitCount/uploadProgress.totalUnitCount status:@"正在上傳"];
                    NSLog(@"正在上傳%f%%",(1.0*uploadProgress.completedUnitCount/uploadProgress.totalUnitCount)*100);
                    
                } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                    [SVProgressHUD showSuccessWithStatus:@"上傳成功"];
                    [SVProgressHUD dismissWithDelay:1.0];
                } fail:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                    [SVProgressHUD showErrorWithStatus:@"上傳失敗"];
                    [SVProgressHUD dismissWithDelay:1.0];
                }];
                
            }
        }
    }];
}
[manager POST:url parameters:parameter constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        [formData appendPartWithFileData:videoData name:@"upimage" fileName:videoName mimeType:@"video/mp4"];
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        progress(uploadProgress);
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        success(task,responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        fail(task,error);
    }];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,813評(píng)論 25 708
  • 直接git clone下來(lái):git clone https://github.com/greenrobot/Eve...
    丸_子閱讀 2,679評(píng)論 0 5
  • 杭杭老師說(shuō)臨摹照片比臨摹畫(huà)成長(zhǎng)速度要快,于是試了下。以我這初學(xué)者的水平確實(shí)難度有些太大了,爪子處理不好,樹(shù)皮也不會(huì)...
    Joy_RY閱讀 629評(píng)論 0 0
  • 也許,把世間所有的感情事物用利益這桿秤掂量一下,就不會(huì)那么難以抉擇,不會(huì)那么割舍不下.我是一個(gè)風(fēng)箏,一個(gè)需要安全感...
    Sabrina667閱讀 183評(píng)論 0 1
  • 在運(yùn)營(yíng)上,將服務(wù)業(yè)制造業(yè)化、流水線化。 將戰(zhàn)略濃縮為一個(gè)簡(jiǎn)單的執(zhí)行動(dòng)作,重復(fù)它,則戰(zhàn)略目標(biāo)必達(dá)成。
    農(nóng)爸爸閱讀 136評(píng)論 0 0