Пример #1
0
        async Task openAndPlay(VideoAudioPair item, int?offsetSeconds = null)
        {
            try
            {
                String name = item.Video != null ? item.Video.Name : item.Audio.Name;

                EventAggregator.GetEvent <TitleChangedEvent>().Publish(name);

                await OpenAndPlayCommand.ExecuteAsync(item);

                if (offsetSeconds != null)
                {
                    SeekCommand.Execute((double)offsetSeconds.Value);
                }
            }
            catch (Exception)
            {
            }
        }
Пример #2
0
        async Task openAndPlay(VideoAudioPair item, int? offsetSeconds = null)
        {
            try
            {
                String name = item.Video != null ? item.Video.Name : item.Audio.Name;

                EventAggregator.GetEvent<TitleChangedEvent>().Publish(name);

                await OpenAndPlayCommand.ExecuteAsync(item);              
                             
                if (offsetSeconds != null)
                {
                    SeekCommand.Execute((double)offsetSeconds.Value);
                }
                
            }
            catch (Exception)
            {

            }

        }
Пример #3
0
        public VideoViewModel(IEventAggregator eventAggregator)
        {
            EventAggregator = eventAggregator;
            VideoSettings = ServiceLocator.Current.GetInstance(typeof(VideoSettingsViewModel)) as VideoSettingsViewModel;
            VideoSettings.SettingsChanged += VideoSettings_SettingsChanged;

            IsInitialized = false;
            isInitializedSignal = new SemaphoreSlim(0, 1);

            CurrentItem = new VideoAudioPair(null, null);
                              
            OpenAndPlayCommand = new AsyncCommand<VideoAudioPair>(async item =>
            {
                await isInitializedSignal.WaitAsync();
                isInitializedSignal.Release();

                try
                {
                    CurrentItem = item;

                    String videoLocation = null;
                    String videoFormatName = null;

                    if (item.Video != null)
                    {
                        videoLocation = item.Video.Location;
                        if (item.Video is MediaStreamedItem)
                        {
                            if (item.Video.Metadata != null)
                            {
                                videoFormatName = MediaFormatConvert.mimeTypeToExtension(item.Video.Metadata.MimeType);
                            }
                        }

                    }

                    String audioLocation = null;
                    String audioFormatName = null;

                    if (item.Audio != null)
                    {
                        audioLocation = item.Audio.Location;

                        if (item.Audio is MediaStreamedItem && item.Audio.Metadata != null)
                        {                          
                            audioFormatName = MediaFormatConvert.mimeTypeToExtension(item.Audio.Metadata.MimeType);                                                       
                        }

                    }

                    CloseCommand.IsExecutable = true;
               
                    IsLoading = true;
                    
                    await VideoPlayer.openAndPlay(videoLocation, videoFormatName, audioLocation, audioFormatName);                   
                                        
                }
                catch (OperationCanceledException)
                {
                    CloseCommand.IsExecutable = false;
                    throw;
                }
                catch (Exception e)
                {
                    CloseCommand.IsExecutable = false;                   
                    MessageBox.Show("Error opening " + item.Video.Location + "\n\n" + e.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    throw;
                }
                finally
                {
                    IsLoading = false;
                  
                }
               
                EventAggregator.GetEvent<TitleChangedEvent>().Publish(CurrentItem.IsEmpty ? null : CurrentItem.Name);              
                
            });

            PlayCommand = new AsyncCommand(async () =>
            {                             
                if (VideoState == VideoPlayerControl.VideoState.CLOSED && !CurrentItem.IsEmpty)
                {
                    await openAndPlay(CurrentItem); 
                }
                else if (VideoState == VideoPlayerControl.VideoState.OPEN || VideoState == VideoPlayerControl.VideoState.PAUSED)
                {
                    VideoPlayer.play();
                }                

            }, false);

            PauseCommand = new Command(() => {
              
                VideoPlayer.pause(); 

            }, false);

            CloseCommand = new AsyncCommand(async () => {
                     
                await VideoPlayer.close(); 

            }, false);

            SeekCommand = new AsyncCommand<double>(async (pos) =>
            {              
                await VideoPlayer.seek(pos); 

            }, false);

            StepForwardCommand = new AsyncCommand(async () =>
                {
                    StepForwardCommand.IsExecutable = false;

                    double timeStamp = VideoPlayer.getFrameSeekTimeStamp() + Settings.Default.VideoStepDurationSeconds;                    
                    await VideoPlayer.seek(timeStamp, VideoLib.VideoPlayer.SeekKeyframeMode.SEEK_FORWARDS);

                    StepForwardCommand.IsExecutable = true;

                }, false);

            StepBackwardCommand = new AsyncCommand(async () =>
            {
                StepBackwardCommand.IsExecutable = false;

                double timeStamp = VideoPlayer.getFrameSeekTimeStamp() - Settings.Default.VideoStepDurationSeconds;                
                await VideoPlayer.seek(timeStamp, VideoLib.VideoPlayer.SeekKeyframeMode.SEEK_BACKWARDS);

                StepBackwardCommand.IsExecutable = true;

            }, false);

            FrameByFrameCommand = new Command(() => {
             
                VideoPlayer.displayNextFrame();
         
            }, false);

            CutVideoCommand = new Command(() =>
            {                
                String outputPath;

                if(FileUtils.isUrl(VideoPlayer.VideoLocation)) {

                    outputPath = MediaFileWatcher.Instance.Path;

                } else {

                    outputPath = FileUtils.getPathWithoutFileName(VideoPlayer.VideoLocation);
                }

                VideoTranscodeView videoTranscode = new VideoTranscodeView();
                videoTranscode.ViewModel.Items.Add(CurrentItem);
                videoTranscode.ViewModel.Title = "Cut Video";
                videoTranscode.ViewModel.IconUri = "/MediaViewer;component/Resources/Icons/videocut.ico";

                videoTranscode.ViewModel.OutputPath = outputPath;
                videoTranscode.ViewModel.IsTimeRangeEnabled = IsTimeRangeEnabled;
                videoTranscode.ViewModel.StartTimeRange = startTimeRangeTimeStamp;
                videoTranscode.ViewModel.EndTimeRange = endTimeRangeTimeStamp;

                String extension = Path.GetExtension(VideoPlayer.VideoLocation).ToLower().TrimStart('.');

                foreach(ContainerFormats format in Enum.GetValues(typeof(ContainerFormats))) {

                    if (format.ToString().ToLower().Equals(extension))
                    {
                        videoTranscode.ViewModel.ContainerFormat = format;
                    }
                }
                
                videoTranscode.ShowDialog();

            }, false);

            ScreenShotCommand = new Command(() =>
            {             
                try
                {
                    String screenShotName = FileUtils.removeIllegalCharsFromFileName(CurrentItem.Video.Name, " ");

                    screenShotName += "." + "jpg";

                    String path = null;

                    switch (Settings.Default.VideoScreenShotSaveMode)
                    {
                        case MediaViewer.Infrastructure.Constants.SaveLocation.Current:
                            {
                                path = MediaFileWatcher.Instance.Path;
                                break;
                            }
                        case MediaViewer.Infrastructure.Constants.SaveLocation.Ask:
                            {
                                DirectoryPickerView directoryPicker = new DirectoryPickerView();
                                DirectoryPickerViewModel vm = (DirectoryPickerViewModel)directoryPicker.DataContext;
                                directoryPicker.Title = "Screenshot Output Directory";
                                vm.SelectedPath = Settings.Default.VideoScreenShotLocation;
                                vm.PathHistory = Settings.Default.VideoScreenShotLocationHistory;

                                if (directoryPicker.ShowDialog() == true)
                                {
                                    path = vm.SelectedPath;
                                }
                                else
                                {
                                    return;
                                }
                                break;
                            }
                        case MediaViewer.Infrastructure.Constants.SaveLocation.Fixed:
                            {
                                path = Settings.Default.VideoScreenShotLocation;
                                break;
                            }
                        default:
                            break;
                    }
                                                                                                      
                    String fullPath = FileUtils.getUniqueFileName(path + "\\" + screenShotName);

                    VideoPlayer.createScreenShot(fullPath, VideoSettings.VideoScreenShotTimeOffset);
                }
                catch (Exception e)
                {
                    MessageBox.Show("Error creating screenshot.\n\n" + e.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }

            }, false);
        
            SetLeftMarkerCommand = new Command(() =>
                {
                    if (IsTimeRangeEnabled == false)
                    {
                        IsTimeRangeEnabled = true;
                    }

                    // get the exact time of the current audio or video frame, positionseconds is potentially inaccurate
                    StartTimeRange = VideoPlayer.PositionSeconds;
                    startTimeRangeTimeStamp = VideoPlayer.getFrameSeekTimeStamp();

                }, false);

            SetRightMarkerCommand = new Command(() =>
            {
                if (IsTimeRangeEnabled == false)
                {
                    IsTimeRangeEnabled = true;
                    StartTimeRange = VideoPlayer.PositionSeconds;
                    startTimeRangeTimeStamp = VideoPlayer.getFrameSeekTimeStamp();
                }
                else
                {
                    EndTimeRange = VideoPlayer.PositionSeconds;
                    endTimeRangeTimeStamp = VideoPlayer.getFrameSeekTimeStamp();
                }

            }, false);

            OpenLocationCommand = new Command(async () =>
            {
                VideoOpenLocationView openLocation = new VideoOpenLocationView();
                                                
                bool? success = openLocation.ShowDialog();
                if (success == true) 
                {
                    MediaItem video = null;
                    MediaItem audio = null;
                    
                    if (!String.IsNullOrWhiteSpace(openLocation.ViewModel.VideoLocation))
                    {
                        video = MediaItemFactory.create(openLocation.ViewModel.VideoLocation);
                        MiscUtils.insertIntoHistoryCollection(Settings.Default.VideoLocationHistory, openLocation.ViewModel.VideoLocation);
                    }

                    if (!String.IsNullOrWhiteSpace(openLocation.ViewModel.AudioLocation))
                    {
                        audio = MediaItemFactory.create(openLocation.ViewModel.AudioLocation);
                        MiscUtils.insertIntoHistoryCollection(Settings.Default.AudioLocationHistory, openLocation.ViewModel.AudioLocation);
                    }

                    await openAndPlay(new VideoAudioPair(video, audio));
                }
              
            });
         
            HasAudio = true;
            VideoState = VideoPlayerControl.VideoState.CLOSED;
           
            IsTimeRangeEnabled = false;
            StartTimeRange = 0;
            EndTimeRange = 0;
            startTimeRangeTimeStamp = 0;
            endTimeRangeTimeStamp = 0;

        }
        string getOutputLocation(VideoAudioPair input)
        {
            String outFilename;

            if (FileUtils.isUrl(input.Location))
            {
                outFilename = FileUtils.removeIllegalCharsFromFileName(input.Name, " ");

                if (String.IsNullOrEmpty(outFilename) || String.IsNullOrWhiteSpace(outFilename))
                {
                    outFilename = "stream";
                }
            }
            else
            {
                outFilename = Path.GetFileNameWithoutExtension(input.Location);
            }

            String outLocation = AsyncState.OutputPath + "\\" + outFilename;

            outLocation += "." + AsyncState.ContainerFormat.ToString().ToLower();

            outLocation = FileUtils.getUniqueFileName(outLocation);

            return outLocation;
        }
Пример #5
0
        public VideoViewModel(IEventAggregator eventAggregator)
        {
            EventAggregator = eventAggregator;
            VideoSettings   = ServiceLocator.Current.GetInstance(typeof(VideoSettingsViewModel)) as VideoSettingsViewModel;
            VideoSettings.SettingsChanged += VideoSettings_SettingsChanged;

            IsInitialized       = false;
            isInitializedSignal = new SemaphoreSlim(0, 1);

            CurrentItem = new VideoAudioPair(null, null);

            OpenAndPlayCommand = new AsyncCommand <VideoAudioPair>(async item =>
            {
                await isInitializedSignal.WaitAsync();
                isInitializedSignal.Release();

                try
                {
                    CurrentItem = item;

                    String videoLocation   = null;
                    String videoFormatName = null;

                    if (item.Video != null)
                    {
                        videoLocation = item.Video.Location;
                        if (item.Video is MediaStreamedItem)
                        {
                            if (item.Video.Metadata != null)
                            {
                                videoFormatName = MediaFormatConvert.mimeTypeToExtension(item.Video.Metadata.MimeType);
                            }
                        }
                    }

                    String audioLocation   = null;
                    String audioFormatName = null;

                    if (item.Audio != null)
                    {
                        audioLocation = item.Audio.Location;

                        if (item.Audio is MediaStreamedItem && item.Audio.Metadata != null)
                        {
                            audioFormatName = MediaFormatConvert.mimeTypeToExtension(item.Audio.Metadata.MimeType);
                        }
                    }

                    CloseCommand.IsExecutable = true;

                    IsLoading = true;

                    await VideoPlayer.openAndPlay(videoLocation, videoFormatName, audioLocation, audioFormatName);
                }
                catch (OperationCanceledException)
                {
                    CloseCommand.IsExecutable = false;
                    throw;
                }
                catch (Exception e)
                {
                    CloseCommand.IsExecutable = false;
                    MessageBox.Show("Error opening " + item.Video.Location + "\n\n" + e.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    throw;
                }
                finally
                {
                    IsLoading = false;
                }

                EventAggregator.GetEvent <TitleChangedEvent>().Publish(CurrentItem.IsEmpty ? null : CurrentItem.Name);
            });

            PlayCommand = new AsyncCommand(async() =>
            {
                if (VideoState == VideoPlayerControl.VideoState.CLOSED && !CurrentItem.IsEmpty)
                {
                    await openAndPlay(CurrentItem);
                }
                else if (VideoState == VideoPlayerControl.VideoState.OPEN || VideoState == VideoPlayerControl.VideoState.PAUSED)
                {
                    VideoPlayer.play();
                }
            }, false);

            PauseCommand = new Command(() => {
                VideoPlayer.pause();
            }, false);

            CloseCommand = new AsyncCommand(async() => {
                await VideoPlayer.close();
            }, false);

            SeekCommand = new AsyncCommand <double>(async(pos) =>
            {
                await VideoPlayer.seek(pos);
            }, false);

            StepForwardCommand = new AsyncCommand(async() =>
            {
                StepForwardCommand.IsExecutable = false;

                double timeStamp = VideoPlayer.getFrameSeekTimeStamp() + Settings.Default.VideoStepDurationSeconds;
                await VideoPlayer.seek(timeStamp, VideoLib.VideoPlayer.SeekKeyframeMode.SEEK_FORWARDS);

                StepForwardCommand.IsExecutable = true;
            }, false);

            StepBackwardCommand = new AsyncCommand(async() =>
            {
                StepBackwardCommand.IsExecutable = false;

                double timeStamp = VideoPlayer.getFrameSeekTimeStamp() - Settings.Default.VideoStepDurationSeconds;
                await VideoPlayer.seek(timeStamp, VideoLib.VideoPlayer.SeekKeyframeMode.SEEK_BACKWARDS);

                StepBackwardCommand.IsExecutable = true;
            }, false);

            FrameByFrameCommand = new Command(() => {
                VideoPlayer.displayNextFrame();
            }, false);

            CutVideoCommand = new Command(() =>
            {
                String outputPath;

                if (FileUtils.isUrl(VideoPlayer.VideoLocation))
                {
                    outputPath = MediaFileWatcher.Instance.Path;
                }
                else
                {
                    outputPath = FileUtils.getPathWithoutFileName(VideoPlayer.VideoLocation);
                }

                VideoTranscodeView videoTranscode = new VideoTranscodeView();
                videoTranscode.ViewModel.Items.Add(CurrentItem);
                videoTranscode.ViewModel.Title   = "Cut Video";
                videoTranscode.ViewModel.IconUri = "/MediaViewer;component/Resources/Icons/videocut.ico";

                videoTranscode.ViewModel.OutputPath         = outputPath;
                videoTranscode.ViewModel.IsTimeRangeEnabled = IsTimeRangeEnabled;
                videoTranscode.ViewModel.StartTimeRange     = startTimeRangeTimeStamp;
                videoTranscode.ViewModel.EndTimeRange       = endTimeRangeTimeStamp;

                String extension = Path.GetExtension(VideoPlayer.VideoLocation).ToLower().TrimStart('.');

                foreach (ContainerFormats format in Enum.GetValues(typeof(ContainerFormats)))
                {
                    if (format.ToString().ToLower().Equals(extension))
                    {
                        videoTranscode.ViewModel.ContainerFormat = format;
                    }
                }

                videoTranscode.ShowDialog();
            }, false);

            ScreenShotCommand = new Command(() =>
            {
                try
                {
                    String screenShotName = FileUtils.removeIllegalCharsFromFileName(CurrentItem.Video.Name, " ");

                    screenShotName += "." + "jpg";

                    String path = null;

                    switch (Settings.Default.VideoScreenShotSaveMode)
                    {
                    case MediaViewer.Infrastructure.Constants.SaveLocation.Current:
                        {
                            path = MediaFileWatcher.Instance.Path;
                            break;
                        }

                    case MediaViewer.Infrastructure.Constants.SaveLocation.Ask:
                        {
                            DirectoryPickerView directoryPicker = new DirectoryPickerView();
                            DirectoryPickerViewModel vm         = (DirectoryPickerViewModel)directoryPicker.DataContext;
                            directoryPicker.Title = "Screenshot Output Directory";
                            vm.SelectedPath       = Settings.Default.VideoScreenShotLocation;
                            vm.PathHistory        = Settings.Default.VideoScreenShotLocationHistory;

                            if (directoryPicker.ShowDialog() == true)
                            {
                                path = vm.SelectedPath;
                            }
                            else
                            {
                                return;
                            }
                            break;
                        }

                    case MediaViewer.Infrastructure.Constants.SaveLocation.Fixed:
                        {
                            path = Settings.Default.VideoScreenShotLocation;
                            break;
                        }

                    default:
                        break;
                    }

                    String fullPath = FileUtils.getUniqueFileName(path + "\\" + screenShotName);

                    VideoPlayer.createScreenShot(fullPath, VideoSettings.VideoScreenShotTimeOffset);
                }
                catch (Exception e)
                {
                    MessageBox.Show("Error creating screenshot.\n\n" + e.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }, false);

            SetLeftMarkerCommand = new Command(() =>
            {
                if (IsTimeRangeEnabled == false)
                {
                    IsTimeRangeEnabled = true;
                }

                // get the exact time of the current audio or video frame, positionseconds is potentially inaccurate
                StartTimeRange          = VideoPlayer.PositionSeconds;
                startTimeRangeTimeStamp = VideoPlayer.getFrameSeekTimeStamp();
            }, false);

            SetRightMarkerCommand = new Command(() =>
            {
                if (IsTimeRangeEnabled == false)
                {
                    IsTimeRangeEnabled      = true;
                    StartTimeRange          = VideoPlayer.PositionSeconds;
                    startTimeRangeTimeStamp = VideoPlayer.getFrameSeekTimeStamp();
                }
                else
                {
                    EndTimeRange          = VideoPlayer.PositionSeconds;
                    endTimeRangeTimeStamp = VideoPlayer.getFrameSeekTimeStamp();
                }
            }, false);

            OpenLocationCommand = new Command(async() =>
            {
                VideoOpenLocationView openLocation = new VideoOpenLocationView();

                bool?success = openLocation.ShowDialog();
                if (success == true)
                {
                    MediaItem video = null;
                    MediaItem audio = null;

                    if (!String.IsNullOrWhiteSpace(openLocation.ViewModel.VideoLocation))
                    {
                        video = MediaItemFactory.create(openLocation.ViewModel.VideoLocation);
                        MiscUtils.insertIntoHistoryCollection(Settings.Default.VideoLocationHistory, openLocation.ViewModel.VideoLocation);
                    }

                    if (!String.IsNullOrWhiteSpace(openLocation.ViewModel.AudioLocation))
                    {
                        audio = MediaItemFactory.create(openLocation.ViewModel.AudioLocation);
                        MiscUtils.insertIntoHistoryCollection(Settings.Default.AudioLocationHistory, openLocation.ViewModel.AudioLocation);
                    }

                    await openAndPlay(new VideoAudioPair(video, audio));
                }
            });

            HasAudio   = true;
            VideoState = VideoPlayerControl.VideoState.CLOSED;

            IsTimeRangeEnabled      = false;
            StartTimeRange          = 0;
            EndTimeRange            = 0;
            startTimeRangeTimeStamp = 0;
            endTimeRangeTimeStamp   = 0;
        }