| 221 | } |
| 222 | |
| 223 | Status ReadInfoFile(const string& filename, uint32* width, uint32* height, |
| 224 | uint32* frames) { |
| 225 | string data; |
| 226 | TF_QCHECK_OK(ReadFileToString(Env::Default(), filename, &data)) |
| 227 | << "Could not read FFmpeg file: " << filename; |
| 228 | bool in_output = false; |
| 229 | bool in_mapping = false; |
| 230 | uint32 frames_value = 0; |
| 231 | uint32 height_value = 0; |
| 232 | uint32 width_value = 0; |
| 233 | for (const string& line : str_util::Split(data, '\n')) { |
| 234 | // Output starts with the first line of `Output #..`. |
| 235 | // Further processing output region starts next line so we could continue |
| 236 | // the loop. |
| 237 | if (!in_output && line.find("Output #") == 0) { |
| 238 | in_output = true; |
| 239 | in_mapping = false; |
| 240 | continue; |
| 241 | } |
| 242 | // Stream mapping starts with the first line of `Stream mapping`, it also |
| 243 | // signals the end of Output section. |
| 244 | // Further processing of stream mapping region starts next line so we could |
| 245 | // continue the loop. |
| 246 | if (!in_mapping && line.find("Stream mapping:") == 0) { |
| 247 | in_output = false; |
| 248 | in_mapping = true; |
| 249 | continue; |
| 250 | } |
| 251 | if (in_output) { |
| 252 | // We only look for the first stream in output `Stream #0`. |
| 253 | // Once processed we will not further process output section. |
| 254 | if (line.find(" Stream #") == 0) { |
| 255 | size_t p = line.find(", rgb24, ", 24); |
| 256 | if (p != std::string::npos) { |
| 257 | string rgb24 = line.substr(p + 9, line.find(" ", p + 9)); |
| 258 | rgb24 = rgb24.substr(0, rgb24.find(",")); |
| 259 | // Strip anything after " ", in case the format is |
| 260 | // `640x360 [SAR 1:1 DAR 16:9]` |
| 261 | rgb24 = rgb24.substr(0, rgb24.find(" ")); |
| 262 | string rgb24_width = rgb24.substr(0, rgb24.find("x")); |
| 263 | string rgb24_height = rgb24.substr(rgb24_width.length() + 1); |
| 264 | if (strings::safe_strtou32(rgb24_width, &width_value) && |
| 265 | strings::safe_strtou32(rgb24_height, &height_value)) { |
| 266 | in_output = false; |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | continue; |
| 271 | } |
| 272 | if (in_mapping) { |
| 273 | // We only look for the first stream mapping to have the number of the |
| 274 | // frames. |
| 275 | // Once processed we will not further process stream mapping section. |
| 276 | if (line.find("frame=") == 0) { |
| 277 | // The format might be `frame= 166 ` or `frame=12488 ` |
| 278 | string number = line.substr(6); |
| 279 | number = number.substr(number.find_first_not_of(" ")); |
| 280 | number = number.substr(0, number.find(" ")); |
no test coverage detected