Skip to content

Commit 0874f7c

Browse files
committed
added fetching info from a video
1 parent faeb0a8 commit 0874f7c

20 files changed

+505
-56
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@
2323

2424
/source/ffmpeg-cpp/remuxing/obj
2525
/source/ffmpeg-cpp/difference/obj/x64/Debug
26+
/source/ffmpeg-cpp/difference/obj
27+
/source/ffmpeg-cpp/print_info/obj/x64

source/ffmpeg-cpp/ffmpeg-cpp.sln

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "filtering_video", "filterin
2323
EndProject
2424
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "difference", "difference\difference.vcxproj", "{9BD8FF1D-1C1B-4755-BF5C-AD58DC023711}"
2525
EndProject
26+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "print_info", "print_info\print_info.vcxproj", "{B4DB4D63-7AD7-42B9-9671-AD39D696B4FF}"
27+
EndProject
2628
Global
2729
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2830
Debug|x64 = Debug|x64
@@ -293,6 +295,34 @@ Global
293295
{9BD8FF1D-1C1B-4755-BF5C-AD58DC023711}.ReleaseLTO|x64.Build.0 = Release|x64
294296
{9BD8FF1D-1C1B-4755-BF5C-AD58DC023711}.ReleaseLTO|x86.ActiveCfg = Release|Win32
295297
{9BD8FF1D-1C1B-4755-BF5C-AD58DC023711}.ReleaseLTO|x86.Build.0 = Release|Win32
298+
{B4DB4D63-7AD7-42B9-9671-AD39D696B4FF}.Debug|x64.ActiveCfg = Debug|x64
299+
{B4DB4D63-7AD7-42B9-9671-AD39D696B4FF}.Debug|x64.Build.0 = Debug|x64
300+
{B4DB4D63-7AD7-42B9-9671-AD39D696B4FF}.Debug|x86.ActiveCfg = Debug|Win32
301+
{B4DB4D63-7AD7-42B9-9671-AD39D696B4FF}.Debug|x86.Build.0 = Debug|Win32
302+
{B4DB4D63-7AD7-42B9-9671-AD39D696B4FF}.DebugDLL|x64.ActiveCfg = Debug|x64
303+
{B4DB4D63-7AD7-42B9-9671-AD39D696B4FF}.DebugDLL|x64.Build.0 = Debug|x64
304+
{B4DB4D63-7AD7-42B9-9671-AD39D696B4FF}.DebugDLL|x86.ActiveCfg = Debug|Win32
305+
{B4DB4D63-7AD7-42B9-9671-AD39D696B4FF}.DebugDLL|x86.Build.0 = Debug|Win32
306+
{B4DB4D63-7AD7-42B9-9671-AD39D696B4FF}.DebugDLLStaticDeps|x64.ActiveCfg = Debug|x64
307+
{B4DB4D63-7AD7-42B9-9671-AD39D696B4FF}.DebugDLLStaticDeps|x64.Build.0 = Debug|x64
308+
{B4DB4D63-7AD7-42B9-9671-AD39D696B4FF}.DebugDLLStaticDeps|x86.ActiveCfg = Debug|Win32
309+
{B4DB4D63-7AD7-42B9-9671-AD39D696B4FF}.DebugDLLStaticDeps|x86.Build.0 = Debug|Win32
310+
{B4DB4D63-7AD7-42B9-9671-AD39D696B4FF}.Release|x64.ActiveCfg = Release|x64
311+
{B4DB4D63-7AD7-42B9-9671-AD39D696B4FF}.Release|x64.Build.0 = Release|x64
312+
{B4DB4D63-7AD7-42B9-9671-AD39D696B4FF}.Release|x86.ActiveCfg = Release|Win32
313+
{B4DB4D63-7AD7-42B9-9671-AD39D696B4FF}.Release|x86.Build.0 = Release|Win32
314+
{B4DB4D63-7AD7-42B9-9671-AD39D696B4FF}.ReleaseDLL|x64.ActiveCfg = Release|x64
315+
{B4DB4D63-7AD7-42B9-9671-AD39D696B4FF}.ReleaseDLL|x64.Build.0 = Release|x64
316+
{B4DB4D63-7AD7-42B9-9671-AD39D696B4FF}.ReleaseDLL|x86.ActiveCfg = Release|Win32
317+
{B4DB4D63-7AD7-42B9-9671-AD39D696B4FF}.ReleaseDLL|x86.Build.0 = Release|Win32
318+
{B4DB4D63-7AD7-42B9-9671-AD39D696B4FF}.ReleaseDLLStaticDeps|x64.ActiveCfg = Release|x64
319+
{B4DB4D63-7AD7-42B9-9671-AD39D696B4FF}.ReleaseDLLStaticDeps|x64.Build.0 = Release|x64
320+
{B4DB4D63-7AD7-42B9-9671-AD39D696B4FF}.ReleaseDLLStaticDeps|x86.ActiveCfg = Release|Win32
321+
{B4DB4D63-7AD7-42B9-9671-AD39D696B4FF}.ReleaseDLLStaticDeps|x86.Build.0 = Release|Win32
322+
{B4DB4D63-7AD7-42B9-9671-AD39D696B4FF}.ReleaseLTO|x64.ActiveCfg = Release|x64
323+
{B4DB4D63-7AD7-42B9-9671-AD39D696B4FF}.ReleaseLTO|x64.Build.0 = Release|x64
324+
{B4DB4D63-7AD7-42B9-9671-AD39D696B4FF}.ReleaseLTO|x86.ActiveCfg = Release|Win32
325+
{B4DB4D63-7AD7-42B9-9671-AD39D696B4FF}.ReleaseLTO|x86.Build.0 = Release|Win32
296326
EndGlobalSection
297327
GlobalSection(SolutionProperties) = preSolution
298328
HideSolutionNode = FALSE
@@ -305,6 +335,7 @@ Global
305335
{B337D322-355B-4348-A2A8-270471BE2C95} = {3B1FE419-D7D2-4406-9C24-5A6F6ED63E73}
306336
{80579A29-8073-46A0-B328-661155E0887B} = {3B1FE419-D7D2-4406-9C24-5A6F6ED63E73}
307337
{9BD8FF1D-1C1B-4755-BF5C-AD58DC023711} = {3B1FE419-D7D2-4406-9C24-5A6F6ED63E73}
338+
{B4DB4D63-7AD7-42B9-9671-AD39D696B4FF} = {3B1FE419-D7D2-4406-9C24-5A6F6ED63E73}
308339
EndGlobalSection
309340
GlobalSection(ExtensibilityGlobals) = postSolution
310341
SolutionGuid = {1838F09B-B929-4D1E-ABB6-FA2A94F4A4BE}

