Beispiel #1
0
        public async Task <SavingError> SaveVideoAsync(StorageFile file)
        {
            var audioPath = file.Path;

            if (App.Locator.CollectionService.VideoAlreadyExists(audioPath))
            {
                return(SavingError.AlreadyExists);
            }

            #region Getting metadata

            #region id3 tags

            TagLib.Tag tags = null;
            TimeSpan   duration;
            // var tryAsM4A = false;
            var fileStream = await file.OpenStreamForReadAsync();

            TagLib.File tagFile = null;
            try
            {
                tagFile  = TagLib.File.Create(new TagLib.StreamFileAbstraction(file.Name, fileStream, fileStream));
                _currupt = false;
            }
            catch (Exception e)
            {
                // tryAsM4A = e is CorruptFileException;
                if (e.InnerException != null)
                {
                    Debug.WriteLine(e.InnerException.Message);
                }
                _currupt = true;
            }


            if (!_currupt)
            {
                tags = tagFile == null ? null : tagFile.Tag;
                if (tags != null)
                {
                    duration = tagFile.Properties.Duration;
                }
                fileStream.Dispose();
                if (tagFile != null)
                {
                    tagFile.Dispose();
                }

                #endregion

                LocalVideo video;
                //if there aren't any id3tags, try using the properties from the file
                var prop = await file.Properties.GetVideoPropertiesAsync().AsTask().ConfigureAwait(false);

                video = new LocalVideo(prop)
                {
                    FilePath = audioPath,
                };

                var val = video.ToVideo();

                #endregion

                //if no metadata was obtain, then result to using the filename
                if (string.IsNullOrEmpty(val.Title))
                {
                    val.Title  = file.DisplayName;
                    val.Author = file.DisplayName;
                }

                if (App.Locator.CollectionService.VideoAlreadyExists(val.Title, val.Author))
                {
                    return(SavingError.AlreadyExists);
                }

                StorageItemThumbnail thumb;
                try
                {
                    try
                    {
                        thumb = await file.GetThumbnailAsync(ThumbnailMode.VideosView);
                    }
                    catch
                    {
                        thumb = null;
                    }
                    await App.Locator.CollectionService.AddVideoAsync(val, tags, thumb).ConfigureAwait(false);

                    return(SavingError.None);
                }

                catch
                {
                    return(SavingError.Unknown);
                }
            }

            else
            {
                return(SavingError.Unknown);
            }
        }
Beispiel #2
0
        protected async override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            if (e.Parameter.GetType() == typeof(List <VideoInfo>))
            {
                // 读取在线视频信息
                isLocal = false;
                infos   = e.Parameter as List <VideoInfo>;
                Index   = Convert.ToInt32(infos[0].Cid) + 1;
                if (Index == 1)
                {
                    left.Visibility = Visibility.Collapsed;
                }
                if (infos.Count <= 2 || Index == infos.Count - 1)
                {
                    right.Visibility = Visibility.Collapsed;
                }
                if (SettingHelper.ContainsKey("_videoformat"))
                {
                    format = SettingHelper.GetValue("_videoformat").ToString() == "0"
                    ? VideoFormat.mp4
                    : VideoFormat.flv;
                }
                // 默认的格式:mp4
                else
                {
                    format = VideoFormat.mp4;
                }
                await read(Index);
            }
            else
            {
                isLocal = true;
                btn_Switchffmpeg.Visibility = Visibility.Visible;
                JsonObject json = new JsonObject();
                if (e.Parameter.GetType() == typeof(string))
                {
                    // 文件关联
                    string a = e.Parameter.ToString();
                    if (a[0] == '@')
                    {
                        left.Visibility = right.Visibility = Visibility.Collapsed;
                        string path = e.Parameter.ToString().Substring(1);
                        file = await StorageFile.GetFileFromPathAsync(path);
                        await GetInfoAsync();

                        Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.Add(file);
                        status.Text += "正在读取弹幕...";
                        var danmupool = await GetDanmu(file);

                        status.Text   += Environment.NewLine + "正在读取视频...";
                        txt_title.Text = file.DisplayName;
                        var stream = await file.OpenAsync(FileAccessMode.Read);

                        media.SetSource(stream, file.ContentType);
                        status.Text += "完毕";
                        await Task.Delay(500);

                        return;
                    }
                }
                // 读取本地视频
                if (e.Parameter.GetType() == typeof(StorageFile))
                {
                    left.Visibility = right.Visibility = Visibility.Collapsed;
                    file            = e.Parameter as StorageFile;
                    await GetInfoAsync();

                    if (file != null)
                    {
                        Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.Add(file);
                        status.Text += "正在读取弹幕...";
                        var danmupool = await GetDanmu(file);

                        status.Text   += Environment.NewLine + "正在读取视频...";
                        txt_title.Text = file.DisplayName;
                        // 系统原生支持的类型
                        if (VideoHelper.videoExtensions_sys.Contains(file.FileType))
                        {
                            var stream = await file.OpenAsync(FileAccessMode.Read);

                            media.SetSource(stream, file.ContentType);
                            status.Text += "完毕";
                            await Task.Delay(500);

                            return;
                        }
                        else
                        {
                            await SetFFmpegSource(file);

                            status.Text += "完毕";
                            await Task.Delay(500);

                            return;
                        }
                    }
                }
                // 下载列表
                if (e.Parameter.GetType() == typeof(LocalVideo))
                {
                    LocalVideo myVideo = e.Parameter as LocalVideo;
                    if (myVideo != null)
                    {
                        left.Visibility = right.Visibility = Visibility.Collapsed;
                        part            = myVideo.Part;
                        folder          = myVideo.Folder;
                        StorageFolder myfolder = await KnownFolders.VideosLibrary.GetFolderAsync("哔哩哔哩");

                        myfolder = await myfolder.GetFolderAsync(folder);

                        // 文件的原本名称是 part_index, 而弹幕文件是 part, 不冲突
                        string filename = $"{part}{myVideo.Format}";
                        file = await myfolder.GetFileAsync(filename);
                        await GetInfoAsync();

                        Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.Add(file);
                        status.Text += "正在读取弹幕...";
                        var danmupool = await GetDanmu(file);

                        if (danmupool != null)
                        {
                            DanmuPool = danmupool;
                        }
                        status.Text   += Environment.NewLine + "正在读取视频...";
                        txt_title.Text = part.Split('_')[0];
                        // 如果是系统原生支持的格式,直接设置媒体源
                        if (VideoHelper.videoExtensions_sys.Contains(file.FileType))
                        {
                            var stream = await file.OpenAsync(FileAccessMode.Read);

                            media.SetSource(stream, file.ContentType);
                            status.Text += "完毕";
                            await Task.Delay(500);

                            return;
                        }
                        // 否则,使用ffmpeg
                        else
                        {
                            await SetFFmpegSource(file);

                            return;
                        }
                    }
                }
            }
        }