send a stream chunk by chunk - c#

Im pretty fresh when it comes to streams in c# but i am somewhat familiar with the basics.
I need help setting up the most efficent way of hooking into a stream of unknown length and send the part read to another function until the end of the stream is reached. Could someone have a look at what i hava and help me fill out the part in the while loop, or maybe if while loop is not the best way tell my what is better. Any help is much appreciated.
var processStartInfo = new ProcessStartInfo
{
FileName = "program.exe",
RedirectStandardInput = true,
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true,
Arguments = " -some -arguments"
};
theProcess.StartInfo = processStartInfo;
theProcess.Start();
while (!theProcess.HasExited)
{
int count = 0;
var b = new byte[32768]; // 32k
while ((count = theProcess.StandardOutput.BaseStream.Read(b, 0, b.Length)) > 0)
{
SendChunck() // ?
}
}

You know how many bytes have been read from the original stream through the count variable, so you can just put them into a buffer
while ((count = theProcess.StandardOutput.BaseStream.Read(b, 0, b.Length)) > 0)
{
byte[] actual = b.Take(count).ToArray();
SendChunck(actual);
}
or if your SendChunk method is designed to take a Stream as parameter you can directly pass it the original object:
SendChunck(theProcess.StandardOutput.BaseStream);
and then the method can take care of reading the data in chunks.

Related

FFmpeg. Reading and writing multiple files from a stream

The problem is the following. I need to convert a video to a set of pictures using the ffmpeg process. I have successfully done this before with the following code:
public void VideoToImages1()
{
var inputFile = #"D:\testVideo.avi";
var outputFilesPattern = #"D:\image%03d.jpg";
using var process = new Process
{
StartInfo = new ProcessStartInfo
{
UseShellExecute = false,
CreateNoWindow = true,
Arguments = $"-y -i {inputFile} {outputFilesPattern}",
FileName = "ffmpeg.exe"
},
EnableRaisingEvents = true
};
process.Start();
process.WaitForExit();
}
Now I need to stream video through the input Stream and receive data from the output Stream. For this I have the following code. It's fully working since I used it to convert video and successfully streamed input via Stream and received output via Stream and produced a valid file.
public void VideoToImages2()
{
var inputFile = #"D:\testVideo.avi";
var outputFile = #"D:\resultImages.png";
var process = new Process
{
StartInfo = new ProcessStartInfo
{
RedirectStandardInput = true,
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true,
Arguments = "-y -i - -f image2 -",
FileName = _ffmpeg
},
EnableRaisingEvents = true
};
process.Start();
//Write input data to input stream
var inputTask = Task.Run(() =>
{
using (var input = new FileStream(inputFile, FileMode.Open))
{
input.CopyTo(process.StandardInput.BaseStream);
process.StandardInput.Close();
}
});
//Read multiple files from output stream
var outputTask = Task.Run(() =>
{
//Problem here
using (var output = new FileStream(outputFile, FileMode.Create))
process.StandardOutput.BaseStream.CopyTo(output);
});
Task.WaitAll(inputTask, outputTask);
process.WaitForExit();
}
The problem here is that instead of creating files in a directory according to the specified pattern, it returns these files in a stream. As a result, I do not know how to write all the files from the Stream and how to process this output, since it contains many files. At the moment I have only 1 image created.
Help me please.
I tried googling but didn't find anything useful
You will have to decode the stream on the fly using the signatures of the output files. Any file has a unique signature and by recognizing these signatures you can keep track of the number of bytes related to this file. For example jpeg file starts with signature 0xFF 0xD8 0xFF and ends with signature 0xF9 xF8 if I'm not mistaken. Matching bytes with these signatures will have to find the files contained in the stream.

Replacing CRLF in PNG file

