|
| 1 | +// Example OTIO C++ code for reading and writing files supported by the OTIO |
| 2 | +// Python adapters. |
| 3 | +// |
| 4 | +// This example uses the "otioconvert" utility in a child process to convert |
| 5 | +// between input/output files and JSON that can be used from C++ code. |
| 6 | +// |
| 7 | +// To run this example make sure that the "otioconvert" utility is in your |
| 8 | +// search path and the environment variable PYTHONPATH is set correctly. |
| 9 | + |
| 10 | +#include "util.h" |
| 11 | + |
| 12 | +#include <opentimelineio/timeline.h> |
| 13 | + |
| 14 | +#include <iostream> |
| 15 | +#include <sstream> |
| 16 | + |
| 17 | +#if defined(_WINDOWS) |
| 18 | +#ifndef WIN32_LEAN_AND_MEAN |
| 19 | +#define WIN32_LEAN_AND_MEAN |
| 20 | +#endif // WIN32_LEAN_AND_MEAN |
| 21 | +#include <cctype> |
| 22 | +#include <codecvt> |
| 23 | +#include <locale> |
| 24 | +#include <windows.h> |
| 25 | +#include <combaseapi.h> |
| 26 | +#else // _WINDOWS |
| 27 | +#include <stdio.h> |
| 28 | +#endif // _WINDOWS |
| 29 | + |
| 30 | +namespace otio = opentimelineio::OPENTIMELINEIO_VERSION; |
| 31 | + |
| 32 | +class PythonAdapters |
| 33 | +{ |
| 34 | +public: |
| 35 | + static otio::SerializableObject::Retainer<otio::Timeline> read_from_file( |
| 36 | + std::string const&, |
| 37 | + otio::ErrorStatus*); |
| 38 | + |
| 39 | + static bool write_to_file( |
| 40 | + otio::SerializableObject::Retainer<otio::Timeline> const&, |
| 41 | + std::string const&, |
| 42 | + otio::ErrorStatus*); |
| 43 | + |
| 44 | +private: |
| 45 | + static bool _run_process(std::string const& cmd_line, otio::ErrorStatus*); |
| 46 | +}; |
| 47 | + |
| 48 | +otio::SerializableObject::Retainer<otio::Timeline> PythonAdapters::read_from_file( |
| 49 | + std::string const& file_name, |
| 50 | + otio::ErrorStatus* error_status) |
| 51 | +{ |
| 52 | + // Convert the input file to a temporary JSON file. |
| 53 | + const std::string temp_file_name = create_temp_dir() + "/temp.otio"; |
| 54 | + std::stringstream ss; |
| 55 | + ss << "otioconvert" << " -i " << normalize_path(file_name) << " -o " << temp_file_name; |
| 56 | + _run_process(ss.str(), error_status); |
| 57 | + |
| 58 | + // Read the temporary JSON file. |
| 59 | + return dynamic_cast<otio::Timeline*>(otio::Timeline::from_json_file(temp_file_name, error_status)); |
| 60 | +} |
| 61 | + |
| 62 | +bool PythonAdapters::write_to_file( |
| 63 | + otio::SerializableObject::Retainer<otio::Timeline> const& timeline, |
| 64 | + std::string const& file_name, |
| 65 | + otio::ErrorStatus* error_status) |
| 66 | +{ |
| 67 | + // Write the temporary JSON file. |
| 68 | + const std::string temp_file_name = create_temp_dir() + "/temp.otio"; |
| 69 | + if (!timeline.value->to_json_file(temp_file_name, error_status)) |
| 70 | + { |
| 71 | + return false; |
| 72 | + } |
| 73 | + |
| 74 | + // Convert the temporary JSON file to the output file. |
| 75 | + std::stringstream ss; |
| 76 | + ss << "otioconvert" << " -i " << temp_file_name << " -o " << normalize_path(file_name); |
| 77 | + _run_process(ss.str(), error_status); |
| 78 | + |
| 79 | + return true; |
| 80 | +} |
| 81 | + |
| 82 | +#if defined(_WINDOWS) |
| 83 | + |
| 84 | +class WCharBuffer |
| 85 | +{ |
| 86 | +public: |
| 87 | + WCharBuffer(const WCHAR* data, size_t size) |
| 88 | + { |
| 89 | + p = new WCHAR[(size + 1) * sizeof(WCHAR)]; |
| 90 | + memcpy(p, data, size * sizeof(WCHAR)); |
| 91 | + p[size] = 0; |
| 92 | + } |
| 93 | + |
| 94 | + ~WCharBuffer() |
| 95 | + { |
| 96 | + delete[] p; |
| 97 | + } |
| 98 | + |
| 99 | + WCHAR* p = nullptr; |
| 100 | +}; |
| 101 | + |
| 102 | +bool PythonAdapters::_run_process(const std::string& cmd_line, otio::ErrorStatus* error_status) |
| 103 | +{ |
| 104 | + // Convert the command-line to UTF16. |
| 105 | + std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> utf16; |
| 106 | + std::wstring w_cmd_line = utf16.from_bytes("/c " + cmd_line); |
| 107 | + WCharBuffer w_cmd_line_buf(w_cmd_line.c_str(), w_cmd_line.size()); |
| 108 | + |
| 109 | + // Create the process and wait for it to complete. |
| 110 | + STARTUPINFOW si; |
| 111 | + ZeroMemory(&si, sizeof(si)); |
| 112 | + PROCESS_INFORMATION pi; |
| 113 | + si.cb = sizeof(si); |
| 114 | + ZeroMemory(&pi, sizeof(pi)); |
| 115 | + if (0 == CreateProcessW( |
| 116 | + // TODO: MSDN documentation says to use "cmd.exe" for the "lpApplicationName" |
| 117 | + // argument, but that gives the error: "The system cannot find the file specified." |
| 118 | + // https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessw |
| 119 | + //L"cmd.exe", |
| 120 | + L"C:\\windows\\system32\\cmd.exe", |
| 121 | + w_cmd_line_buf.p, |
| 122 | + NULL, |
| 123 | + NULL, |
| 124 | + FALSE, |
| 125 | + 0, |
| 126 | + NULL, |
| 127 | + NULL, |
| 128 | + &si, |
| 129 | + &pi)) |
| 130 | + { |
| 131 | + const DWORD error = GetLastError(); |
| 132 | + TCHAR error_buf[4096]; |
| 133 | + FormatMessage( |
| 134 | + FORMAT_MESSAGE_FROM_SYSTEM | |
| 135 | + FORMAT_MESSAGE_IGNORE_INSERTS, |
| 136 | + NULL, |
| 137 | + error, |
| 138 | + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
| 139 | + error_buf, |
| 140 | + 4096, |
| 141 | + NULL); |
| 142 | + error_status->outcome = otio::ErrorStatus::Outcome::FILE_OPEN_FAILED; |
| 143 | + error_status->details = "cannot create process: " + std::string(error_buf, lstrlen(error_buf)); |
| 144 | + return false; |
| 145 | + } |
| 146 | + WaitForSingleObject(pi.hProcess, INFINITE); |
| 147 | + CloseHandle(pi.hProcess); |
| 148 | + CloseHandle(pi.hThread); |
| 149 | + |
| 150 | + return true; |
| 151 | +} |
| 152 | + |
| 153 | +#else // _WINDOWS |
| 154 | + |
| 155 | +bool PythonAdapters::_run_process(const std::string& cmd_line, otio::ErrorStatus* error_status) |
| 156 | +{ |
| 157 | + FILE* f = popen(cmd_line.c_str(), "r"); |
| 158 | + if (!f) |
| 159 | + { |
| 160 | + error_status->outcome = otio::ErrorStatus::Outcome::FILE_OPEN_FAILED; |
| 161 | + error_status->details = "cannot create process"; |
| 162 | + return false; |
| 163 | + } |
| 164 | + if (-1 == pclose(f)) |
| 165 | + { |
| 166 | + error_status->outcome = otio::ErrorStatus::Outcome::FILE_OPEN_FAILED; |
| 167 | + error_status->details = "cannot execute process"; |
| 168 | + return false; |
| 169 | + } |
| 170 | + return true; |
| 171 | +} |
| 172 | + |
| 173 | +#endif // _WINDOWS |
| 174 | + |
| 175 | +int main(int argc, char** argv) |
| 176 | +{ |
| 177 | + if (argc != 3) |
| 178 | + { |
| 179 | + std::cout << "Usage: python_adapters_child_process (inputpath) (outputpath)" << std::endl; |
| 180 | + return 1; |
| 181 | + } |
| 182 | + |
| 183 | + otio::ErrorStatus error_status; |
| 184 | + auto timeline = PythonAdapters::read_from_file(argv[1], &error_status); |
| 185 | + if (!timeline) |
| 186 | + { |
| 187 | + print_error(error_status); |
| 188 | + return 1; |
| 189 | + } |
| 190 | + |
| 191 | + std::cout << "Video tracks: " << timeline.value->video_tracks().size() << std::endl; |
| 192 | + std::cout << "Audio tracks: " << timeline.value->audio_tracks().size() << std::endl; |
| 193 | + |
| 194 | + if (!PythonAdapters::write_to_file(timeline, argv[2], &error_status)) |
| 195 | + { |
| 196 | + print_error(error_status); |
| 197 | + return 1; |
| 198 | + } |
| 199 | + |
| 200 | + return 0; |
| 201 | +} |
0 commit comments