【问题标题】:How to transcode video format in .net MVC to .mp4 using awsSDK elastic transcoder?如何使用 awsSDK 弹性转码器将 .net MVC 中的视频格式转码为 .mp4?
【发布时间】:2017-07-07 20:07:43
【问题描述】:

我正在开发一个媒体门户。我已经每天插入和查看视频和图像。想要使用带有 C# 的 Amazon Elastic 转码器将视频格式转码为 .mp4。以下是我上传视频的代码:

public string UploadVideoToLocation(Stream fs, String folder, String subFolder, String filename)
        {
            string accessKey = this.accessKey;
            string secretKey = this.secretKey;

            filename = filename.Replace("+", "");
            String filePath = folder.Replace("+", "") + "/" + subFolder.Replace("+", "") + "/" + Guid.NewGuid() + filename;
            if (Path.GetExtension(filePath).IsNullOrEmpty())
            {
                filePath += ".mp4";
            }

            using (var client = new Amazon.S3.AmazonS3Client(accessKey, secretKey, S3Config))
            {
                PutObjectRequest request = new PutObjectRequest { BucketName = "VideoToConvert", CannedACL = S3CannedACL.PublicRead, Key = filePath, InputStream = fs }; 
                client.PutObject(request);
            }
            String finalOriginalPath = AMAZON_ROOT + filePath;
            finalOriginalPath = finalOriginalPath.Replace("+", "%2B");
            //TODO: transcode to needed format
            return finalOriginalPath;
        }

我应该如何处理将视频转码为 .mp4 的代码?

【问题讨论】:

    标签: c# asp.net-mvc amazon-web-services video amazon-elastic-transcoder


    【解决方案1】:

    使用此代码创建了视频转码功能

    public string UploadVideoToLocation(Stream fs, String folder, String subFolder, String filename)
        {
            string accessKey = this.accessKey;
            string secretKey = this.secretKey;
    
            filename = filename.Replace("+", "");
            String filePath = folder.Replace("+", "") + "/" + subFolder.Replace("+", "") + "/" + Guid.NewGuid() + filename;
            if (Path.GetExtension(filePath).IsNullOrEmpty())
            {
                filePath += ".mp4";
            }
    
            using (var client = new Amazon.S3.AmazonS3Client(accessKey, secretKey, S3Config))
            {
                PutObjectRequest request = new PutObjectRequest { BucketName = "videoToconvert", CannedACL = S3CannedACL.PublicRead, Key = filePath, InputStream = fs }; 
                client.PutObject(request);
            }
            String finalOriginalPath = AMAZON_ROOT + filePath;
            finalOriginalPath = finalOriginalPath.Replace("+", "%2B");
    
            var etsClient = new AmazonElasticTranscoderClient(accessKey, secretKey, S3Config.RegionEndpoint);
    
            var notifications = new Notifications()
            {
                Completed = "arn:aws:sns:us-west-2:277579135337:Transcode",
                Error = "arn:aws:sns:us-west-2:277579135337:Transcode",
                Progressing = "arn:aws:sns:us-west-2:277579135337:Transcode",
                Warning = "arn:aws:sns:us-west-2:277579135337:Transcode"
            };
    
            var pipeline = new Pipeline();
            if (etsClient.ListPipelines().Pipelines.Count() == 0)
            {
                pipeline = etsClient.CreatePipeline(new CreatePipelineRequest()
                {
                    Name = "MyTranscodedVideos",
                    InputBucket = "videoToconvert",
                    OutputBucket = "videoToconvert",
                    Notifications = notifications,
                    Role = "arn:aws:iam::277579135337:role/Elastic_Transcoder_Default_Role",
                }).Pipeline; //createpipelineresult
            }
            else
            {
                pipeline= etsClient.ListPipelines().Pipelines.First();
            }
    
            etsClient.CreateJob(new CreateJobRequest()
            {
                PipelineId = pipeline.Id,
                Input = new JobInput()
                {
                    AspectRatio = "auto",
                    Container = "mp4", //H.264
                    FrameRate = "auto",
                    Interlaced = "auto",
                    Resolution = "auto",
                    Key = filePath
                },
                Output = new CreateJobOutput()
                {
                    ThumbnailPattern = "thumbnnail{count}",
                    Rotate = "0",
                    PresetId = "1351620000001-000010", //Generic-720 px
                    Key = finalOriginalPath
                }
            });
    
            using (var delClient = new Amazon.S3.AmazonS3Client(accessKey, secretKey, S3Config))
            {
                //delClient.DeleteObject("VideoToConvert", filePath);
            }
    
            return finalOriginalPath;
        }
    

    【讨论】:

      猜你喜欢
      • 2012-01-14
      • 2012-05-07
      • 2016-05-01
      • 2012-01-20
      • 2012-02-20
      • 1970-01-01
      • 2012-03-23
      • 2012-05-22
      • 2012-04-10
      相关资源
      最近更新 更多