Skip to content

Commit b6dabb7

Browse files
committed
Guess video bitrate if not provided by ffmpeg
The total bitrate may be given, but the video and/or audio not. If the video bitrate is not available, attempt to use the total bitrate
1 parent f78a71e commit b6dabb7

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

app/src/main/java/protect/videotranscoder/FFmpegUtil.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ public void onResult(String mediaDetailsStr)
208208
static MediaInfo parseMediaInfo(File mediaFile, String string)
209209
{
210210
long durationMs = 0;
211+
Integer totalBitrate = null;
211212
MediaContainer container = null;
212213
VideoCodec videoCodec = null;
213214
String videoResolution = null;
@@ -263,6 +264,24 @@ static MediaInfo parseMediaInfo(File mediaFile, String string)
263264
}
264265

265266
durationMs = time;
267+
268+
split = line.split(",");
269+
for(String item : split)
270+
{
271+
if(item.contains("bitrate:"))
272+
{
273+
item = item.replace("bitrate:", "").replace("kb/s", "").trim();
274+
try
275+
{
276+
// This may be used later if the video bitrate cannot be determined.
277+
totalBitrate = Integer.parseInt(item);
278+
}
279+
catch(NumberFormatException e)
280+
{
281+
continue;
282+
}
283+
}
284+
}
266285
}
267286

268287
if(line.startsWith("Input"))
@@ -388,6 +407,29 @@ static MediaInfo parseMediaInfo(File mediaFile, String string)
388407
}
389408
}
390409

410+
if(totalBitrate != null)
411+
{
412+
if(videoBitrate == null)
413+
{
414+
if(audioBitrate != null)
415+
{
416+
// We know the audio bitrate, we can calculate the video bitrate
417+
videoBitrate = totalBitrate - audioBitrate;
418+
}
419+
420+
if(videoBitrate == null)
421+
{
422+
// We do not know any of the separate bitrates. Lets guess 100 kb/s for the audio,
423+
// and subtract that from the total to guess the video bitrate.
424+
425+
// As a guess, subtract 100 kb/s from the bitrate for audio, and
426+
// assume that the video is the rest. This should be a decent-ish
427+
// estimate if the video bitrate cannot be found later.
428+
videoBitrate = totalBitrate - 100;
429+
}
430+
}
431+
}
432+
391433
MediaInfo info = new MediaInfo(mediaFile, durationMs, container, videoCodec, videoResolution,
392434
videoBitrate, videoFramerate, audioCodec, audioSampleRate, audioBitrate, audioChannels);
393435
return info;

app/src/test/java/protect/videotranscoder/FFmpegUtilTest.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,22 @@ public void parseMediaInfo() throws Exception
104104
assertEquals(MediaContainer.FLV, info.container);
105105
assertEquals(null, info.videoCodec);
106106
assertEquals("320x240", info.videoResolution);
107-
assertEquals(null, info.videoBitrate);
107+
assertEquals(Integer.valueOf(692), info.videoBitrate); // This is guessed from total bitrate
108108
assertEquals("25", info.videoFramerate);
109109
assertEquals(AudioCodec.AAC, info.audioCodec);
110110
assertEquals(Integer.valueOf(48000), info.audioSampleRate);
111111
assertEquals(null, info.audioBitrate);
112112
assertEquals(Integer.valueOf(2), info.audioChannels);
113+
114+
string = "Input #0, flv, from 'SampleVideo_360x240_1mb.flv':\n" +
115+
" Metadata:\n" + " encoder : Lavf53.24.2\n" +
116+
" Duration: 00:00:10.64, start: 0.000000, bitrate: 792 kb/s\n" +
117+
" Stream #0:0: Audio: aac (LC), 48000 Hz, 5.1, fltp, 92 kb/s (default)\n" +
118+
" Stream #0:1: Video: flv1, yuv420p, 320x240, 25 fps, 25 tbr, 1k tbn";
119+
120+
info = FFmpegUtil.parseMediaInfo(file, string);
121+
122+
assertEquals(Integer.valueOf(700), info.videoBitrate); // This is guessed from total and audio bitrate
113123
}
114124

115125
@Test

0 commit comments

Comments
 (0)