如何使用awsSDK弹性转码器将.net MVC中的视频格式转码为.mp4?

时间:2016-06-30 09:44:58

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

我正在开发媒体门户网站。我已经有每天插入和查看的视频和图像。希望使用带有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的代码?

1 个答案:

答案 0 :(得分:0)

使用此代码

创建视频转码功能
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;
    }