source/ffmpeg-cpp/ffmpeg-cpp/CodecDeducer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ namespace ffmpegcpp
3737

3838
AVCodec* CodecDeducer::DeduceDecoder(AVCodecID codecId)
3939
{
40+
if (codecId == AV_CODEC_ID_NONE) return nullptr;
4041
AVCodec* codec = avcodec_find_decoder(codecId);
4142
if (!codec)
4243
{

source/ffmpeg-cpp/ffmpeg-cpp/Demuxing/AudioInputStream.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
namespace ffmpegcpp
44
{
5-
AudioInputStream::AudioInputStream(AudioFrameSink* frameSink, AVFormatContext* format, AVStream* stream)
5+
AudioInputStream::AudioInputStream(AVFormatContext* format, AVStream* stream)
66
: InputStream(format, stream)
77
{
8-
SetFrameSink(frameSink);
98
}
109

1110
void AudioInputStream::ConfigureCodecContext()
@@ -21,6 +20,11 @@ namespace ffmpegcpp
2120
AudioInputStream::~AudioInputStream()
2221
{
2322
}
23+
24+
void AudioInputStream::AddStreamInfo(ContainerInfo* containerInfo)
25+
{
26+
// TODO
27+
}
2428
}
2529

2630

source/ffmpeg-cpp/ffmpeg-cpp/Demuxing/AudioInputStream.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ namespace ffmpegcpp
1111

1212
public:
1313

14-
AudioInputStream(AudioFrameSink* frameSink, AVFormatContext* format, AVStream* stream);
14+
AudioInputStream(AVFormatContext* format, AVStream* stream);
1515
~AudioInputStream();
1616

17+
void AddStreamInfo(ContainerInfo* info);
18+
1719
protected:
1820

1921
virtual void ConfigureCodecContext();

source/ffmpeg-cpp/ffmpeg-cpp/Demuxing/InputStream.cpp

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ namespace ffmpegcpp
4040
// does nothing by default
4141
}
4242

43-
void InputStream::Open()
43+
void InputStream::Open(FrameSink* frameSink)
4444
{
45+
output = frameSink->CreateStream();
46+
4547
const AVCodec* codec = codecContext->codec;
4648

4749
// Copy codec parameters from input stream to output codec context
@@ -93,11 +95,6 @@ namespace ffmpegcpp
9395
}
9496
}
9597

