带帧计数器的 Silverlight 媒体播放器 [英] Silverlight media player with frame counter

查看:20
本文介绍了带帧计数器的 Silverlight 媒体播放器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个简单的 Silverlight 媒体播放器,但我需要时间戳为 hh:mm:ss:ff,其中 FF 是帧数.

I am trying to write a simple Silverlight media player, but I need the timestamp to be hh:mm:ss:ff where FF is Frame count.

我使用了一个计时器来获取刻度并计算我所在的帧,但它似乎非常不准确.如何可靠地计算我所在的帧?

I used a timer in order to get ticks and calculate the frame I am in, but it seems very inaccurate. How can I count reliably the frame I am in?

有谁知道可以做到这一点的免费 Silverlight 播放器?

Does anyone know of a free Silverlight player that will do that?

推荐答案

Silverlight 旨在不定期更新,并在渲染下一帧时将任何动画或媒体播放到当前已用时间.

Silverlight is designed to update at irregular intervals and catch-up any animation or media playing to the current elapsed time when the next frame is rendered.

要计算当前帧(一帧只是一秒的特定分数),只需乘以 过去 总时间,自播放开始,通过视频中编码的每秒帧数,然后找到该秒内的剩余帧以获得当前帧.

To calculate the current frame (a frame is just a specific fraction of a second) it is simply a matter of multiplying total elapsed time, since the playback started, by the number of frames-per-second encoded in the video then finding the remainder of frames within that second to get the current frame.

例如当前帧 = (Elapsed-Time-in-seconds * FramesPerSecond) % FramesPerSecond;

因此,如果 20.12 秒过去了,那么在每秒 24 帧的视频中,您处于第 482 帧(实际上是 482.88,但只有整个帧很重要).

So if 20.12 seconds has elapsed, on a video that has 24 frames per second, you are on Frame 482 (actually 482.88 but only whole frames matter).

以每秒帧数取其模数,您将获得剩余的帧数(例如 2),因此您位于第二个 20(或 00:00:20:02)中的第 2 帧.

Take the Modulus of that by the Frames-per-second and you get the remaining number of frames (e.g. 2) so you are on frame number 2 in second number 20 (or 00:00:20:02).

您需要使用双精度数(或浮点数)和整数值的最终模数进行乘法运算,因此在 C# 代码中将如下所示:

You need to do the multiply using doubles (or floats) and the final modulus on an integer value so it will be like this in C# code:

int framesPerSecond = 24; // for example
double elapsedTimeInSeconds = ...; /// Get the elapsed time...
int currentFrame = ((int)(elapsedTimeInSeconds * (double)framesPerSecond)) % framesPerSecond;

由于问题已更改(在评论中)为分数帧速率,因此数学将按照此控制台应用程序进行:

As the question has changed (in comment) to a fractional frame rate the maths will be as per this console app:

using System;

namespace TimeTest
{
    class Program
    {
        static void Main(string[] args)
        {
            double framesPerSecond = 29.97;

            for (double elapsedTime = 0; elapsedTime < 5; elapsedTime+=0.01)
            {
                int currentFrame = (int)((elapsedTime*framesPerSecond) % framesPerSecond);
                Console.WriteLine("Time: {0} = Frame: {1}", elapsedTime, currentFrame);
            }
        }
    }
}

注意:您不能保证显示每个帧数,因为帧率并不完美,但您只能看到渲染的帧,所以没有关系.您看到的帧将具有正确的帧编号.

这篇关于带帧计数器的 Silverlight 媒体播放器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