连续播放视频文件

时间:2015-02-10 12:59:04

标签: c# c++ delphi lazarus

我正在寻找一种方法连续播放2个视频而没有任何延迟(或者 更换文件时非常小的延迟 我有1 mp4文件和ffmpeg我把它分成2个文件,现在我想要 将它们添加到播放列表中并播放它们但我不想要用户感觉 改变视频但是当我因为加载文件而测试时有一个 延迟。
我想在第一个文件结束之前将第二个文件串流到内存中。 我怎么能这样做?
我测试了LibVLC并阅读Play a Video from MemoryStream, Using FFMpeg但仍然没有运气 PS:任何语言的图书馆对我都有好处。

编辑:文件很小(小于5mb且小于5秒)。

编辑:我在FreePascal中添加了一个带有PasLibVLC的演示,它在第一个文件之后播放第二个文件,但是有一个延迟beetwen正在改变。如何最小化这个?(例如从内存中加载VLC并加载第二个文件到内存中)。

unit MainFormUnit;

{$mode Delphi}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Menus,LCLIntf, LCLType,
  ExtCtrls, StdCtrls,PasLibVlcUnit,PasLibVlcPlayerUnit;

const
  MAX_ARGS = 255;

type

  { TMainForm }

  TMainForm = class(TForm)
    Button1: TButton;
    MenuFile: TMenuItem;
    MenuFileOpen: TMenuItem;
    MenuFileQuit: TMenuItem;
    Panel1: TPanel;
    Panel2: TPanel;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    p_li: libvlc_instance_t_ptr;
    p_mi: libvlc_media_player_t_ptr;
    argv: packed array[0..MAX_ARGS - 1] of ansistring;
    args: packed array[0..MAX_ARGS - 1] of PAnsiChar;
    argc: integer;
    p_mi_ev_mgr: libvlc_event_manager_t_ptr;
    procedure AddArg(Value: ansistring);
    procedure PlayerInit();
    procedure PlayerPlay(fileName: WideString);
    procedure PlayerStop();
    procedure PlayerDestroy();
    procedure WmMediaPlayerEndReached(var m: TVlcMessage); message WM_MEDIA_PLAYER_END_REACHED;
  public
    { Public declarations }
  end;

var
  MainForm: TMainForm;

procedure lib_vlc_player_event_hdlr(p_event: libvlc_event_t_ptr; Data: Pointer); cdecl;

implementation

{$R *.lfm}
procedure lib_vlc_player_event_hdlr(p_event: libvlc_event_t_ptr; Data: Pointer); cdecl;
var
  Form: TMainForm;
begin
  if (Data = nil) then
    exit;
  Form := TMainForm(Data);
  if not Assigned(Form) then
    exit;
  case p_event^.event_type of
    libvlc_MediaPlayerEndReached:
      PostMessage(Form.Handle, WM_MEDIA_PLAYER_END_REACHED, WPARAM(0), LPARAM(0));
  end;
end;

{ TMainForm }

procedure TMainForm.AddArg(Value: ansistring);
begin
  if (argc < MAX_ARGS) then
  begin
    argv[argc] := Value;
    args[argc] := PAnsiChar(argv[argc]);
    Inc(argc);
  end;
end;

procedure TMainForm.PlayerInit();
begin
  libvlc_dynamic_dll_init();

  if (libvlc_dynamic_dll_error <> '') then
  begin
    MessageDlg(libvlc_dynamic_dll_error, mtError, [mbOK], 0);
    exit;
  end;

  argc := 0;
  AddArg(libvlc_dynamic_dll_path);
  AddArg('--intf=dummy');
  AddArg('--ignore-config');
  AddArg('--quiet');
  AddArg('--no-video-title-show');
  AddArg('--no-video-on-top');

  p_li := libvlc_new(argc, @args);
  p_mi := nil;
end;

procedure TMainForm.PlayerPlay(fileName: WideString);
var
  p_md: libvlc_media_t_ptr;
  a_st: ansistring;
  p_st: PAnsiChar;
begin
  PlayerStop();

  a_st := UTF8Encode(fileName);
  p_st := PAnsiChar(@a_st[1]);

  p_md := libvlc_media_new_path(p_li, p_st);

  if (p_md <> nil) then
  begin
    p_mi := libvlc_media_player_new_from_media(p_md);
    if (p_mi <> nil) then
    begin
      libvlc_video_set_key_input(p_mi, 1);
      libvlc_video_set_mouse_input(p_mi, 1);
      libvlc_media_player_set_display_window(p_mi, SELF.Handle);
      p_mi_ev_mgr := libvlc_media_player_event_manager(p_mi);
      libvlc_event_attach(p_mi_ev_mgr, libvlc_MediaPlayerEndReached, @lib_vlc_player_event_hdlr, SELF);
    end;
    libvlc_media_player_play(p_mi);
    libvlc_media_release(p_md);
  end;
end;

procedure TMainForm.PlayerStop();
begin
  if (p_mi <> nil) then
  begin
    libvlc_media_player_stop(p_mi);
    while (libvlc_media_player_get_state(p_mi) = libvlc_Playing) do
    begin
      Sleep(50);
    end;
    libvlc_media_player_release(p_mi);
    p_mi := nil;
  end;
end;

procedure TMainForm.PlayerDestroy();
begin
  if (p_li <> nil) then
  begin
    PlayerStop();
    libvlc_release(p_li);
    p_li := nil;
  end;
end;

procedure TMainForm.WmMediaPlayerEndReached(var m: TVlcMessage);
begin
 PlayerPlay('C:\ffmpeg\bin\tmp\OUTPUT1.mp4');
end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
  p_li := nil;
  p_mi := nil;
  PlayerInit();

  Button1.Click;
end;

procedure TMainForm.Button1Click(Sender: TObject);
begin
    PlayerPlay('C:\ffmpeg\bin\tmp\OUTPUT0.mp4');
end;

procedure TMainForm.FormDestroy(Sender: TObject);
begin
  PlayerDestroy();
end;

end.

1 个答案:

答案 0 :(得分:0)

HTML5媒体源扩展(MSE)应该允许你这样做,但它很棘手,因为你必须自己处理视频的流媒体和缓冲。如果你能找到一个内置功能的媒体播放器就会容易得多。请点击此处查看更多信息和MSE链接以回答类似问题: