public static EncodingParameters ParseCommandLine(string[] args) { EncodingParameters result = new EncodingParameters(); string pendingOption = null; foreach (string param in args) { // check if previous argument was an option, so this argument is the option value if (pendingOption != null) { if (string.Compare(pendingOption, "-b", StringComparison.CurrentCultureIgnoreCase) == 0) { byte bitrate; if (byte.TryParse(param, out bitrate)) { result.Bitrate = bitrate; pendingOption = null; } else { throw new ArgumentOutOfRangeException("Invalid bitrate \"" + param + "\""); } } else if (string.Compare(pendingOption, "-o", StringComparison.CurrentCultureIgnoreCase) == 0) { result.OutputFolder = param; pendingOption = null; } else if (string.Compare(pendingOption, "--tt", StringComparison.CurrentCultureIgnoreCase) == 0) { result.Title = param; pendingOption = null; } else if (string.Compare(pendingOption, "--ta", StringComparison.CurrentCultureIgnoreCase) == 0) { result.Artist = param; pendingOption = null; } else if (string.Compare(pendingOption, "--tl", StringComparison.CurrentCultureIgnoreCase) == 0) { result.Album = param; pendingOption = null; } else if (string.Compare(pendingOption, "--ty", StringComparison.CurrentCultureIgnoreCase) == 0) { result.Year = param; pendingOption = null; } else if (string.Compare(pendingOption, "--tg", StringComparison.CurrentCultureIgnoreCase) == 0) { result.Genre = param; pendingOption = null; } else if (string.Compare(pendingOption, "--tn", StringComparison.CurrentCultureIgnoreCase) == 0) { byte trackNumber; if (byte.TryParse(param, out trackNumber)) { result.TrackNumber = trackNumber; pendingOption = null; } else { throw new ArgumentOutOfRangeException("Invalid track number \"" + param + "\""); } } else if (string.Compare(pendingOption, "-V", StringComparison.CurrentCultureIgnoreCase) == 0) { byte quality; if (byte.TryParse(param, out quality)) { result.Quality = quality; pendingOption = null; } else { throw new ArgumentOutOfRangeException("Invalid quality \"" + param + "\""); } } } else if (param.StartsWith("-")) { pendingOption = param; } else if (result.InputFilename == null) { result.InputFilename = param; } else if (result.OutputFilename == null) { result.OutputFilename = param; } else { // add something to the ignored parameter list ;-) } } return(result); }
static void Main(string[] args) { Console.WriteLine("WmaEncoder.Net"); if (args.Length < 2) { PrintUsageHelp(); return; } EncodingParameters encodingParams = Parser.ParseCommandLine(args); // make sure they've at least specified a filename! if (string.IsNullOrWhiteSpace(encodingParams.InputFilename)) { Console.WriteLine("No input file specified"); return; } if (string.IsNullOrWhiteSpace(encodingParams.OutputFilename) && string.IsNullOrWhiteSpace(encodingParams.OutputFolder)) { Console.WriteLine("No output filename or folder specified"); return; } // next, try to open the specified filename. If we can't open it, no need to bother with anything else MediaItem mi = null; try { mi = new MediaItem(encodingParams.InputFilename); } catch (InvalidMediaFileException e) { Console.WriteLine("Error opening source file: " + e.Message); return; } // if an output filename is specified, use that. Otherwise, try to use the output folder if (!string.IsNullOrWhiteSpace(encodingParams.OutputFilename)) { mi.OutputFileName = Path.GetFileName(encodingParams.OutputFilename); } else { mi.OutputFileName = Path.GetFileName(encodingParams.InputFilename); } if (!string.IsNullOrEmpty(encodingParams.Title)) { mi.Metadata["Title"] = encodingParams.Title; } if (!string.IsNullOrEmpty(encodingParams.Album)) { mi.Metadata["WM/AlbumTitle"] = encodingParams.Album; } if (!string.IsNullOrEmpty(encodingParams.Artist)) { mi.Metadata["Author"] = encodingParams.Artist; } if (encodingParams.TrackNumber > 0) { mi.Metadata["WM/TrackNumber"] = Convert.ToString(encodingParams.TrackNumber); } if (!string.IsNullOrEmpty(encodingParams.Genre)) { mi.Metadata["WM/Genre"] = encodingParams.Genre; } if (!string.IsNullOrEmpty(encodingParams.Year)) { mi.Metadata["WM/Year"] = encodingParams.Year; } ObservableCollection <AudioProfile> codecProfiles = new ObservableCollection <AudioProfile>(); Microsoft.Expression.Encoder.Profiles.LocalProfiles.EnumerateAudioCodecs(codecProfiles); AudioCodec selectedCodec = AudioCodec.Wma; if (encodingParams.Quality == 100) { selectedCodec = AudioCodec.WmaLossless; if (encodingParams.Bitrate < 0) { encodingParams.Bitrate = 44100; } } else { selectedCodec = AudioCodec.Wma; if (encodingParams.Bitrate < 0) { encodingParams.Bitrate = 128; } } // find the correct codec AudioProfile selectedAudioProfile = null; foreach (AudioProfile audioProfile in codecProfiles) { if (encodingParams.Quality > 0) { if (encodingParams.Quality == 100) { if ((audioProfile.BitsPerSample == 16) && (audioProfile.Codec == selectedCodec) && (audioProfile.Channels == 2) && (audioProfile.SamplesPerSecond == encodingParams.Bitrate)) { selectedAudioProfile = audioProfile; break; } } else if (audioProfile.Bitrate is Microsoft.Expression.Encoder.Profiles.VariableQualityBitrate /* (audioProfile.Bitrate.IsVariableBitrate == true) */) { Microsoft.Expression.Encoder.Profiles.VariableQualityBitrate vqb = (Microsoft.Expression.Encoder.Profiles.VariableQualityBitrate)audioProfile.Bitrate; if (vqb.Quality == encodingParams.Quality) { selectedAudioProfile = audioProfile; break; } } } else { if ((audioProfile.BitsPerSample == 16) && (audioProfile.Codec == selectedCodec) && (audioProfile.Channels == 2) && (audioProfile.Bitrate.BitrateValue == encodingParams.Bitrate)) { selectedAudioProfile = audioProfile; break; } } } if (selectedAudioProfile != null) { mi.OutputFormat.AudioProfile = selectedAudioProfile; } else { throw new Exception("Unable to find a matching codec profile"); } Console.WriteLine("Initializing Expression Encoder 4 SDK..."); Job job = new Job(); job.MediaItems.Add(mi); job.CreateSubfolder = false; if (string.IsNullOrWhiteSpace(encodingParams.OutputFolder)) { job.OutputDirectory = Path.GetDirectoryName(Path.GetFullPath(encodingParams.OutputFilename)); } else { DirectoryInfo di = new DirectoryInfo(encodingParams.OutputFolder); job.OutputDirectory = di.FullName; } job.EncodeProgress += new EventHandler <EncodeProgressEventArgs>(job_EncodeProgress); job.Encode(); }