I am trying to replace CRLF with LF (see reason # Read binary stdout data from adb shell?)
A brief summary of the above referenced thread is that basically when piping a screenshot from an Android device via the Android debug bridge, it looks like the line feeds line endings in the data stream are being replaced with carriage return line feeds, therefore I am receiving a corrupted file at the other end of the pipe. What has worked for others is undoing the replacement via code, as below, but it doesn't seem to be working from me.
My code is still spitting a corrupted file... any ideas why?
++ I know the code isn't as clean and efficient as it can be, will fix up after so please hold the comments related to my coding skill, or lack thereof.
Thanks
static void Main(string[] args)
{
// adb shell screencap -p > <path>
string path = #"<filepath>\screen.png";
var fs = new FileStream(path, FileMode.Open);
var fsw = new FileStream(path.Replace(".png", "_fixed.png"), FileMode.Create);
var buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
fs.Flush();
fs.Close();
var switched = Repair(buffer);
fsw.Write(switched, 0, switched.Length);
fsw.Flush();
fsw.Close();
Console.WriteLine(buffer.Length);
Console.WriteLine(switched.Length);
Console.Read();
}
static byte[] Repair(byte[] enc)
{
var bstr = new MemoryStream();
for (int i = 0; i < enc.Length; i++)
{
if (enc.Length > i + 1 && enc[i] == 0x0d && enc[i + 1] == 0x0a)
{
bstr.WriteByte(0x0a);
i++;
}
else bstr.WriteByte(enc[i]);
}
bstr.Flush();
bstr.Close();
return bstr.ToArray();
}

Why ZipEntry.Size always -1

I read ZipInputStream from a stream. There are 10 ZipEntries, but size of all of them is -1! I can't figure out why, because there are data, so it must be > 0. Here's my code:
var zipInputStream = new ZipInputStream(new MemoryStream(reports));
ZipEntry zipEntry;
while ((zipEntry = zipInputStream.GetNextEntry()) != null)
{
var fileName = Path.GetFileName(zipEntry.Name);
if (String.IsNullOrEmpty(fileName)) continue;
var identifier = fileName.Split('.')[1];
var buffer = new byte[zipEntry.Size];
zipInputStream.Read(buffer, 0, buffer.Length);
var report = encoding.GetString(buffer);
...
}
And on the line var buffer = new byte[zipEntry.Size] I've got an OverflowException. When I check zipEntry.Size - it's always -1. If I write var buffer = new byte[4096] for example it's ok, but not correct. Any thoughts, please? Thanks in advance!
Here, 0 would indicate "no data"; -1 is indicating that it doesn't know the size of the data. Your best bet, then, is to read to the end of that entry. Perhaps:
MemoryStream ms = new MemoryStream();
while ((zipEntry = zipInputStream.GetNextEntry()) != null)
{
var fileName = Path.GetFileName(zipEntry.Name);
if (String.IsNullOrEmpty(fileName)) continue;
var identifier = fileName.Split('.')[1];
ms.SetLength(0); // reset between iterations, but let it re-use the memory
zipInputStream.CopyTo(ms);
var report = encoding.GetString(ms.GetBuffer(), 0, (int)ms.Length);
}

c#: Overlay mp3 file to another

I have merged 3 mp3 files into one mp3 file simply using the following code
using (var fs = File.OpenWrite(Path.Combine(txtOutputFile.Text, "new.mp3")))
{
var buffer = File.ReadAllBytes(Path.Combine(txtIntroLoc.Text, fileName1));
fs.Write(buffer, 0, buffer.Length);
buffer = File.ReadAllBytes(Path.Combine(txtOutroloc.Text, fileName2));
fs.Write(buffer, 0, buffer.Length);
buffer = File.ReadAllBytes(Path.Combine(txtFileThree.Text, fileName3));
fs.Write(buffer, 0, buffer.Length);
fs.Flush();
}
What i want is to overlay another mp3 file which will play in the background of this newly created mp3 file. I know its possible but not getting right way to go to acheive this. Any help will be great.
thanks
On the question of "overlaying":
There is no way of doing this without decoding the original to PCM, mixing in your overlay, and then re-encoding the whole thing to MP3.
On your existing code:
You can just about get away with concatenating MP3 files like this. Usually though I would at recommend ditching the ID3 tags, and just making one file that has the MP3 frames from each file. If you are using NAudio, then you can use the ReadNextFrame() method on Mp3FileReader to get each MP3 frame and write its RawBytes out to the file.
For best results, you'd want all the MP3 files to use the same sample rate and channel count. Also, if these are VBR, you'll be invalidating the information in the XING or VBRI headers, so it might even be best to ditch those as well.
Finally i found a solution. Using NAudio we can mix the wav stream so first converting the mp3 to wav and then mixing the wav files and then re convert the resulted wav file to mp3 using the lame.exe.
Convert MP3 to WAV can be performed using the following piece of code using NAudio library thanks to Mark Heath.
string file = "new.mp3";
Mp3FileReader readers = new Mp3FileReader(file);
WaveFormat targetFormat = new WaveFormat();
WaveStream convertedStream = new WaveFormatConversionStream(targetFormat, readers);
WaveFileWriter.CreateWaveFile("firstwav.wav", convertedStream);
Now mixing it with another wav file can be performed using this code consuming the NAudio classes.
string[] inputFiles = new string[2];
Stream output = new MemoryStream();
inputFiles[0] = "firstwav.wav";
inputFiles[1] = "secondwav.wav";
mixWAVFiles(inputFiles);
The mixWAVFiles Method
public void mixWAVFiles(string[] inputFiles)
{
int count = inputFiles.GetLength(0);
WaveMixerStream32 mixer = new WaveMixerStream32();
WaveFileReader[] reader = new WaveFileReader[count];
WaveChannel32[] channelSteam = new WaveChannel32[count];
mixer.AutoStop = true;
for (int i = 0; i < count; i++)
{
reader[i] = new WaveFileReader(inputFiles[i]);
channelSteam[i] = new WaveChannel32(reader[i]);
mixer.AddInputStream(channelSteam[i]);
}
mixer.Position = 0;
WaveFileWriter.CreateWaveFile("mixedWavFile.wav", mixer);
}
And now finally converting the finalwav file to mp3 using lame.exe found here
public void convertWAVtoMP3(string wavfile)
{
//string lameEXE = #"C:\Users\Jibran\Desktop\MP3 Merger\bin\Debug\lame.exe";
string lameEXE = Path.GetDirectoryName(Application.ExecutablePath) +"/lame.exe";
string lameArgs = "-V2";
string wavFile = wavfile;
string mp3File = "mixed.mp3";
Process process = new Process();
process.StartInfo = new ProcessStartInfo();
process.StartInfo.FileName = lameEXE;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo.Arguments = string.Format(
"{0} {1} {2}",
lameArgs,
wavFile,
mp3File);
process.Start();
process.WaitForExit();
int exitCode = process.ExitCode;
}

FileResult buffered to memory

I'm trying to return large files via a controller ActionResult and have implemented a custom FileResult class like the following.
public class StreamedFileResult : FileResult
{
private string _FilePath;
public StreamedFileResult(string filePath, string contentType)
: base(contentType)
{
_FilePath = filePath;
}
protected override void WriteFile(System.Web.HttpResponseBase response)
{
using (FileStream fs = new FileStream(_FilePath, FileMode.Open, FileAccess.Read))
{
int bufferLength = 65536;
byte[] buffer = new byte[bufferLength];
int bytesRead = 0;
while (true)
{
bytesRead = fs.Read(buffer, 0, bufferLength);
if (bytesRead == 0)
{
break;
}
response.OutputStream.Write(buffer, 0, bytesRead);
}
}
}
}
However the problem I am having is that entire file appears to be buffered into memory. What would I need to do to prevent this?
You need to flush the response in order to prevent buffering. However if you keep on buffering without setting content-length, user will not see any progress. So in order for users to see proper progress, IIS buffers entire content, calculates content-length, applies compression and then sends the response. We have adopted following procedure to deliver files to client with high performance.
FileInfo path = new FileInfo(filePath);
// user will not see a progress if content-length is not specified
response.AddHeader("Content-Length", path.Length.ToString());
response.Flush();// do not add anymore headers after this...
byte[] buffer = new byte[ 4 * 1024 ]; // 4kb is a good for network chunk
using(FileStream fs = path.OpenRead()){
int count = 0;
while( (count = fs.Read(buffer,0,buffer.Length)) >0 ){
if(!response.IsClientConnected)
{
// network connection broke for some reason..
break;
}
response.OutputStream.Write(buffer,0,count);
response.Flush(); // this will prevent buffering...
}
}
You can change buffer size, but 4kb is ideal as lower level file system also reads buffer in chunks of 4kb.
Akash Kava is partly right and partly wrong. You DO NOT need to add the Content-Length header or do the flush afterward. But you DO, need to periodically flush response.OutputStream and then response. ASP.NET MVC (at least version 5) will automatically convert this into a "Transfer-Encoding: chunked" response.
byte[] buffer = new byte[ 4 * 1024 ]; // 4kb is a good for network chunk
using(FileStream fs = path.OpenRead()){
int count = 0;
while( (count = fs.Read(buffer,0,buffer.Length)) >0 ){
if(!response.IsClientConnected)
{
// network connection broke for some reason..
break;
}
response.OutputStream.Write(buffer,0,count);
response.OutputStream.Flush();
response.Flush(); // this will prevent buffering...
}
}
I tested it and it works.

Categories