Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stuck requesting frame #1

Merged
merged 7 commits into from
Jun 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 0 additions & 30 deletions FFMPEG.cs

This file was deleted.

3 changes: 1 addition & 2 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using CommandLine;
using System;
using System.IO;

namespace StreamLapse
{
Expand All @@ -15,7 +14,7 @@ static void Main(string[] args)
Parser.Default.ParseArguments<Options>(args).WithParsed(RunOptions);
}

catch (Exception ex) when (ex is FormatException || ex is IOException)
catch (Exception ex) when (ex is FormatException)
{
Console.WriteLine(ex.Message);
}
Expand Down
4 changes: 2 additions & 2 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("1.1.0.0")]
[assembly: AssemblyFileVersion("1.1.0.0")]
8 changes: 4 additions & 4 deletions StreamLapse.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="FFMPEG.cs" />
<Compile Include="Options.cs" />
<Compile Include="src\FFMPEG.cs" />
<Compile Include="src\Options.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="StreamHandler.cs" />
<Compile Include="Validation.cs" />
<Compile Include="src\StreamHandler.cs" />
<Compile Include="src\Validation.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
Expand Down
52 changes: 52 additions & 0 deletions src/FFMPEG.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;

namespace StreamLapse
{
public class FFMPEG
{
const int MAX_WAIT_TIME_MS = 10 * 1000;

public static bool CaptureStreamFrame(string streamURL, string filenameFullPath)
{
Console.WriteLine("\nRequesting new frame...");

Task captureOneFrame = Task.Run(() =>
{
using (Process p = new Process())
{
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.FileName = "ffmpeg.exe";
p.StartInfo.Arguments = $"-loglevel quiet -rtsp_transport tcp -i {streamURL} -vframes 1 {filenameFullPath}";
p.Start();
p.WaitForExit();
}
});

Task timeoutTask = Task.Delay(MAX_WAIT_TIME_MS);

int completedTaskIndex = Task.WaitAny(captureOneFrame, timeoutTask);

if (completedTaskIndex == 1)
{
Console.WriteLine("\n - Timeout reached.\n");

Beep();
}

return File.Exists(filenameFullPath);
}

static void Beep()
{
Console.Beep(1000, 100);

Console.Beep(800, 100);

Console.Beep(500, 100);
}
}
}
File renamed without changes.
32 changes: 26 additions & 6 deletions StreamHandler.cs → src/StreamHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace StreamLapse
{
Expand All @@ -15,6 +14,8 @@ public class StreamHandler
private int _captureIntervalInMilliseconds;
private long _totalBytesWrited;

const byte MAX_RETRIES_TO_SAVE_FRAME = 5;

/// <summary>
/// Handle connection to a camera stream.
/// </summary>
Expand Down Expand Up @@ -81,17 +82,36 @@ public void SaveFrameToJPGFile()

string filenameFullPath = GenerateImageNameOutputPath(filename);

if (File.Exists(filenameFullPath)) throw new IOException($"Failed to save image {filenameFullPath} because it already exists.");
if (File.Exists(filenameFullPath))
{
Console.WriteLine($"{filename} already existis! Skipping...");

return;
}

stopwatch.Start();

bool frameWasSaved = false;

while(!frameWasSaved)
byte retries = 0;

while (!frameWasSaved && retries < MAX_RETRIES_TO_SAVE_FRAME)
{
frameWasSaved = FFMPEG.CaptureStreamFrame(_streamURL, filenameFullPath);

if (!frameWasSaved) Console.WriteLine($"Failed to save the frame: {filename}. Retrying...");
if (!frameWasSaved)
{
Console.WriteLine($"Failed to save the frame: {filename}. Retrying {++retries}/{MAX_RETRIES_TO_SAVE_FRAME}...");
}
}

if (!frameWasSaved)
{
Console.WriteLine($"\nFailed to save the frame: {filename}. Decrementing the index to next try...");

_frameCount--;

return;
}

stopwatch.Stop();
Expand Down Expand Up @@ -135,9 +155,9 @@ private void PrintStatus(string filename, double timeSpent)

Console.WriteLine($"File {filename} saved in {timeSpent} seconds.\n");

Console.WriteLine("Total frames saved at now: " + _frameCount + '\n');
Console.WriteLine("Total bytes writed in this session: " + FormatFileSize(_totalBytesWrited) + '\n');

Console.WriteLine("Total filesize of images: " + FormatFileSize(_totalBytesWrited));
Console.WriteLine("Total frames saved: " + _frameCount);
}

/// <summary>
Expand Down
File renamed without changes.