Пример #1
0
        public async Task <Video> DownloadLocalPlaylist(Media media)
        {
            try
            {
                Video video = new Video();
                if (media.JsonMetadata.ContainsKey("video1Path"))
                {
                    var video1Path = media.JsonMetadata["video1Path"].ToString();
                    video.Video1 = await FileRecord.GetNewFileRecordAsync(video1Path, Path.GetExtension(video1Path));
                }
                if (media.JsonMetadata.ContainsKey("video2Path"))
                {
                    var video2Path = media.JsonMetadata["video2Path"].ToString();
                    video.Video2 = await FileRecord.GetNewFileRecordAsync(video2Path, Path.GetExtension(video2Path));
                }

                return(video);
            }
            catch (Exception e)
            {
                GetLogger().LogError(e, "DownloadLocalPlaylist failed. mediaId {0}", media.Id);
                using (var context = CTDbContext.CreateDbContext())
                {
                    context.Medias.Remove(media);
                    context.SaveChanges();
                }
                return(null);
            }
        }
Пример #2
0
        public async Task <Video> DownloadYoutubeVideo(Media media)
        {
            var mediaResponse = await _rpcClient.PythonServerClient.DownloadYoutubeVideoRPCAsync(new CTGrpc.MediaRequest
            {
                VideoUrl = media.JsonMetadata["videoUrl"].ToString()
            });

            if (FileRecord.IsValidFile(mediaResponse.FilePath))
            {
                Video video = new Video
                {
                    Video1 = await FileRecord.GetNewFileRecordAsync(mediaResponse.FilePath, mediaResponse.Ext)
                };
                return(video);
            }
            else
            {
                // Deleting media is fine if download failed as we can get it back from the youtube playlist.
                GetLogger().LogError("DownloadYoutubeVideo failed. mediaId {0}, removing Media record", media.Id);
                using (var context = CTDbContext.CreateDbContext())
                {
                    context.Medias.Remove(media);
                    context.SaveChanges();
                }

                return(null);
            }
        }
Пример #3
0
        public async Task <Video> DownloadEchoVideo(Media media)
        {
            Video video = new Video();
            bool  video1Success = false, video2Success = false;

            var mediaResponse = await _rpcClient.PythonServerClient.DownloadEchoVideoRPCAsync(new CTGrpc.MediaRequest
            {
                VideoUrl       = media.JsonMetadata["videoUrl"].ToString(),
                AdditionalInfo = media.Playlist.JsonMetadata["downloadHeader"].ToString()
            });

            video1Success = FileRecord.IsValidFile(mediaResponse.FilePath);
            if (video1Success)
            {
                video.Video1 = await FileRecord.GetNewFileRecordAsync(mediaResponse.FilePath, mediaResponse.Ext);
            }


            if (!string.IsNullOrEmpty(media.JsonMetadata["altVideoUrl"].ToString()))
            {
                var mediaResponse2 = await _rpcClient.PythonServerClient.DownloadEchoVideoRPCAsync(new CTGrpc.MediaRequest
                {
                    VideoUrl       = media.JsonMetadata["altVideoUrl"].ToString(),
                    AdditionalInfo = media.Playlist.JsonMetadata["downloadHeader"].ToString()
                });

                video2Success = FileRecord.IsValidFile(mediaResponse2.FilePath);
                if (video2Success)
                {
                    video.Video2 = await FileRecord.GetNewFileRecordAsync(mediaResponse2.FilePath, mediaResponse.Ext);
                }
            }
            else
            {
                // As there is no file to download, it's "successfull"
                video2Success = true;
            }



            if (video1Success && video2Success)
            {
                return(video);
            }
            else
            {
                // Deleting media is fine if download failed as we can get it back from the echo playlist.
                GetLogger().LogError("DownloadEchoVideo failed. mediaId {0}, removing Media record", media.Id);
                using (var context = CTDbContext.CreateDbContext())
                {
                    context.Medias.Remove(media);
                    context.SaveChanges();
                }

                return(null);
            }
        }
