MythTV  master
mythtv/programs/mythccextractor/main.cpp
Go to the documentation of this file.
1 // -*- Mode: c++ -*-
2 
3 // C++ headers
4 #include <iostream>
5 
6 // Qt headers
7 #include <QCoreApplication>
8 #include <QString>
9 #include <QtCore>
10 #include <QtGui>
11 
12 // MythTV headers
13 #include "mythccextractorplayer.h"
14 #include "commandlineparser.h"
15 #include "mythcontext.h"
16 #include "mythversion.h"
17 #include "programinfo.h"
18 #include "io/mythmediabuffer.h"
19 #include "exitcodes.h"
20 #include "signalhandling.h"
21 #include "loggingserver.h"
22 #include "cleanupguard.h"
23 
24 namespace {
25  void cleanup()
26  {
27  delete gContext;
28  gContext = nullptr;
30  }
31 }
32 
33 static int RunCCExtract(ProgramInfo &program_info, const QString & destdir)
34 {
35  QString filename = program_info.GetPlaybackURL();
36  if (filename.startsWith("myth://"))
37  {
38  QString msg =
39  QString("Only locally accessible files are supported (%1).")
40  .arg(program_info.GetPathname());
41  std::cerr << qPrintable(msg) << std::endl;
43  }
44 
45  if (!QFile::exists(filename))
46  {
47  std::cerr << qPrintable(
48  QString("Could not open input file (%1).").arg(filename)) << std::endl;
50  }
51 
53  if (!tmprbuf)
54  {
55  std::cerr << qPrintable(QString("Unable to create RingBuffer for %1")
56  .arg(filename)) << std::endl;
58  }
59 
60  if (program_info.GetRecordingEndTime() > MythDate::current())
61  {
62  std::cout << "Program will end @ "
63  << qPrintable(program_info.GetRecordingEndTime(MythDate::ISODate))
64  << std::endl;
65  tmprbuf->SetWaitForWrite();
66  }
67 
68  auto flags = (PlayerFlags)(kVideoIsNull | kAudioMuted |
72  auto *ctx = new PlayerContext(kCCExtractorInUseID);
73  auto *ccp = new MythCCExtractorPlayer(ctx, flags, true, filename, destdir);
74  ctx->SetPlayingInfo(&program_info);
75  ctx->SetRingBuffer(tmprbuf);
76  ctx->SetPlayer(ccp);
77  if (ccp->OpenFile() < 0)
78  {
79  std::cerr << "Failed to open " << qPrintable(filename) << std::endl;
80  return GENERIC_EXIT_NOT_OK;
81  }
82  if (!ccp->run())
83  {
84  std::cerr << "Failed to decode " << qPrintable(filename) << std::endl;
85  return GENERIC_EXIT_NOT_OK;
86  }
87 
88  delete ctx;
89 
90  return GENERIC_EXIT_OK;
91 }
92 
93 int main(int argc, char *argv[])
94 {
95  QCoreApplication a(argc, argv);
96 
97  QCoreApplication::setApplicationName(MYTH_APPNAME_MYTHCCEXTRACTOR);
98 
100  if (!cmdline.Parse(argc, argv))
101  {
102  cmdline.PrintHelp();
104  }
105 
106  int retval = cmdline.ConfigureLogging("none");
107  if (retval != GENERIC_EXIT_OK)
108  return retval;
109 
110  if (cmdline.toBool("showhelp"))
111  {
112  cmdline.PrintHelp();
113  return GENERIC_EXIT_OK;
114  }
115 
116  if (cmdline.toBool("showversion"))
117  {
119  return GENERIC_EXIT_OK;
120  }
121 
122  QString infile = cmdline.toString("inputfile");
123  if (infile.isEmpty())
124  {
125  std::cerr << "The input file --infile is required" << std::endl;
127  }
128 
129  QString destdir = cmdline.toString("destdir");
130 
131  bool useDB = !QFile::exists(infile);
132 
133  CleanupGuard callCleanup(cleanup);
134 
135 #ifndef _WIN32
136  QList<int> signallist;
137  signallist << SIGINT << SIGTERM << SIGSEGV << SIGABRT << SIGBUS << SIGFPE
138  << SIGILL;
139 #if ! CONFIG_DARWIN
140  signallist << SIGRTMIN;
141 #endif
142  SignalHandler::Init(signallist);
143  SignalHandler::SetHandler(SIGHUP, logSigHup);
144 #endif
145 
147  if (!gContext->Init(
148  false/*use gui*/, false/*prompt for backend*/,
149  false/*bypass auto discovery*/, !useDB/*ignoreDB*/))
150  {
151  std::cerr << "Failed to init MythContext, exiting." << std::endl;
153  }
154 
155  ProgramInfo pginfo(infile);
156  return RunCCExtract(pginfo, destdir);
157 }
158 
159 
160 /* vim: set expandtab tabstop=4 shiftwidth=4: */
GENERIC_EXIT_OK
#define GENERIC_EXIT_OK
Exited with no error.
Definition: exitcodes.h:10
main
int main(int argc, char *argv[])
Definition: mythtv/programs/mythccextractor/main.cpp:93
MythContext
Startup context for MythTV.
Definition: mythcontext.h:43
MythMediaBuffer
Definition: mythmediabuffer.h:50
PlayerFlags
PlayerFlags
Definition: mythplayer.h:65
RunCCExtract
static int RunCCExtract(ProgramInfo &program_info, const QString &destdir)
Definition: mythtv/programs/mythccextractor/main.cpp:33
kDecodeLowRes
@ kDecodeLowRes
Definition: mythplayer.h:68
GENERIC_EXIT_INVALID_CMDLINE
#define GENERIC_EXIT_INVALID_CMDLINE
Command line parse error.
Definition: exitcodes.h:15
ProgramInfo::GetRecordingEndTime
QDateTime GetRecordingEndTime(void) const
Approximate time the recording should have ended, did end, or is intended to end.
Definition: programinfo.h:412
MythCCExtractorPlayer
Definition: mythccextractorplayer.h:112
MythDate::current
QDateTime current(bool stripped)
Returns current Date and Time in UTC.
Definition: mythdate.cpp:10
ProgramInfo::GetPathname
QString GetPathname(void) const
Definition: programinfo.h:343
mythversion.h
MythCommandLineParser::Parse
virtual bool Parse(int argc, const char *const *argv)
Loop through argv and populate arguments with values.
Definition: mythcommandlineparser.cpp:1449
programinfo.h
signalhandling.h
CleanupGuard
Definition: cleanupguard.h:6
MYTH_APPNAME_MYTHCCEXTRACTOR
#define MYTH_APPNAME_MYTHCCEXTRACTOR
Definition: mythcorecontext.h:25
SIGHUP
#define SIGHUP
Definition: compat.h:216
MythCommandLineParser::PrintVersion
static void PrintVersion(void)
Print application version information.
Definition: mythcommandlineparser.cpp:1277
commandlineparser.h
MythCommandLineParser::PrintHelp
void PrintHelp(void) const
Print command line option help.
Definition: mythcommandlineparser.cpp:1293
kDecodeNoDecode
@ kDecodeNoDecode
Definition: mythplayer.h:72
GENERIC_EXIT_PERMISSIONS_ERROR
#define GENERIC_EXIT_PERMISSIONS_ERROR
File permissions error.
Definition: exitcodes.h:19
mythccextractorplayer.h
cleanup
static QString cleanup(const QString &str)
Definition: remoteencoder.cpp:672
mythmediabuffer.h
kDecodeSingleThreaded
@ kDecodeSingleThreaded
Definition: mythplayer.h:69
MYTH_BINARY_VERSION
#define MYTH_BINARY_VERSION
Update this whenever the plug-in ABI changes.
Definition: mythversion.h:15
kVideoIsNull
@ kVideoIsNull
Definition: mythplayer.h:74
cmdline
MythCommFlagCommandLineParser cmdline
Definition: mythtv/programs/mythcommflag/main.cpp:74
ProgramInfo
Holds information on recordings and videos.
Definition: programinfo.h:67
MythCommandLineParser::toString
QString toString(const QString &key) const
Returns stored QVariant as a QString, falling to default if not provided.
Definition: mythcommandlineparser.cpp:2252
GENERIC_EXIT_NOT_OK
#define GENERIC_EXIT_NOT_OK
Exited with error.
Definition: exitcodes.h:11
MythCommandLineParser::toBool
bool toBool(const QString &key) const
Returns stored QVariant as a boolean.
Definition: mythcommandlineparser.cpp:2095
cleanupguard.h
ProgramInfo::GetPlaybackURL
QString GetPlaybackURL(bool checkMaster=false, bool forceCheckLocal=false)
Returns filename or URL to be used to play back this recording.
Definition: programinfo.cpp:2544
MythDate::ISODate
@ ISODate
Default UTC.
Definition: mythdate.h:17
mythcontext.h
kDecodeNoLoopFilter
@ kDecodeNoLoopFilter
Definition: mythplayer.h:71
GENERIC_EXIT_NO_MYTHCONTEXT
#define GENERIC_EXIT_NO_MYTHCONTEXT
No MythContext available.
Definition: exitcodes.h:13
SignalHandler::SetHandler
static void SetHandler(int signum, SigHandlerFunc handler)
Definition: signalhandling.cpp:138
kAudioMuted
@ kAudioMuted
Definition: mythplayer.h:75
PlayerContext
Definition: playercontext.h:52
MythMediaBuffer::Create
static MythMediaBuffer * Create(const QString &Filename, bool Write, bool UseReadAhead=true, std::chrono::milliseconds Timeout=kDefaultOpenTimeout, bool StreamOnly=false)
Creates a RingBuffer instance.
Definition: mythmediabuffer.cpp:97
MythCommandLineParser::ConfigureLogging
int ConfigureLogging(const QString &mask="general", bool progress=false)
Read in logging options and initialize the logging interface.
Definition: mythcommandlineparser.cpp:2756
exitcodes.h
build_compdb.filename
filename
Definition: build_compdb.py:21
loggingserver.h
MythCCExtractorCommandLineParser
Definition: mythccextractor/commandlineparser.h:8
gContext
MythContext * gContext
This global variable contains the MythContext instance for the application.
Definition: mythcontext.cpp:64
MythContext::Init
bool Init(bool gui=true, bool promptForBackend=false, bool disableAutoDiscovery=false, bool ignoreDB=false)
Definition: mythcontext.cpp:1571
SignalHandler::Done
static void Done(void)
Definition: signalhandling.cpp:131
kDecodeFewBlocks
@ kDecodeFewBlocks
Definition: mythplayer.h:70
kCCExtractorInUseID
const QString kCCExtractorInUseID
Definition: programtypes.cpp:24
SignalHandler::Init
static void Init(QList< int > &signallist, QObject *parent=nullptr)
Definition: signalhandling.cpp:124
MythMediaBuffer::SetWaitForWrite
void SetWaitForWrite(void)
Definition: mythmediabuffer.cpp:318