/// @brief Does indexing of a source file /// @param Indexer A pointer to the indexer object representing the file to be indexed /// @param CacheName The filename of the output index file /// @param Trackmask A binary mask of the track numbers to index /// @param IgnoreDecodeErrors True if audio decoding errors will be tolerated, false otherwise /// @return Returns the index object on success, nullptr otherwise /// FFMS_Index *FFmpegSourceProvider::DoIndexing(FFMS_Indexer *Indexer, agi::fs::path const& CacheName, int Trackmask, FFMS_IndexErrorHandling IndexEH) { char FFMSErrMsg[1024]; FFMS_ErrorInfo ErrInfo; ErrInfo.Buffer = FFMSErrMsg; ErrInfo.BufferSize = sizeof(FFMSErrMsg); ErrInfo.ErrorType = FFMS_ERROR_SUCCESS; ErrInfo.SubType = FFMS_ERROR_SUCCESS; std::string MsgString; // set up progress dialog callback DialogProgress Progress(wxGetApp().frame, _("Indexing"), _("Reading timecodes and frame/sample data")); // index all audio tracks FFMS_Index *Index; Progress.Run([&](agi::ProgressSink *ps) { struct progress { agi::ProgressSink *ps; int calls; }; progress state = { ps, 0 }; Index = FFMS_DoIndexing(Indexer, Trackmask, FFMS_TRACKMASK_NONE, nullptr, nullptr, IndexEH, static_cast<TIndexCallback>([](int64_t Current, int64_t Total, void *Private) -> int { auto state = static_cast<progress *>(Private); if (++state->calls % 10 == 0) state->ps->SetProgress(Current, Total); return state->ps->IsCancelled(); }), &state, &ErrInfo); }); if (Index == nullptr) { MsgString += "Failed to index: "; MsgString += ErrInfo.Buffer; throw MsgString; } // write index to disk for later use FFMS_WriteIndex(CacheName.string().c_str(), Index, &ErrInfo); return Index; }
FFMS_API(FFMS_Index *) FFMS_MakeIndex(const char *SourceFile, int IndexMask, int DumpMask, TAudioNameCallback ANC, void *ANCPrivate, int ErrorHandling, TIndexCallback IC, void *ICPrivate, FFMS_ErrorInfo *ErrorInfo) { FFMS_Indexer *Indexer = FFMS_CreateIndexer(SourceFile, ErrorInfo); if (!Indexer) return NULL; return FFMS_DoIndexing(Indexer, IndexMask, DumpMask, ANC, ANCPrivate, ErrorHandling, IC, ICPrivate, ErrorInfo); }
static AVSValue __cdecl CreateFFIndex(AVSValue Args, void* UserData, IScriptEnvironment* Env) { FFMS_Init((int)AvisynthToFFCPUFlags(Env->GetCPUFlags()), Args[7].AsBool(false)); char ErrorMsg[1024]; FFMS_ErrorInfo E; E.Buffer = ErrorMsg; E.BufferSize = sizeof(ErrorMsg); if (!Args[0].Defined()) Env->ThrowError("FFIndex: No source specified"); const char *Source = Args[0].AsString(); const char *CacheFile = Args[1].AsString(""); int IndexMask = Args[2].AsInt(-1); int DumpMask = Args[3].AsInt(0); const char *AudioFile = Args[4].AsString("%sourcefile%.%trackzn%.w64"); int ErrorHandling = Args[5].AsInt(FFMS_IEH_IGNORE); bool OverWrite = Args[6].AsBool(false); const char *DemuxerStr = Args[8].AsString("default"); std::string DefaultCache(Source); DefaultCache.append(".ffindex"); if (!strcmp(CacheFile, "")) CacheFile = DefaultCache.c_str(); if (!strcmp(AudioFile, "")) Env->ThrowError("FFIndex: Specifying an empty audio filename is not allowed"); int Demuxer; if (!strcmp(DemuxerStr, "default")) Demuxer = FFMS_SOURCE_DEFAULT; else if (!strcmp(DemuxerStr, "lavf")) Demuxer = FFMS_SOURCE_LAVF; else if (!strcmp(DemuxerStr, "matroska")) Demuxer = FFMS_SOURCE_MATROSKA; else if (!strcmp(DemuxerStr, "haalimpeg")) Demuxer = FFMS_SOURCE_HAALIMPEG; else if (!strcmp(DemuxerStr, "haaliogg")) Demuxer = FFMS_SOURCE_HAALIOGG; else Env->ThrowError("FFIndex: Invalid demuxer requested"); FFMS_Index *Index = FFMS_ReadIndex(CacheFile, &E); if (OverWrite || !Index || (Index && FFMS_IndexBelongsToFile(Index, Source, 0) != FFMS_ERROR_SUCCESS)) { FFMS_Indexer *Indexer = FFMS_CreateIndexerWithDemuxer(Source, Demuxer, &E); if (!Indexer) Env->ThrowError("FFIndex: %s", E.Buffer); if (!(Index = FFMS_DoIndexing(Indexer, IndexMask, DumpMask, FFMS_DefaultAudioFilename, (void *)AudioFile, ErrorHandling, NULL, NULL, &E))) Env->ThrowError("FFIndex: %s", E.Buffer); if (FFMS_WriteIndex(CacheFile, Index, &E)) { FFMS_DestroyIndex(Index); Env->ThrowError("FFIndex: %s", E.Buffer); } FFMS_DestroyIndex(Index); if (!OverWrite) return AVSValue(1); else return AVSValue(2); } else { FFMS_DestroyIndex(Index); return AVSValue(0); } }
// Constructor and destructor c_media_file_ffms::c_media_file_ffms(boost::filesystem::path path) : // Path m_path(path), // File m_source(nullptr), // Info m_frames(0), m_rate(0), m_aspect(1), m_width(0), m_height(0) { // Debug //std::cout << boost::format("FFMS: Opening file! path = %1%") % path << std::endl; // FFMS error m_fferr.Buffer = m_ffmsg.data(); m_fferr.BufferSize = m_ffmsg.size(); m_fferr.ErrorType = FFMS_ERROR_SUCCESS; m_fferr.SubType = FFMS_ERROR_SUCCESS; // Library FFMS_Init(0, 1); // Index FFMS_Index* index = nullptr; // Cached index auto path_index = m_path; path_index.replace_extension(".ffindex"); if (boost::filesystem::is_regular_file(path_index)) { // Read index index = FFMS_ReadIndex(path_index.c_str(), &m_fferr); if (index) { // Check validity int result = FFMS_IndexBelongsToFile(index, m_path.c_str(), &m_fferr); if (result) { // Invalid index FFMS_DestroyIndex(index); index = nullptr; // Delete index file too boost::filesystem::remove(path_index); } } } // Create index if (!index) { // Indexer FFMS_Indexer* indexer = FFMS_CreateIndexer(m_path.c_str(), &m_fferr); if (!indexer) throw c_exception("FFMS: Could not create indexer!", { throw_format("path", m_path) }); //index = FFMS_DoIndexing2(indexer, FFMS_IEH_ABORT, &m_fferr); index = FFMS_DoIndexing(indexer, 0, 0, nullptr, nullptr, FFMS_IEH_ABORT, nullptr, nullptr, &m_fferr); if (!index) throw c_exception("FFMS: Failed to index media!", { throw_format("path", m_path) }); // Write index to file FFMS_WriteIndex(path_index.c_str(), index, &m_fferr); } // Track int track_id = FFMS_GetFirstTrackOfType(index, FFMS_TYPE_VIDEO, &m_fferr); if (track_id < 0) { FFMS_DestroyIndex(index); throw c_exception("FFMS: Failed to find any video tracks!", { throw_format("path", m_path) }); } // Source m_source = FFMS_CreateVideoSource(m_path.c_str(), track_id, index, 1, FFMS_SEEK_NORMAL, &m_fferr); if (!m_source) { FFMS_DestroyIndex(index); throw c_exception("FFMS: Failed to create video source!", { throw_format("path", m_path) }); } // Destroy index FFMS_DestroyIndex(index); index = nullptr; // Video properties const FFMS_VideoProperties* props = FFMS_GetVideoProperties(m_source); m_frames = props->NumFrames; if (props->FirstTime < props->LastTime && props->LastTime > 0.0) m_rate = (props->LastTime - props->FirstTime) / static_cast<double>(m_frames); else if (props->FPSNumerator != 0) m_rate = static_cast<double>(props->FPSNumerator) / static_cast<double>(props->FPSDenominator); if (props->SARNum != 0) m_aspect = static_cast<double>(props->SARNum) / static_cast<double>(props->SARDen); // First frame const FFMS_Frame* frame = FFMS_GetFrame(m_source, 0, &m_fferr); if (!frame) throw c_exception("FFMS: Failed to get first video frame!", { throw_format("path", m_path) }); if (frame->ScaledWidth > 0) m_width = frame->ScaledWidth; else m_width = frame->EncodedWidth; if (frame->ScaledHeight > 0) m_height = frame->ScaledHeight; else m_height = frame->EncodedHeight; // Conversion int pixfmts[2]; pixfmts[0] = FFMS_GetPixFmt("rgb24"); pixfmts[1] = -1; if (FFMS_SetOutputFormatV2(m_source, pixfmts, frame->EncodedWidth, frame->EncodedHeight, FFMS_RESIZER_POINT, &m_fferr)) throw c_exception("FFMS: Failed to set output format!", { throw_format("path", m_path) }); // Info std::cout << boost::format("FFMS: width = %d, height = %d, frames = %d, rate = %.3f, aspect = %.3f") % m_width % m_height % m_frames % m_rate % m_aspect << std::endl; }