Пример #4
0
        protected async override Task OnConsume(string transcriptionId, TaskParameters taskParameters, ClientActiveTasks cleanup)
        {
            registerTask(cleanup, transcriptionId); // may throw AlreadyInProgress exception

            GetLogger().LogInformation($"Creating VTT & SRT files for ({transcriptionId})");

            using (var _context = CTDbContext.CreateDbContext())
            {
                var transcription = await _context.Transcriptions.FindAsync(transcriptionId);

                CaptionQueries captionQueries = new CaptionQueries(_context);
                var            captions       = await captionQueries.GetCaptionsAsync(transcription.Id);

                var vttfile = await FileRecord.GetNewFileRecordAsync(Caption.GenerateWebVTTFile(captions, transcription.Language), ".vtt");

                FileRecord?existingVtt = await _context.FileRecords.FindAsync(transcription.FileId);

                if (existingVtt is null)
                {
                    GetLogger().LogInformation($"{transcriptionId}: Creating new vtt file {vttfile.FileName}");
                    await _context.FileRecords.AddAsync(vttfile);

                    transcription.File = vttfile;
                    _context.Entry(transcription).State = EntityState.Modified;
                }
                else
                {
                    GetLogger().LogInformation($"{transcriptionId}: replacing existing vtt file contents {existingVtt.FileName}");
                    existingVtt.ReplaceWith(vttfile);
                    _context.Entry(existingVtt).State = EntityState.Modified;
                }

                var srtfile = await FileRecord.GetNewFileRecordAsync(Caption.GenerateSrtFile(captions), ".srt");

                FileRecord?existingSrt = await _context.FileRecords.FindAsync(transcription.SrtFileId);

                if (existingSrt is null)
                {
                    GetLogger().LogInformation($"{transcriptionId}: Creating new srt file {srtfile.FileName}");

                    await _context.FileRecords.AddAsync(srtfile);

                    transcription.SrtFile = srtfile;
                    _context.Entry(transcription).State = EntityState.Modified;
                }
                else
                {
                    GetLogger().LogInformation($"{transcriptionId}: replacing existing srt file contents {existingSrt.FileName}");
                    existingSrt.ReplaceWith(srtfile);
                    _context.Entry(existingSrt).State = EntityState.Modified;
                }

                await _context.SaveChangesAsync();

                GetLogger().LogInformation($"{transcriptionId}: Database updated");
            }
        }
Пример #5
0
        public async Task <ActionResult <Image> > PostImage(IFormFile imageFile, [FromForm] string sourceType, [FromForm] string sourceId)
        {
            if (imageFile == null || imageFile.Length <= 0)
            {
                return(BadRequest("Image file is compulsory"));
            }

            if (sourceType == null || sourceId == null)
            {
                return(BadRequest("Must include valid sourceType and sourceId"));
            }

            try
            {
                ResourceType type = (ResourceType)Enum.Parse(typeof(ResourceType), sourceType);

                Image image = new Image
                {
                    SourceType = type,
                    SourceId   = sourceId
                };

                // full path to file in temp location
                string extension         = Path.GetExtension(imageFile.FileName).ToLower(System.Globalization.CultureInfo.CurrentCulture);
                var    allowedExtensions = new string[] { ".png", ".jpg" };

                if (!allowedExtensions.Contains(extension))
                {
                    return(BadRequest($"File format not permitted, only {string.Join(',', allowedExtensions)} files are accepted"));
                }

                var filePath = CommonUtils.GetTmpFile();
                using (var stream = new FileStream(filePath, FileMode.Create))
                {
                    await imageFile.CopyToAsync(stream);
                }

                image.ImageFile = await FileRecord.GetNewFileRecordAsync(filePath, extension);

                _context.Images.Add(image);
                await _context.SaveChangesAsync();

                return(CreatedAtAction("GetImage", new { id = image.Id }, image));
            }
            catch (ArgumentException ex)
            {
                _logger.LogError(ex, $"PostImage {imageFile.FileName}");
                return(BadRequest($"{sourceType} is not a valid resource type"));
            }
        }
