[FFmpeg-user] watch a folder for new images

Michael Koch astroelectronic at t-online.de
Mon Jan 31 11:27:53 EET 2022


Am 30.01.2022 um 15:40 schrieb Gyan Doshi:
>
>
> On 2022-01-30 07:25 pm, Michael Koch wrote:
>> Am 30.01.2022 um 14:29 schrieb Gyan Doshi:
>>>
>>>
>>> On 2022-01-30 06:36 pm, Michael Koch wrote:
>>>>
>>>> The problem is that FFmpeg seems to read the link only one time. 
>>>> When I change the link target (while FFmpeg is running) with the 
>>>> copy command, the link is actually changed but FFmpeg is still 
>>>> showing the first image.
>>>> When I stop and restart FFmpeg, then it shows the new image.
>>>
>>> Add -readrate 1 before the input.
>>
>> That didn't work when I tested it.
>> The combination of -framerate 1 and -f image2 seems to work fine.
>
> Without an explicit -f image2, an auto image demuxer is used which is 
> does not have a seek function defined.
>
> I'll see if I can rectify that.

The best solution would be if something like fileSystemWatcher could be 
added to FFmpeg.
Writing images from any software to a folder is easy. However writing 
images _and_ updating the symlink is complicated. Either the software 
which creates the images must call the script which updates the symlink, 
or there must be an additional tool running which has a 
fileSystemWatcher and updates the symlink when required.

Below is a C# example for fileSystemWatcher. It's important to note that 
the "fileSystemWatcher_Created" event does already come when the 
operating system begins to write the file. That may be too early to read 
the file, especially if the file is large or coming over a slow network. 
There must be a waiting loop until the file is completely written.

private static bool CanWriteFile(string fileName)
{
try
{
using (FileStream fs = new FileStream(fileName, FileMode.Open))
{
return fs.CanWrite;
}
}
catch
{
   return false;
}
}

private void fileSystemWatcher1_Created(object sender, 
System.IO.FileSystemEventArgs e)
{
for ( ; CanWriteFile(e.FullPath) == false ; )   // wait until the file 
is completely written
   {
System.Threading.Thread.Sleep(1);             // delay 1ms
}
   BinaryReader binReader = new BinaryReader(File.Open(e.FullPath, 
FileMode.Open));
// now the file is open for reading
}


Michael



More information about the ffmpeg-user mailing list