Skip to content

Commit

Permalink
Fix FFmpeg framerate parsing for 'k' suffix. (fixes #362)
Browse files Browse the repository at this point in the history
Signed-off-by: Jeffrey Han <[email protected]>
  • Loading branch information
itdelatrisu committed Feb 12, 2018
1 parent cd1cdf8 commit 0b36328
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/itdelatrisu/opsu/video/FFmpeg.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,16 @@ public static VideoMetadata extractMetadata(File srcMovieFile) throws IOExceptio
// " Stream #0:0: Video: vp6f, yuv420p, 320x240, 314 kb/s, 30 tbr, 1k tbn, 1k tbc"
// ----------------------------------------------------------^
if (line.trim().startsWith("Stream #") && line.contains("Video:")) {
framerate = Float.parseFloat(RegexUtil.findFirst(line, Pattern.compile("\\s(\\d+(\\.\\d+)?)\\stbr,"), 1));
// Parse framerate
// Note: Can contain 'k' suffix (*1000), see dump.c#print_fps.
// https://www.ffmpeg.org/doxygen/3.1/dump_8c_source.html#l00119
String fps = RegexUtil.findFirst(line, Pattern.compile("\\s(\\d+(\\.\\d+)?k?)\\stbr,"), 1);
if (fps.endsWith("k"))
framerate = Float.parseFloat(fps.substring(0, fps.length() - 1)) * 1000f;
else
framerate = Float.parseFloat(fps);

// Parse width/height
int[] wh = TextValues.parseInts(RegexUtil.find(line, Pattern.compile("\\s(\\d+)x(\\d+)[\\s,]"), 1, 2));
width = wh[0];
height = wh[1];
Expand Down

2 comments on commit 0b36328

@tpenguinltg
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I should have mentioned that I was working on it.

My patch would have been something like this, which is one line shorter and doesn't duplicate the parseFloat:

diff --git a/src/itdelatrisu/opsu/video/FFmpeg.java b/src/itdelatrisu/opsu/video/FFmpeg.java
index 3b24ce1..a07575f 100644
--- a/src/itdelatrisu/opsu/video/FFmpeg.java
+++ b/src/itdelatrisu/opsu/video/FFmpeg.java
@@ -108,7 +108,11 @@ public class FFmpeg {
 				// "	Stream #0:0: Video: vp6f, yuv420p, 320x240, 314 kb/s, 30 tbr, 1k tbn, 1k tbc"
 				// ----------------------------------------------------------^
 				if (line.trim().startsWith("Stream #") && line.contains("Video:")) {
-					framerate = Float.parseFloat(RegexUtil.findFirst(line, Pattern.compile("\\s(\\d+(\\.\\d+)?)\\stbr,"), 1));
+					String[] fr = RegexUtil.find(line, Pattern.compile("\\s(\\d+(\\.\\d+)?)(k?)\\stbr,"), 1, 3);
+					framerate = Float.parseFloat(fr[0]);
+					if (fr[1] == "k")
+						framerate *= 1000f;
+
 					int[] wh = TextValues.parseInts(RegexUtil.find(line, Pattern.compile("\\s(\\d+)x(\\d+)[\\s,]"), 1, 2));
 					width = wh[0];
 					height = wh[1];

@itdelatrisu
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, will replace in a separate commit. 👍

Please sign in to comment.