Пример #6
0
        protected async override Task OnConsume(string transcriptionId, TaskParameters taskParameters, ClientActiveTasks cleanup)
        {
            registerTask(cleanup, transcriptionId); // may throw AlreadyInProgress exception
            using (var _context = CTDbContext.CreateDbContext())
            {
                var transcription = await _context.Transcriptions.FindAsync(transcriptionId);

                FileRecord existingVtt = await _context.FileRecords.FindAsync(transcription.FileId);

                FileRecord existingSrt = await _context.FileRecords.FindAsync(transcription.SrtFileId);


                CaptionQueries captionQueries = new CaptionQueries(_context);
                var            captions       = await captionQueries.GetCaptionsAsync(transcription.Id);

                var vttfile = await FileRecord.GetNewFileRecordAsync(Caption.GenerateWebVTTFile(captions, transcription.Language), ".vtt");

                if (string.IsNullOrEmpty(transcription.FileId))
                {
                    await _context.FileRecords.AddAsync(vttfile);

                    transcription.File = vttfile;
                    _context.Entry(transcription).State = EntityState.Modified;
                }
                else
                {
                    existingVtt.ReplaceWith(vttfile);
                    _context.Entry(existingVtt).State = EntityState.Modified;
                }

                var srtfile = await FileRecord.GetNewFileRecordAsync(Caption.GenerateSrtFile(captions), ".srt");

                if (string.IsNullOrEmpty(transcription.SrtFileId))
                {
                    await _context.FileRecords.AddAsync(srtfile);

                    transcription.SrtFile = srtfile;
                    _context.Entry(transcription).State = EntityState.Modified;
                }
                else
                {
                    existingSrt.ReplaceWith(srtfile);
                    _context.Entry(existingSrt).State = EntityState.Modified;
                }

                await _context.SaveChangesAsync();
            }
        }
        /// <summary>
        /// Original implementation of OnConsume. This code may be deleted if it is no longer useful. It is left as available for now as a template
        /// </summary>
        /// <param name="videoId"></param>
        /// <param name="taskParameters"></param>
        /// <returns></returns>
        private async Task OldOnConsumeNotUsed(string videoId, TaskParameters taskParameters)
        {
            using (var _context = CTDbContext.CreateDbContext())
            {
                // Get the video object
                var video = await _context.Videos.FindAsync(videoId);

                _logger.LogInformation("Consuming" + video);
                // Make RPC call to produce audio file.
                var file = await _rpcClient.PythonServerClient.ConvertVideoToWavRPCWithOffsetAsync(new CTGrpc.FileForConversion
                {
                    File = new CTGrpc.File {
                        FilePath = video.Video1.VMPath
                    }
                });


                // Check if a valid file was returned.
                if (FileRecord.IsValidFile(file.FilePath))
                {
                    var fileRecord = await FileRecord.GetNewFileRecordAsync(file.FilePath, file.Ext);

                    // Get the latest video object, in case it has changed
                    var videoLatest = await _context.Videos.FindAsync(video.Id);

                    // If there is no Audio file present, then update.
                    if (videoLatest.Audio == null)
                    {
                        await _context.FileRecords.AddAsync(fileRecord);

                        videoLatest.Audio = fileRecord;
                        await _context.SaveChangesAsync();


                        // If no transcriptions present, produce transcriptions.
                        if (!videoLatest.Transcriptions.Any())
                        {
                            _transcriptionTask.Publish(videoLatest.Id);
                        }
                    }
                }
                else
                {
                    throw new Exception("ConvertVideoToWavTask Failed + " + video.Id);
                }
            }
        }
Пример #8
0
        public async Task <Video> DownloadBoxVideo(Media media)
        {
            try
            {
                var guid    = Guid.NewGuid().ToString();
                var newPath = Path.Combine(Globals.appSettings.DATA_DIRECTORY, guid + ".mp4");
                var client  = await _box.GetBoxClientAsync();

                var stream = await client.FilesManager.DownloadAsync(media.UniqueMediaIdentifier);

                using (var fileStream = File.Create(newPath))
                {
                    stream.CopyTo(fileStream);
                }
                if (FileRecord.IsValidFile(newPath))
                {
                    Video video = new Video
                    {
                        Video1 = await FileRecord.GetNewFileRecordAsync(newPath, Path.GetExtension(newPath))
                    };
                    return(video);
                }
                else
                {
                    // Deleting media is fine if download failed as we can get it back from the youtube playlist.
                    GetLogger().LogError("DownloadBoxVideo failed. mediaId {0}, removing Media record", media.Id);
                    using (var context = CTDbContext.CreateDbContext())
                    {
                        context.Medias.Remove(media);
                        context.SaveChanges();
                    }
                    return(null);
                }
            }
            catch (Box.V2.Exceptions.BoxSessionInvalidatedException e)
            {
                GetLogger().LogError(e, "Box Token Failure.");
                await _slack.PostErrorAsync(e, "Box Token Failure.");

                throw;
            }
        }
