ffmpeg convert helper for windows
#include <stdio.h> | |
#include <windows.h> | |
void wait_key(); | |
int main() | |
{ | |
printf("ffmpeg convert helper\n"); | |
LPWSTR *argv; | |
int argc; | |
argv = CommandLineToArgvW(GetCommandLineW(), &argc); | |
//wprintf_s(L"command line: %s\n", argv[0]); | |
if (argc < 2) | |
{ | |
fprintf(stderr, "No file\n"); | |
wait_key(); | |
return 1; | |
} | |
WCHAR executable_path[MAX_PATH]; | |
DWORD pathSize = GetModuleFileNameW(NULL, executable_path, MAX_PATH); | |
WCHAR executable_drive[MAX_PATH]; | |
WCHAR executable_dir_path[MAX_PATH]; | |
WCHAR executable_file[MAX_PATH]; | |
WCHAR executable_ext[MAX_PATH]; | |
_wsplitpath_s(executable_path, executable_drive, MAX_PATH, executable_dir_path, MAX_PATH, executable_file, MAX_PATH, executable_ext, MAX_PATH); | |
if (wcscmp(executable_file, L"ffmpeg-convert") == 0) | |
{ | |
fprintf(stderr, "H264 mode\n"); | |
} | |
else if (wcscmp(executable_file, L"ffmpeg-convert-h265") == 0) | |
{ | |
fprintf(stderr, "H265 mode\n"); | |
} | |
else | |
{ | |
fprintf(stderr, "executable file nanme should be \"ffmpeg-convert.exe\" or \"ffmpeg-convert-h265.exe\".\n"); | |
wprintf_s(L"actual: %s%s\n", executable_file, executable_ext); | |
wait_key(); | |
return 1; | |
} | |
WCHAR input_drive[MAX_PATH]; | |
WCHAR input_path_dir[MAX_PATH]; | |
WCHAR input_file[MAX_PATH]; | |
WCHAR input_ext[MAX_PATH]; | |
_wsplitpath_s(argv[1], input_drive, MAX_PATH, input_path_dir, MAX_PATH, input_file, MAX_PATH, input_ext, MAX_PATH); | |
if (wcscmp(input_ext, L".mp4") == 0) | |
{ | |
fprintf(stderr, "Input file extension should not be \".mp4\"\n"); | |
wait_key(); | |
return 1; | |
} | |
WCHAR ffmpeg_cmd[MAX_PATH]; | |
swprintf_s( | |
ffmpeg_cmd, | |
MAX_PATH, | |
L"\"%s%sffmpeg.exe\" -i \"%s\" -strict -2 -c:a copy -c:v libx264 -pix_fmt yuv420p \"%s%s%s.mp4\"", | |
executable_drive, | |
executable_dir_path, | |
argv[1], | |
input_drive, | |
input_path_dir, | |
input_file | |
); | |
wprintf_s(L"%s\n", executable_path); | |
wprintf_s(L"%s\n", ffmpeg_cmd); | |
PROCESS_INFORMATION ps = {}; | |
STARTUPINFOW si = { sizeof(STARTUPINFOW) }; | |
fprintf(stderr, "start\n"); | |
if (!CreateProcessW(NULL, ffmpeg_cmd, NULL, NULL, FALSE, 0, NULL, NULL, &si, &ps)) { | |
fprintf(stderr, "failed to run ffmpeg"); | |
wait_key(); | |
return 1; | |
} | |
fprintf(stderr, "thread id: %d / process id: %d\n", ps.dwThreadId, ps.dwProcessId); | |
if (!CloseHandle(ps.hThread)) { | |
fprintf(stderr, "CloseHandle(hThread)"); | |
wait_key(); | |
return -1; | |
} | |
WaitForSingleObject(ps.hProcess, INFINITE); | |
if (!CloseHandle(ps.hProcess)) { | |
fprintf(stderr, "CloseHandle(hProcess)"); | |
wait_key(); | |
return -1; | |
} | |
wait_key(); | |
} | |
void wait_key() | |
{ | |
WCHAR line[10]; | |
fprintf(stderr, "Press enter key to quit\n"); | |
fflush(stdout); | |
fflush(stderr); | |
fgetws(line, 10, stdin); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment