-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhlstranscode.cpp
More file actions
112 lines (96 loc) · 2.87 KB
/
hlstranscode.cpp
File metadata and controls
112 lines (96 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "hlstranscode.h"
#include <QFile>
#include <QDebug>
HlsTranscode::HlsTranscode(QString subs, QString ffmpp, QString streamPath, QObject *parent)
: QObject(parent), ffmpegPath(ffmpp), streamPath(streamPath), subs(subs), readyForData(false)
{
// start ffmpeg
connect(&ffmpeg, SIGNAL(started()), this, SLOT(ffmpegStarted()));
connect(&ffmpeg, SIGNAL(readyReadStandardOutput()), this, SLOT(segmentComplete()));
connect(&ffmpeg, SIGNAL(readyReadStandardError()), this, SLOT(transcodeError()));
connect(&ffmpeg, SIGNAL(bytesWritten(qint64)), this, SLOT(bytesWritten(qint64)));
connect(&ffmpeg, SIGNAL(finished(int)), this, SLOT(ffmpegTerminated(int)));
startFfmpeg();
}
void HlsTranscode::startFfmpeg()
{
QStringList args;
args << "-i" << "pipe:0";
// add subtitles
if (subs.length() > 0)
args << "-vf" << "subtitles="+subs+":charenc=Windows-1250";
args << "-hide_banner";
args << "-nostdin";
args << "-nostats";
args << "-loglevel" << "warning";
args << "-c:v" << "libx264";
args << "-profile:v" << "baseline" << "-level" << "3.1";
args << "-pix_fmt" << "yuv420p";
args << "-preset" << "ultrafast";
args << "-crf" << "22";
args << "-c:a" << "aac" << "-strict" << "-2";
args << "-force_key_frames" << "expr:gte(t,n_forced*5)";
args << "-f" << "ssegment";
args << "-segment_format" << "mpegts";
args << "-segment_list" << "pipe:1";
args << "-segment_list_type" << "csv";
args << "-segment_time" << "10";
args << streamPath + "/a%03d.ts";
qDebug() << "start ffmpeg";
qDebug() << ffmpegPath << args;
// start
pendingBytes = 0;
completedSegments = 0;
ffmpeg.start(ffmpegPath, args);
}
void HlsTranscode::transcodeError()
{
QByteArray err = ffmpeg.readAllStandardError();
if (err.length() > 0)
qDebug() << err;
}
void HlsTranscode::segmentComplete()
{
// parse output
while (ffmpeg.canReadLine())
{
QString ln = ffmpeg.readLine().trimmed();
QStringList s = ln.split(",");
if (s.length() != 3)
continue;
completedSegments++;
emit segmentAvailable(completedSegments - 1);
}
}
void HlsTranscode::ffmpegStarted()
{
readyForData = true;
}
void HlsTranscode::stop()
{
readyForData = false;
if (ffmpeg.isOpen())
ffmpeg.close();
}
void HlsTranscode::pipeInput(QByteArray *in)
{
if (!ffmpeg.isOpen()) return;
readyForData = false;
pendingBytes += in->size();
ffmpeg.write(*in);
}
void HlsTranscode::bytesWritten(qint64 sz)
{
pendingBytes -= sz;
readyForData = (pendingBytes == 0);
}
void HlsTranscode::ffmpegTerminated(int code)
{
readyForData = false;
qDebug() << ffmpeg.readAllStandardError() << ffmpeg.readAllStandardOutput();
qDebug() << "ffmpeg terminated:" + QString::number(code);
}
HlsTranscode::~HlsTranscode()
{
stop();
}