Recent

Author Topic: Passing unicode filenames to external program  (Read 21828 times)

LeeJiEun

  • New Member
  • *
  • Posts: 17
Re: Passing unicode filenames to external program
« Reply #15 on: October 25, 2013, 06:33:48 am »
So, what I've found is that it's kind of hit or miss with unicode, some stuff works, some doesn't.

I've come up with a workaround.
The file names I get with the OpenDialog appear to be correct, so I use those with CopyFile to create temporary files with names  temp001.mp4 and temp002.mp4, for example:

CopyFile(VideoFilename, 'C:\ffmpeg\media\temp001.mp4', TRUE);

CopyFile() didn't seem to have any problem with the Korean characters.

I can then use these temp files in the call to ffmpeg since they have no unusual characters to get messed up, and for the name of the file ffmpeg will output I used temp003.mp4

Now the trick is to get the temp003 file back to the name with the Korean characters. I tried using RenameFile(), but once again the Korean characters got screwed up no matter what kind of encoding I tried. So instead I used CopyFile() again and this worked.
Afterwards I just delete the 3 temp files. It's a kluge, but I don't see any other way.

Conclusion - Support for Korean characters is a mixed bag, CopyFile() works but RenameFile() and passing as parameters to external programs don't work.

Deepaak

  • Sr. Member
  • ****
  • Posts: 454
Re: Passing unicode filenames to external program
« Reply #16 on: October 25, 2013, 10:10:40 am »
If your program is native for windows platform, then you can use win32 way to make your program work. The problem you are facing is unicode and ansi conversion problem, can you give a sample program of your with a koren file. I will try to help you, I cannot garuntee to solve your problem, but i will try to solve. But if your program is cross-platform then sorry.
Holiday season is online now. :-)

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1946
Re: Passing unicode filenames to external program
« Reply #17 on: October 25, 2013, 10:46:13 am »
Try
RenameFileUTF8('')   
(FileUtil)

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12901
  • FPC developer.
Re: Passing unicode filenames to external program
« Reply #18 on: October 25, 2013, 01:18:57 pm »
Should work fine with FPC trunk too. (if the argument is an unicodestring)

avra

  • Hero Member
  • *****
  • Posts: 2591
    • Additional info
Re: Passing unicode filenames to external program
« Reply #19 on: October 25, 2013, 03:27:23 pm »
@avra, I think Bart is completely correct:
TOpenDialog.FileName is in UTF8 encoding.
Simply assigning it to a widestring probably won't do.
You need to do
Code: [Select]
uses
  LazUtf8, ...
var
  VideoFileName: WideString;
...
begin
  ...
  VideoFileName := Utf8ToUtf16(OpenDialog1.FileName);
  ...

I was thinking more of something like skipping conversion functions and dialogs at all and just test raw functionality. This is what I have tested and it works:
Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
var
  SEInfo: SHELLEXECUTEINFOW;
begin
  FillChar(SEInfo, SizeOf(SEInfo), 0);
  SEInfo.cbSize := SizeOf(TShellExecuteInfo);
  with SEInfo do
  begin
    fMask := SEE_MASK_NOCLOSEPROCESS;
    Wnd := Handle;
    lpFile := PWideChar(Utf8ToUtf16('D:\Ћирилична фасцикла\autoruns.exe'));
    //lpParameters := PWideChar(params);
    nShow := 1;
  end;
  if not ShellExecuteExW(@SEInfo) then
    ShowMessage('Error starting ffmpeg!');
end;
With that I was able to execute 'D:\Ћирилична фасцикла\autoruns.exe' without problems on WinXP.

I would try to just execute FFMPEG with this method from Korean path without any parameters. If that is a success then Korean paths are not the problem, and problem is either string conversion or FFMPEG not being able to accept parameters with Korean path file.
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

LeeJiEun

  • New Member
  • *
  • Posts: 17