Пример #9
0
        public async Task <ActionResult <Image> > PostImage(IFormFile imageFile, [FromForm] string sourceType, [FromForm] string sourceId)
        {
            if (imageFile == null || imageFile.Length <= 0)
            {
                return(BadRequest("Image file is compulsory"));
            }

            try
            {
                ResourceType type = (ResourceType)Enum.Parse(typeof(ResourceType), sourceType);

                Image image = new Image
                {
                    SourceType = type,
                    SourceId   = sourceId
                };

                // full path to file in temp location
                if (Path.GetExtension(imageFile.FileName) != ".png" && Path.GetExtension(imageFile.FileName) != ".jpg")
                {
                    return(BadRequest("File format not permitted, only .png and .jpg files are accepted"));
                }

                var filePath = CommonUtils.GetTmpFile();
                using (var stream = new FileStream(filePath, FileMode.Create))
                {
                    await imageFile.CopyToAsync(stream);
                }

                image.ImageFile = await FileRecord.GetNewFileRecordAsync(filePath, Path.GetExtension(filePath));

                _context.Images.Add(image);
                await _context.SaveChangesAsync();

                return(CreatedAtAction("GetImage", new { id = image.Id }, image));
            }
            catch (ArgumentException)
            {
                return(BadRequest($"{sourceType} is not a valid resource type"));
            }
        }
Пример #10
0
        public async Task <Video> DownloadKalturaVideo(Media media)
        {
            var mediaResponse = await _rpcClient.PythonServerClient.DownloadKalturaVideoRPCAsync(new CTGrpc.MediaRequest
            {
                VideoUrl = media.JsonMetadata["downloadUrl"].ToString()
            });

            Video video;

            if (FileRecord.IsValidFile(mediaResponse.FilePath))
            {
                video = new Video
                {
                    Video1 = await FileRecord.GetNewFileRecordAsync(mediaResponse.FilePath, mediaResponse.Ext)
                };
            }
            else
            {
                throw new Exception("DownloadKalturaVideo Failed + " + media.Id);
            }

            return(video);
        }
Пример #11
0
        protected async override Task OnConsume(string videoId, TaskParameters taskParameters, ClientActiveTasks cleanup)
        {
            registerTask(cleanup, videoId); // may throw AlreadyInProgress exception
            Video video;
            bool  videoUpdated = false;

            using (var _context = CTDbContext.CreateDbContext())
            {
                video = await _context.Videos.Include(v => v.Video1)
                        .Include(v => v.Video2)
                        .Include(v => v.ProcessedVideo1)
                        .Include(v => v.ProcessedVideo2)
                        .Where(v => v.Id == videoId).FirstAsync();
            }
            GetLogger().LogInformation("Consuming" + video);
            if (video.Duration == null && video.Video1 != null)
            {
                var mediaInfoResult = await _rpcClient.PythonServerClient.GetMediaInfoRPCAsync(new CTGrpc.File
                {
                    FilePath = video.Video1.VMPath
                });

                var mediaJson = JObject.Parse(mediaInfoResult.Json);
                video.FileMediaInfo = mediaJson;
                video.UpdateMediaProperties();
                videoUpdated = true;
            }
            bool runbrokencode = false;

            if (runbrokencode)
            {
                if (video.Video1 != null)
                {
                    if (video.ProcessedVideo1 == null || taskParameters.Force)
                    {
                        var file = await _rpcClient.PythonServerClient.ProcessVideoRPCAsync(new CTGrpc.File
                        {
                            FilePath = video.Video1.VMPath
                        });

                        //This does not work
                        video.ProcessedVideo1 = await FileRecord.GetNewFileRecordAsync(file.FilePath, file.Ext);

                        videoUpdated = true;
                    }
                }
                if (video.Video2 != null)
                {
                    if (video.ProcessedVideo2 == null || taskParameters.Force)
                    {
                        var file = await _rpcClient.PythonServerClient.ProcessVideoRPCAsync(new CTGrpc.File
                        {
                            FilePath = video.Video2.VMPath
                        });

                        //This does not work
                        video.ProcessedVideo2 = await FileRecord.GetNewFileRecordAsync(file.FilePath, file.Ext);

                        videoUpdated = true;
                    }
                }
            }
            if (videoUpdated)
            {
                using (var _context = CTDbContext.CreateDbContext())
                {
                    _context.Entry(video).State = EntityState.Modified;
                    await _context.SaveChangesAsync();
                }
            }
        }