public IAudioStream OpenFile(FileInfo fileInfo)
        {
            if (FFmpegSourceStream.WaveProxySuggested(fileInfo))
            {
                Console.WriteLine("File format with known seek problems, creating proxy file...");
                return(AudioStreamFactory.FromFileInfo(FFmpegSourceStream.CreateWaveProxy(fileInfo)));
            }
            else
            {
                try
                {
                    FFmpegSourceStream stream = new FFmpegSourceStream(fileInfo);

                    // Make a seek to test if it works or if it throws an exception
                    stream.Position = 0;

                    return(stream);
                }
                catch (FFmpegSourceStream.FileNotSeekableException)
                {
                    /*
                     * This exception gets thrown if a file is not seekable and therefore cannot
                     * provide all the functionality that is needed for an IAudioStream, although
                     * the problem could be solved by creating a seek index. See FFmpegSourceStream
                     * for further information.
                     *
                     * For now, we create a WAV proxy file, because it is easier (consumes
                     * additional space though).
                     */
                    Console.WriteLine("File not seekable, creating proxy file...");
                    return(AudioStreamFactory.FromFileInfo(FFmpegSourceStream.CreateWaveProxy(fileInfo)));
                }
                catch (FFmpegSourceStream.FileSeekException)
                {
                    /*
                     * This exception gets thrown if a file should be seekable but seeking still does
                     * not work correctly. We also create a proxy in this case.
                     */
                    Console.WriteLine("File test seek failed, creating proxy file...");
                    return(AudioStreamFactory.FromFileInfo(FFmpegSourceStream.CreateWaveProxy(fileInfo)));
                }
                catch (DllNotFoundException e)
                {
                    throw new DllNotFoundException("Cannot open file through FFmpeg: DLL missing", e);
                }
            }
        }
        private void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker     = sender as BackgroundWorker;
            Parameters       parameters = (Parameters)e.Argument;

            var streams = parameters.SourceFiles.Select(fi => AudioStreamFactory.FromFileInfo(fi));

            // load source stream
            var sourceStream = new ConcatenationStream(streams.ToArray());

            var    firstFile            = parameters.SourceFiles.First();
            string targetFileNamePrefix = firstFile.FullName.Remove(firstFile.FullName.Length - firstFile.Extension.Length);
            string targetFileNameSuffix = firstFile.Extension;

            int        partCount  = 0;
            Random     random     = new Random();
            CropStream cropStream = new CropStream(sourceStream, 0, 0);

            while (sourceStream.Position < sourceStream.Length)
            {
                partCount++;
                int  length     = random.Next(parameters.MinLength, parameters.MaxLength); // length in seconds of the current part to write
                long byteLength = TimeUtil.TimeSpanToBytes(new TimeSpan(TimeUtil.SECS_TO_TICKS * length), cropStream.Properties);

                Debug.WriteLine("writing part " + partCount + " (" + length + " secs = " + byteLength + " bytes)");
                Debug.WriteLine("before: " + cropStream.Begin + " / " + cropStream.End + " / " + cropStream.Position + " / " + sourceStream.Position);
                cropStream.Begin    = cropStream.End;
                cropStream.End     += sourceStream.Length - cropStream.Begin < byteLength ? sourceStream.Length - cropStream.Begin : byteLength;
                cropStream.Position = 0;
                Debug.WriteLine("after : " + cropStream.Begin + " / " + cropStream.End + " / " + cropStream.Position + " / " + sourceStream.Position);
                AudioStreamFactory.WriteToFile(cropStream, String.Format("{0}.part{1:000}{2}", targetFileNamePrefix, partCount, targetFileNameSuffix));

                if (worker.CancellationPending)
                {
                    e.Cancel = true;
                    Debug.WriteLine("canceled");
                    return;
                }
                worker.ReportProgress((int)((double)sourceStream.Position / sourceStream.Length * 100));
            }
            Debug.WriteLine("finished");
        }
示例#3
0
 public AudioTrack(FileInfo[] fileInfos, bool initialize)
     : base(fileInfos)
 {
     this.TimeWarps = new TimeWarpCollection();
     if (initialize)
     {
         using (IAudioStream stream = AudioStreamFactory.FromFileInfo(FileInfo)) {
             sourceProperties = stream.Properties;
             if (MultiFile)
             {
                 // For multi-file tracks, we need to get a concatenated stream of all files for the length
                 InitializeLength();
             }
             else
             {
                 // Single-file tracks can just reuse this stream to get the length
                 InitializeLength(stream);
             }
         }
     }
 }