Skip to content

Instantly share code, notes, and snippets.

@informationsea
Last active May 1, 2021 08:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save informationsea/ebd1401a903669677fe7b12f71b17aa1 to your computer and use it in GitHub Desktop.
Save informationsea/ebd1401a903669677fe7b12f71b17aa1 to your computer and use it in GitHub Desktop.
ffmpeg convert helper for windows
#include <stdio.h>
#include <errno.h>
#include <windows.h>
#include <stdlib.h>
#include <string.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;
}
// check codec
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);
LPWSTR codec;
if (wcscmp(executable_file, L"ffmpeg-convert") == 0)
{
fprintf(stderr, "H264 mode\n");
codec = L"libx264";
}
else if (wcscmp(executable_file, L"ffmpeg-convert-h265") == 0)
{
fprintf(stderr, "H265 mode\n");
codec = L"libx265";
}
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;
}
// Load parameters
WCHAR parameters[1024*10 + 1];
memset(parameters, 0, sizeof(parameters));
WCHAR parameters_file_path[MAX_PATH + 1];
swprintf_s(parameters_file_path, MAX_PATH, L"%s%sparameters.txt", executable_drive, executable_dir_path);
wprintf_s(L"parameter file: %s / %s / %s\n", parameters_file_path, executable_drive, executable_dir_path);
FILE* parameters_file = NULL;
int open_error = _wfopen_s(&parameters_file, parameters_file_path, L"r");
if (open_error) {
perror("Failed to open parameter file.\n");
wait_key();
return 1;
}
char parameter_buffer[1024*10+1];
memset(parameter_buffer, 0, sizeof(parameter_buffer));
size_t parameter_buffer_read_size = fread(parameter_buffer, 1, sizeof(parameter_buffer) - 1, parameters_file);
size_t converted_parameters_size;
int convert_error = mbstowcs_s(&converted_parameters_size, parameters, sizeof(parameters)/sizeof(parameters[0]), parameter_buffer, parameter_buffer_read_size);
if (convert_error) {
fprintf(stderr, "Cannot convert parameter text to wide char\n");
wait_key();
return 1;
}
if (parameters_file) {
fclose(parameters_file);
}
WCHAR input_drive[MAX_PATH + 1];
WCHAR input_path_dir[MAX_PATH + 1];
WCHAR input_file[MAX_PATH + 1];
WCHAR input_ext[MAX_PATH + 1];
_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 %s -pix_fmt yuv420p %s \"%s%s%s.mp4\"",
executable_drive,
executable_dir_path,
argv[1],
codec,
parameters,
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: %ld / process id: %ld\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