Re: Passing unicode filenames to external program
« Reply #20 on: October 27, 2013, 03:48:13 am »
RenameFileUTF8('') works, so that saves copying and then deleting a file.

Funny that there's RenameFile() and RenameFileUTF8(), but just the one CopyFile() which appears to do UTF8.

I had tried embedding the file name directly in the code and that didn't help. The file names I'm getting from the open dialog are fine, as I can use them for copying and renaming files.

I haven't tried it with Cyrillic characters, but they're not encoded quite the same way as East Asian characters, so they might not suffer from the same problem.


Bart

  • Hero Member
  • *****
  • Posts: 5727
    • Bart en Mariska's Webstek
Re: Passing unicode filenames to external program
« Reply #21 on: October 27, 2013, 01:32:48 pm »
Funny that there's RenameFile() and RenameFileUTF8(), but just the one CopyFile() which appears to do UTF8.

These functions (in LazFileUtils) only get the "UTF8" suffix (or prefix) if there is an ANSI equivalent in RTL.

Bart

mdalacu

  • Full Member
  • ***
  • Posts: 245
    • dmSimpleApps
Re: Passing unicode filenames to external program
« Reply #22 on: May 23, 2014, 03:11:52 pm »
Have you found any workaround in calling ffmpeg with special chars into filename? I am pulling my hair out here ... :'(
Thx.

LeeJiEun

  • New Member
  • *
  • Posts: 17
Re: Passing unicode filenames to external program
« Reply #23 on: June 30, 2014, 12:29:51 am »
Have you found any workaround in calling ffmpeg with special chars into filename? I am pulling my hair out here ... :'(
Thx.

Hi mdakacu, sorry for the late reply.

I never found a way to make it work the way it should, so I ended up using the temporary file method using CopyFile() and RenameFileUTF8() as mentioned earlier in this thread. If you need any more detail, let me know exactly what you're trying to do and I'll help as best I can.


Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1272
Re: Passing unicode filenames to external program
« Reply #24 on: June 30, 2014, 07:29:30 am »
??  I spent a large part of the weekend pulling my hair on a similar issue (mplayer not ffpmeg, otherwise issue is identical).   For now, we can't use rely on any using TProcess - results are intermittent on windows.  Apparently the fpc guys are aware of the issue and working towards a solution, hopefully in 2.8 - nothing yet in trunk, and no quick fix though....

In the meantime I like the idea of copying the file to a temporary folder, and using that.   In fact kicking myself for a) not thinking of it, and b) not finding this thread...

Worth mentioning that in Linux tests, TProcessUTF8 worked just fine...
Lazarus Trunk/FPC latest fixes on Windows 11
  I'm getting old and stale.  Slowly getting used to git, I'll get there...

rasberryrabbit

  • Full Member
  • ***
  • Posts: 151
Re: Passing unicode filenames to external program
« Reply #25 on: June 30, 2014, 07:31:28 am »
In fpc 2.7.1,
If you don't loose ansi encoding it should be assign with pchar(mystring).
Sometimes mistranslate encoding by mixing utf-8 and ansi.

Change default encoding to utf-8 also works.
Code is long, Life is short, AI is not your enemy.

Gizmo

  • Hero Member
  • *****
  • Posts: 831
Re: Passing unicode filenames to external program
« Reply #26 on: June 30, 2014, 03:38:09 pm »
Note this thread (http://forum.lazarus.freepascal.org/index.php/topic,24765.msg149502.html#msg149502) which, on the whole, makes reference to creating handles to files using CreateFileW, instead of just CreateFile. There's lot of interesting and useful info and additional links regarding the use of 16-byte LE Unicode on Windows, rather than the more standard UTF8 adopted by Linux etc

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Passing unicode filenames to external program
« Reply #27 on: June 30, 2014, 04:08:38 pm »
Change default encoding to utf-8 also works.
You mean set the console codepage with something like chcp 65001 or something?

@Gizmo: interesting, thanks!
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

 

TinyPortal © 2005-2018