96-
void InputStream::SetFrameSink(FrameSink* frameSink)
97-
{
98-
output = frameSink->CreateStream();
99-
}
100-
10198
StreamData* InputStream::DiscoverMetaData()
10299
{
103100
/*metaData = new StreamData();
@@ -175,12 +172,28 @@ namespace ffmpegcpp
175172
return output->IsPrimed();
176173
}
177174

178-
StreamInfo InputStream::GetInfo()
175+
float InputStream::CalculateBitRate(AVCodecContext* ctx)
179176
{
180-
StreamInfo info;
181-
177+
int64_t bit_rate;
178+
int bits_per_sample;
182179

183-
return info;
180+
switch (ctx->codec_type)
181+
{
182+
case AVMEDIA_TYPE_VIDEO:
183+
case AVMEDIA_TYPE_DATA:
184+
case AVMEDIA_TYPE_SUBTITLE:
185+
case AVMEDIA_TYPE_ATTACHMENT:
186+
bit_rate = ctx->bit_rate;
187+
break;
188+
case AVMEDIA_TYPE_AUDIO:
189+
bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
190+
bit_rate = bits_per_sample ? ctx->sample_rate * (int64_t)ctx->channels * bits_per_sample : ctx->bit_rate;
191+
break;
192+
default:
193+
bit_rate = 0;
194+
break;
195+
}
196+
return bit_rate / 1000.0f;
184197
}
185198
}
186199

source/ffmpeg-cpp/ffmpeg-cpp/Demuxing/InputStream.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include "ffmpeg.h"
44
#include "std.h"
55
#include "Frame Sinks/FrameSink.h"
6-
#include "Info/StreamInfo.h"
6+
#include "Info/ContainerInfo.h"
77

88
namespace ffmpegcpp
99
{
@@ -15,29 +15,32 @@ namespace ffmpegcpp
1515
InputStream(AVFormatContext* format, AVStream* stream);
1616
~InputStream();
1717

18-
void Open();
18+
void Open(FrameSink* frameSink);
1919

2020
virtual void DecodePacket(AVPacket* pkt);
2121
void Close();
2222

2323
bool IsPrimed();
2424

25-
StreamInfo GetInfo();
25+
virtual void AddStreamInfo(ContainerInfo* info) = 0;
26+
2627

2728
protected:
2829

2930
AVCodecContext* codecContext = nullptr;
3031

31-
void SetFrameSink(FrameSink* frameSink);
3232

3333
virtual void ConfigureCodecContext();
3434

35+
AVFormatContext* format;
36+
AVStream* stream;
37+
38+
float CalculateBitRate(AVCodecContext* ctx);
39+
3540
private:
3641

3742
AVRational timeBaseCorrectedByTicksPerFrame;
3843

39-
AVFormatContext* format;
40-
AVStream* stream;
4144

4245
FrameSinkStream* output;
4346

@@ -48,6 +51,7 @@ namespace ffmpegcpp
4851
StreamData* DiscoverMetaData();
4952

5053
void CleanUp();
54+
5155
};
5256

5357

source/ffmpeg-cpp/ffmpeg-cpp/Demuxing/VideoInputStream.cpp

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
#include "VideoInputStream.h"
2+
#include "FFmpegException.h"
3+
#include "CodecDeducer.h"
24

35
namespace ffmpegcpp
46
{
5-
VideoInputStream::VideoInputStream(VideoFrameSink* frameSink, AVFormatContext* format, AVStream* stream)
7+
VideoInputStream::VideoInputStream(AVFormatContext* format, AVStream* stream)
68
: InputStream(format, stream)
79
{
8-
SetFrameSink(frameSink);
910
}
1011

1112
VideoInputStream::~VideoInputStream()
@@ -16,5 +17,52 @@ namespace ffmpegcpp
1617
{
1718

1819
}
20+
21+
void VideoInputStream::AddStreamInfo(ContainerInfo* containerInfo)
22+
{
23+
VideoStreamInfo info;
24+
25+
AVRational overrideFrameRate;
26+
overrideFrameRate.num = 0;
27+
28+
AVRational tb = overrideFrameRate.num ? av_inv_q(overrideFrameRate) : stream->time_base;
29+
AVRational fr = overrideFrameRate;
30+
if (!fr.num) fr = av_guess_frame_rate(format, stream, NULL);
31+
32+
StreamData* metaData = new StreamData();
33+
info.timeBase = tb;
34+
info.frameRate = fr;
35+
36+
AVCodecContext* codecContext = avcodec_alloc_context3(NULL);
37+
if (!codecContext) throw new FFmpegException("Failed to allocate temporary codec context.");
38+
int ret = avcodec_parameters_to_context(codecContext, stream->codecpar);
39+
if (ret < 0)
40+
{
41+
avcodec_free_context(&codecContext);
42+
throw new FFmpegException("Failed to read parameters from stream");
43+
}
44+
45+
codecContext->properties = stream->codec->properties;
46+
codecContext->codec = stream->codec->codec;
47+
codecContext->qmin = stream->codec->qmin;
48+
codecContext->qmax = stream->codec->qmax;
49+
codecContext->coded_width = stream->codec->coded_width;
50+
codecContext->coded_height = stream->codec->coded_height;
51+
52+
info.bitRate = CalculateBitRate(codecContext);
53+
54+
AVCodec* codec = CodecDeducer::DeduceDecoder(codecContext->codec_id);
55+
info.codec = codec;
56+
57+
info.format = codecContext->pix_fmt;
58+
info.formatName = av_get_pix_fmt_name(info.format);
59+
60+
info.width = codecContext->width;
61+
info.height = codecContext->height;
62+
63+
avcodec_free_context(&codecContext);
64+
65+
containerInfo->videoStreams.push_back(info);
66+
}
1967
}
2068

source/ffmpeg-cpp/ffmpeg-cpp/Demuxing/VideoInputStream.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "ffmpeg.h"
44
#include "InputStream.h"
55
#include "Frame Sinks/VideoFrameSink.h"
6+
#include "Info/VideoStreamInfo.h"
67

78
namespace ffmpegcpp
89
{
@@ -11,9 +12,11 @@ namespace ffmpegcpp
1112

1213
public:
1314

14-
VideoInputStream(VideoFrameSink* frameSink, AVFormatContext* format, AVStream* stream);
15+
VideoInputStream(AVFormatContext* format, AVStream* stream);
1516
~VideoInputStream();
1617

18+
void AddStreamInfo(ContainerInfo* info);
19+
1720
protected:
1821

1922
virtual void ConfigureCodecContext();

source/ffmpeg-cpp/ffmpeg-cpp/Info/ContainerInfo.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
#pragma once
22

33
#include "ffmpeg.h"
4-
#include "StreamInfo.h"
4+
#include "VideoStreamInfo.h"
5+
#include "std.h"
56

67
namespace ffmpegcpp
78
{
89
struct ContainerInfo
910
{
10-
float duration;
11+
long durationInMicroSeconds;
12+
float durationInSeconds;
1113
float start;
1214
float bitRate;
15+
const AVInputFormat* format;
1316

14-
int nStreams;
15-
StreamInfo* streams;
16-
17+
std::vector<VideoStreamInfo> videoStreams;
18+
// TODO: audio streams!
1719
};
1820

1921

0 commit comments

Comments
 (0)