This following is a code to join two videos. When I run the program it joins two videos and puts joined video in a folder. The joined video size is correct as it should be.
But when I play the video it plays the first part of the video in WMP but when i play the video in VLC it plays the second part of video.
public void JoiningVideo()
{
string j = #"D:/test2";
string outputpath = #"D:/test3/beforeEventab1.wmv";
DirectoryInfo di = new DirectoryInfo(j);
FileStream fs;
fs = new FileStream(outputpath, FileMode.Append);
foreach (FileInfo fi in di.GetFiles(#"*.wmv"))
{
byte[] bytesource = System.IO.File.ReadAllBytes(fi.FullName);
fs.Write(bytesource, 0, bytesource.Length);
}
fs.Close();
}
You know that each video-file starts with something called "header" ?
This part of the file contains information about the length etc.
If you want to join to seperate video files, you have to merge the headers to a new one containing information about both (joined) parts and to make sure that both videos fit to each other. (*)
Otherwise the video is not a valid file.
Due to the differences of the decoders of WMP and VLC, one recognises the first and the other one recognises the second file.
You can be lucky of that the the programs even played this 'corrupt' file! ;)
Just ask a search engine about merge wmv for a solution that should work for you!
(*)
To merge two videos they need to have
The same format (e.g. Resolution, Framerate, Bitrate)
If this does not apply, at least one of them has to be converted to match the other video
The videos have to be 'glued' together, it is not sufficient to append one's data to the other one.
Each video starts with a header. This header has to be changed to comprise information about the new (joined) video.
Also the raw image data cannot be simply appended. Every image is like a piece of a puzzle fitting to the next image in the video. The transition is like a new piece of a puzzle that has to be created. It may even be necessary to change/reorder the whole second file in order to get a working transition.
I am not a specialist, but at leas I can tell you, that this procedure is different for each type (MPEG, WMV, ..) of video. The best approach is to use an existing library for this purpose.
Related
I need to make noise on one audio file. I have the first audio (data) and the second audio (noise). How to put noise on the first audio so that it is looped. Or, if the first audio is shorter than the audio noise, then the result was the duration of the first audio.
I gave an example of their tutorial. But there is simply an overlay of one file on another. Without the possibility of looping and length optimization
using(var reader1 = new AudioFileReader("file1.wav"))
using(var reader2 = new AudioFileReader("file2.wav"))
{
var mixer = new MixingSampleProvider(new[] { reader1, reader2 });
WaveFileWriter.CreateWaveFile16("mixed.wav", mixer);
}
You could use a looping technique like the one I describe here to loop the shorter file (would then need to follow that with a ToSampleProvider).
And then use the Take extension method on your MixingSampleProvider to ensure you only read the duration of the longer file, so you don't create an infinitely large WAV file.
I am attempting to propagate a single sound source to multiple outputs (such as one microphone input to multiple sound cards or channels). The output does not have to be sync'd (a few ms delay is acceptable) but it would be nice if it could be sync'd.
I have successfully written code that loops a microphone input to an output using a WaveIn, a BufferedWaveProvider, and a WaveOut. However when I try to read one BufferedWaveProvider with two instances of WaveOut, the two outputs create this odd 'interleaved' choppy sound. Here is a code snippet for the output portion;
private void CreateWaveOutDevice()
{
waveProvider = new BufferedWaveProvider(waveIn.WaveFormat);
waveOut = new WaveOut();
waveOut.DeviceNumber = 0; //Sound card 1
waveOut.DesiredLatency = 100;
waveOut.Init(waveProvider);
waveOut.PlaybackStopped += wavePlayer_PlaybackStopped;
waveOut2 = new WaveOut();
waveOut2.DeviceNumber = 1; //Sound card 2
waveOut2.DesiredLatency = 100;
waveOut2.Init(waveProvider);
waveOut2.PlaybackStopped += wavePlayer_PlaybackStopped;
waveOut.Play();
waveOut2.Play();
}
I think the reason this is happening is because when the waveProvider circular buffer is read, the data is deleted so the two read methods are 'fighting' over the data which results in the choppy sound.
So I really have two questions;
1.) I see the Naudio library contains many types of waveStreams (RawSourceWaveStream is particularly interesting) However, I have been unable to find a good example of how to read a single stream with multiple waveOut methods. I have also been unable to create working code using waveStream with multiple outputs. Is anyone familiar with waveStreams and knows if this is something that can be done?
2.) If the Naudio wave streams cannot be used in a single producer multiple consumer situation then I believe I would need to make a circular buffer that is not cleared on a read, but only when the buffer is full and new data is pushed in. The code won't care if the data was read or not it just keeps filling the buffer. Would this be the correct approach?
I've spent days searching so hopefully this hasn't already been asked. Thanks for reading.
If you're just reading from a microphone and want two WaveOut's to play it, then the simple option is to create two BufferedWaveProviders, one for each WaveOut, and then when audio is received, send it to both.
Likewise if you were playing from an audio file to two soundcards, the easiest way is to use two reader objects and start them both separately.
There is unfortunately no easy way to synchronize, short of starting and stopping both players at the same time.
There are a few more advanced ways to try to split off an audio stream to two readers, but there can be complications especially if the two readers are not able to read at roughly the same rate.
I have a requirement to play the sound (.wav file) only on the left channel of teh headphone and another file to play only on the right channel of the headphone.
I am new to c #, please help me out to solve this problem.
I don't think that WPF alone can do that, but you might want to check out NAudio.
var input2 = new Mp3FileReader(#"C:\Users\Public\Music\Sample Music\Kalimba.mp3");
var input1 = new Mp3FileReader(#"C:\Users\Public\Music\Sample Music\Maid with the Flaxen Hair.mp3");
MultiplexingWaveProvider waveProvider = new MultiplexingWaveProvider(new IWaveProvider[] { input1, input2 }, 2);
waveProvider.ConnectInputToOutput(1, 1);
WaveOut wave = new WaveOut();
wave.Init(waveProvider);
wave.Play();
I just added the following lines to the above code and it works with 2 channels
waveProvider.ConnectInputToOutput(1, 0);
waveProvider.ConnectInputToOutput(3, 1);
and removed
waveProvider.ConnectInputToOutput(1, 1);
This article on the code project shows a way to visualize audio.
If you can actively visualize audio you will understand that your common stereo wave file contains two channels (A left and a right channel). With that information on how to visualize it you can easily adapt the stream to destroy the left channel (E.G. put zero's in the left stream/right stream). And thus when you would play it the regular way after that it would only play music in the left or the right ear.
The above is the easy part when you have a stereo file which has two equal channels.
There is also the case where your stereo file has different channels (E.G. a guitar on the left ear a drum on the right ear.) In that case you will have to merge two channels to one channel, find duplicate information and finally destroy the other channel you don't want to use.)
The third possibility is your audio file is mono and thus only has one channel. Normally your computer will automatically duplicate the channel from a mono file to a stereo file. So it plays on both ears/speakers. Thus we will never here that the file is mono. However in programming if you would want to make it play on just one ear/channel. You would have to manually convert it to a stereo file (by adding a blank channel). And then again you would have sound on just one ear/speaker.
Hope this helps.
I am currently trying to write an Audio Player in C#. I am using BASS library to deal with playing music but now i have small problem with getting length of the song.
Well i have read BASS doc and found a way:
"All" i need to do is
int stream = Bass.BASS_StreamCreateFile(filepath,....);
int length = Bass.BASS_ChannelBytes2Seconds(stream, Bass.BASS_ChannelGetLength(stream));
And in most of cases i get valid length of song. And here the problem starts. As far as i know the stream creation operation is quite expensive (correct me if i am mistaken) and creating a stream only to get length of the song looks a little silly.
So my question is: Is there any other way to get it without creating steam file (not being so expensive). I will be thinking later about reading id3 tags. Is creating that stream "evil that must be done no matter what" and even if i would try to get it with other library it would do exactly the same thing?
You can use the Microsoft.WindowsAPICodePack.Shell:
using Microsoft.WindowsAPICodePack.Shell;
Then code like so:
string file = "myfile.mp3"
ShellFile so = ShellFile.FromFilePath(file);
double 100nanoseconds;
double.TryParse(so.Properties.System.Media.Duration.Value.ToString(), out 100nanoseconds);
There is a code project that could help you as well
I have a library of different words/phrases, and in order to build sentences at present I add a combination of these phrases into a playlist to make a sentence. Unfortunately if the user is running CPU intensive applications (which most of my users are) there can be a lag of a few seconds mid-sentence (in between phrases).
In order to combat this I was thinking of an approach which will merge the right combination of MP3 files on the fly into an appropriate phrase, save this in the %temp% directory, and then play this 1 MP3 file which should overcome the problem I'm experiencing with gaps.
What is the easiest way to do this in C#? Is there an easy way to do this? The files are fairly small, 3-4 seconds long each, and a sentence can consist of 3-20ish phrases.
here's how you can concatenate MP3 files using NAudio:
public static void Combine(string[] inputFiles, Stream output)
{
foreach (string file in inputFiles)
{
Mp3FileReader reader = new Mp3FileReader(file);
if ((output.Position == 0) && (reader.Id3v2Tag != null))
{
output.Write(reader.Id3v2Tag.RawData, 0, reader.Id3v2Tag.RawData.Length);
}
Mp3Frame frame;
while ((frame = reader.ReadNextFrame()) != null)
{
output.Write(frame.RawData, 0, frame.RawData.Length);
}
}
}
see here for more info
MP3 files consist of "frames", that each represent a short snippet (I think around 25 ms) of audio.
So yes, you can just concatenate them without a problem.
As MP3s are a compressed audio source, I imagine that you can't just concatenate them into a single file without decoding each one first to the wave form that it would play. This may be quite intensive. Perhaps you could cheat by using a critical section when playing back your phrase so that the CPU is not stolen from you until the phrase was complete. This isn't necessarily playing nice with other threads but might work if your phrases are short.
On simple option is to shell to the command line:
copy /b *.mp3 c:\new.mp3
Better would be to concatenate the streams. That's been answered here:
What would be the fastest way to concatenate three files in C#?