Topic: Fix for Kinovea/FFMpeg unable to open video files with UTF filenames.

Hello there Joan and folks, please allow me to explain how I found out about a bug in Kinovea and finally got it fixed (or at least from the tests I did it seems to be so).
When starting to use Kinovea a few months ago I noticed that some file's previews were not being displayed in the File Explorer panel, and a "file not found" error popped up when trying to load those files from the Video Files list.
At first I thought it could be that Kinovea wasn't supporting those files for some reason, but then observed that all the files that were not being displayed had some non-ASCII character in their file names.
So I changed some of those file's names to plain and simple ASCII character filenames, and then Kinovea started to show them in the File Explorer and was able to open them properly, so it became clear that those files were in fact supported, but not their filenames.

Trying to find a workaround to this issue, I stumbled upon a topic in this forum where it was previously discussed:

And where it also pointed to its ticket in the bug-tracker:  #231

From where I learned the root cause of the problem is that the FFMpeg library used by Kinovea doesn't get along well with file path names using characters not in the system's default character encoding codepage.
Knowing this, I had now some idea of where the issue was located, so I plunged into the code to try to get it fixed, since at first it seemed an easy task to accomplish (poor silly me, didn't know what I had coming ahead ...).
So, after several days where I almost gone nuts doing tests and researching about encodings, how to handle them with the available libraries, and how to get this FFMpeg library to work with non-"ANSI"/Windows codepage or UTF filenames, I finally came up with this fix:

In the file VideoReaderFFMpeg.cpp, replace the line:

char* pszFilePath = static_cast<char *>(Marshal::StringToHGlobalAnsi(_filePath).ToPointer());

with this code:

String^ encFilePath = System::Text::Encoding::Default->GetString(System::Text::Encoding::UTF8->GetBytes(_filePath));
char* pszFilePath = static_cast<char*>(Marshal::StringToHGlobalAnsi(encFilePath).ToPointer());

And that's it. It re-encodes the UTF file path string to the system's default encoding, so that the FFMpeg library can handle it, thus allowing Kinovea to show and open the file.

Of course, I cannot assure that this is the best way to fix it, or guarantee that it will work everywhere and in all possible cases, but I'm sure that in the system where I did the tests it worked perfectly.

Well, that's all. I just wanted to let you know about this, and if it can help in some way to improve even a little bit your wonderful software, I'll be immensely glad.


2 (edited by joan 2020-Mar-28 18:09:36)

Re: Fix for Kinovea/FFMpeg unable to open video files with UTF filenames.

This sounds awesome! Thank you for looking into this.

edit: Tested with various alphabets and it seems to work beautifully. Thanks!