public static async Task <FFmpegProcess> RunAsync(params string[] args)
        {
            var ffmpegProcess = new FFmpegProcess(args);
            await ffmpegProcess.RunAsync();

            return(ffmpegProcess);
        }
        public async Task ExecuteAsync(WorkerJobHelper <TransformJob> job)
        {
            S3Locator inputFile;

            if (!job.JobInput.TryGet <S3Locator>(nameof(inputFile), out inputFile))
            {
                throw new Exception("Invalid or missing input file.");
            }

            S3Locator outputLocation;

            if (!job.JobInput.TryGet <S3Locator>(nameof(outputLocation), out outputLocation))
            {
                throw new Exception("Invalid or missing output location.");
            }

            if (string.IsNullOrWhiteSpace(inputFile.AwsS3Bucket) || string.IsNullOrWhiteSpace(inputFile.AwsS3Key))
            {
                throw new Exception("Not able to obtain input file");
            }

            var data = await inputFile.GetAsync();

            var localFileName = "/tmp/" + Guid.NewGuid();
            await data.WriteResponseStreamToFileAsync(localFileName, true, CancellationToken.None);

            var tempFileName  = "/tmp/" + Guid.NewGuid() + ".mp4";
            var ffmpegParams  = new[] { "-y", "-i", localFileName, "-preset", "ultrafast", "-vf", "scale=-1:360", "-c:v", "libx264", "-pix_fmt", "yuv420p", tempFileName };
            var ffmpegProcess = await FFmpegProcess.RunAsync(ffmpegParams);

            File.Delete(localFileName);

            var s3Params = new PutObjectRequest
            {
                BucketName  = outputLocation.AwsS3Bucket,
                Key         = (outputLocation.AwsS3KeyPrefix ?? string.Empty) + Guid.NewGuid().ToString() + ".mp4",
                FilePath    = tempFileName,
                ContentType = "video/mp4"
            };

            var outputS3 = await outputLocation.GetClientAsync();

            var putResp = await outputS3.PutObjectAsync(s3Params);

            job.JobOutput["outputFile"] = new S3Locator
            {
                AwsS3Bucket = s3Params.BucketName,
                AwsS3Key    = s3Params.Key
            };

            await job.CompleteAsync();
        }
Exemplo n.º 3
0
        internal static async Task ProcessJobAssignmentAsync(TransformServiceWorkerRequest @event)
        {
            var resourceManager = @event.Request.GetAwsV4ResourceManager();
            var table           = new DynamoDbTable(@event.Request.StageVariables["TableName"]);
            var jobAssignmentId = @event.JobAssignmentId;

            try
            {
                // 1. Setting job assignment status to RUNNING
                await UpdateJobAssignmentStatusAsync(resourceManager, table, jobAssignmentId, "RUNNING", null);

                // 2. Retrieving WorkflowJob
                var transformJob = await RetrieveTransformJobAsync(resourceManager, table, jobAssignmentId);

                // 3. Retrieve JobProfile
                var jobProfile = await RetrieveJobProfileAsync(resourceManager, transformJob);

                // 4. Retrieve job inputParameters
                var jobInput = transformJob.JobInput;

                // 5. Check if we support jobProfile and if we have required parameters in jobInput
                ValidateJobProfile(jobProfile, jobInput);

                switch (jobProfile.Name)
                {
                case JOB_PROFILE_CREATE_PROXY_LAMBDA:
                    S3Locator inputFile;
                    if (!jobInput.TryGet <S3Locator>(nameof(inputFile), out inputFile))
                    {
                        throw new Exception("Invalid or missing input file.");
                    }

                    S3Locator outputLocation;
                    if (!jobInput.TryGet <S3Locator>(nameof(outputLocation), out outputLocation))
                    {
                        throw new Exception("Invalid or missing output location.");
                    }

                    if (string.IsNullOrWhiteSpace(inputFile.AwsS3Bucket) || string.IsNullOrWhiteSpace(inputFile.AwsS3Key))
                    {
                        throw new Exception("Not able to obtain input file");
                    }

                    var data = await inputFile.GetAsync();

                    var localFileName = "/tmp/" + Guid.NewGuid();
                    await data.WriteResponseStreamToFileAsync(localFileName, true, CancellationToken.None);

                    var tempFileName  = "/tmp/" + Guid.NewGuid() + ".mp4";
                    var ffmpegParams  = new[] { "-y", "-i", localFileName, "-preset", "ultrafast", "-vf", "scale=-1:360", "-c:v", "libx264", "-pix_fmt", "yuv420p", tempFileName };
                    var ffmpegProcess = await FFmpegProcess.RunAsync(ffmpegParams);

                    File.Delete(localFileName);

                    var s3Params = new PutObjectRequest
                    {
                        BucketName  = outputLocation.AwsS3Bucket,
                        Key         = (outputLocation.AwsS3KeyPrefix ?? string.Empty) + Guid.NewGuid().ToString() + ".mp4",
                        FilePath    = tempFileName,
                        ContentType = "video/mp4"
                    };

                    var outputS3 = await outputLocation.GetClientAsync();

                    var putResp = await outputS3.PutObjectAsync(s3Params);

                    var jobOutput = new JobParameterBag();
                    jobOutput["outputFile"] = new S3Locator
                    {
                        AwsS3Bucket = s3Params.BucketName,
                        AwsS3Key    = s3Params.Key
                    };

                    await UpdateJobAssignmentWithOutputAsync(table, jobAssignmentId, jobOutput);
                    await UpdateJobAssignmentStatusAsync(resourceManager, table, jobAssignmentId, "COMPLETED");

                    break;

                case JOB_PROFILE_CREATE_PROXY_EC2:
                    var ec2hostname = @event.Request.StageVariables["HostnameInstanceEC2"];

                    var ec2Url = "http://" + ec2hostname + "/new-transform-job";

                    var message = new
                    {
                        input = jobInput,
                        notificationEndpoint = new NotificationEndpoint {
                            HttpEndpoint = jobAssignmentId + "/notifications"
                        }
                    };

                    Logger.Debug("Sending to", ec2Url, "message", message);
                    var mcmaHttp = new McmaHttpClient();
                    await mcmaHttp.PostAsJsonAsync(ec2Url, message);

                    Logger.Debug("Done");
                    break;
                }
            }
            catch (Exception ex)
            {
                Logger.Exception(ex);

                try
                {
                    await UpdateJobAssignmentStatusAsync(resourceManager, table, jobAssignmentId, "FAILED", ex.ToString());
                }
                catch (Exception innerEx)
                {
                    Logger.Exception(innerEx);
                }
            }
            finally
            {
                Logger.Debug("Exiting TransformServiceWorker.ProcessJobAssignmentAsync");
            }
        }