-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflv_to_rush.cpp
118 lines (107 loc) · 3.68 KB
/
flv_to_rush.cpp
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
112
113
114
115
116
117
118
#include <cstdio>
#include <vector>
#include "flv/Flv.h"
#include "flv/FlvIo.h"
#include "lib/ByteUtils.h"
#include "lib/RushMuxer.h"
using namespace rush;
std::vector<std::shared_ptr<FlvTag>> process_flv(const std::string& filename) {
std::vector<std::shared_ptr<FlvTag>> tags;
FileReader reader(filename);
FlvHeader h(&reader);
auto prevTagSize = reader.read32bit();
while (true) {
auto tag = FlvTag::parse(&reader);
if (tag == nullptr) {
break;
}
tags.emplace_back(tag);
prevTagSize = reader.read32bit();
}
return tags;
}
// pts, dts are assumed to be in timescale 1000 aka ms
// 1. Figure out AudioSpecificConfig
// 2. Write connect payload
// 3. Write audio & video data (skipping AudioHeader, VideoHeader)
// Note: currently assuming that video/audio data is H264/AAC
void createRushFile(
IWriter* writer,
const std::vector<std::shared_ptr<FlvTag>>& tags,
const std::string& connect_payload) {
// flv has audioTimescale = 1000, videoTimescale = 1000
RushMuxer muxer(1000, 1000);
for (const auto& tag : tags) {
if (tag->getTagType() == FLVTAGTYPE::AUDIO) {
// Skipping logic to check for AAC, assuming AAC
auto audioTag = std::dynamic_pointer_cast<FlvTagAudioData>(tag);
if (audioTag->getPacketType() == 0) {
// AudioHeader, AudioSpecificConfig
muxer.setAACAudioSpecificConfig(
audioTag->getAudioData(), audioTag->getAudioDataSize());
}
} else if (tag->getTagType() == FLVTAGTYPE::VIDEO) {
// Skipping logic to check for H264, assuming H264
auto videoTag = std::dynamic_pointer_cast<FlvTagVideoData>(tag);
if (videoTag->getPacketType() == 0) {
// VideoHeader, AVCDecoderConfig
muxer.setAVCDecoderConfig(
videoTag->getVideoData(), videoTag->getVideoDataSize());
}
}
}
auto connect = muxer.createConnectPayload(connect_payload);
writer->write(connect.data(), connect.size());
for (const auto& tag : tags) {
if (tag->getTagType() == FLVTAGTYPE::AUDIO) {
auto audioTag = std::dynamic_pointer_cast<FlvTagAudioData>(tag);
if (audioTag->getPacketType() == 1) {
// AudioData
auto rushAudioData = muxer.createAudioPayload(
audioTag->getTimestamp(),
audioTag->getAudioData(),
audioTag->getAudioDataSize());
writer->write(rushAudioData.data(), rushAudioData.size());
}
} else if (tag->getTagType() == FLVTAGTYPE::VIDEO) {
auto videoTag = std::dynamic_pointer_cast<FlvTagVideoData>(tag);
if (videoTag->getPacketType() != 0) {
// VideoData
int64_t dts = videoTag->getTimestamp();
int64_t pts = dts + videoTag->getCompositionTimestamp();
auto rushVideoData = muxer.createVideoPayload(
pts,
dts,
videoTag->getVideoData(),
videoTag->getVideoDataSize());
writer->write(rushVideoData.data(), rushVideoData.size());
}
}
}
}
void print_usage() {
printf(
"flv_to_rush is an example program to convert from flv to fbvp/rush\n");
printf("./flv_to_rush <example.flv> <example.fbvp> <connect_payload>\n");
}
int main(int argc, char* argv[]) {
if (argc < 4) {
print_usage();
return 0;
}
std::string flv_filename(argv[1]);
std::string rush_filename(argv[2]);
std::string connect_payload(argv[3]);
auto tags = process_flv(flv_filename);
if (tags.size() == 0) {
printf("Failed to process flv: %s\n", flv_filename.c_str());
return 0;
}
FileWriter writer(rush_filename);
if (!writer.isValid()) {
printf("Failed to open/write to rush file: %s\n", rush_filename.c_str());
return 0;
}
createRushFile(&writer, tags, connect_payload);
return 0;
}