Call mplayer in QT to display the video and add transparent text

Source: Internet
Author: User

Previously, I used the QT multi-media library phonon to play a video. It was also troublesome to use it for cross-platform playback. It is better to bring an mplayer to play the video directly. CPU usage is acceptable.

# Include <qapplication>

# Include <qtgui>

 

# Ifdef q_ws_win

Const qstring mplayerpath ("C: \ Program Files \ smplayer \ mplayer \ mplayer.exe ");

Const qstring moviefile ("C:/traffic. mpg ");

# Else

Const qstring mplayerpath ("/usr/bin/mplayer ");

Const qstring moviefile ("/home/Anderson/test. Avi ");

# Endif

 

Class maskedlabel: Public qlabel

{

Public:

Maskedlabel (qwidget * parent = 0): qlabel (parent)

{

}

 

Protected:

Void resizeevent (qresizeevent * event)

{

Qlabel: resizeevent (event );

 

Qpixmap pixmap (SIZE ());

Pixmap. Fill (QT: transparent );

Qpainter: setredirected (this, & pixmap );

Qpaintevent PE (rect ());

Paintevent (& PE );

Qpainter: restoreredirected (this );

Setmask (pixmap. Mask ());

}

};

 

Class playerwidget: Public qwidget

{

Q_object

 

Public:

Virtual ~ Playerwidget (){}

Playerwidget (qwidget * parent = 0): qwidget (parent)

{

Controller = new qpushbutton ("play ");

 

Rendertarget = new qwidget (this );

Rendertarget-> setsizepolicy (qsizepolicy: preferred, qsizepolicy: preferred ));

Rendertarget-> setattribute (QT: wa_opaquepaintevent );

Rendertarget-> setminimumsize (g00, g00 );

 

Timeline = new qslider (QT: horizontal );

 

Log = new qtextedit;

Log-> setreadonly (true );

 

Qvboxlayout * layout = new qvboxlayout;

Layout-> addwidget (Controller );

Layout-> addwidget (rendertarget );

Layout-> addwidget (timeline );

Layout-> addwidget (log );

Setlayout (layout );

 

Mplayerprocess = new qprocess (this );

 

Poller = new qtimer (this );

 

Connect (controller, signal (clicked (), this, slot (switchplaystate (); // switchplaystate is behind

Connect (mplayerprocess, signal (readyreadstandardoutput ()),

This, slot (catchoutput (); // catchoutput is behind

Connect (mplayerprocess, signal (finished (INT, qprocess: exitstatus )),

This, slot (mplayerended (INT, qprocess: exitstatus); // mplayerended

Connect (poller, signal (timeout (), this, slot (pollcurrenttime (); // pollcurrenttime is behind

Connect (timeline, signal (slidermoved (INT), this, slot (timelinechanged (INT); // timelinechanged is behind

 

Qlabel * label = new maskedlabel ();

Label-> settext ("<font color = \" # effece0 \ "size = \" 22 \ "> <B> 1now there was a man of the Pharisees named nicdemus, A member of the Jewish ruling councer. 2he came to Jesus at night and said, \ "Rabbi, we know you are a teacher who has come from God. for no one cocould perform the miraculous signs you are doing if God were not with him. \ "3in reply Jesus declared, \" I tell you the truth, no one can see th E kingdom of God unless he is born again. [a] \ "4 \" How can a man be born when he is old? \ "Nicdemus asked. \" Surely he cannot enter a second time into his mother's womb to be born! \ "</B> </font> ");

// Label-> setgeometry (rendertarget-> geometry ());

Label-> setwordwrap (true );

Label-> show ();

 

}

 

PRIVATE:

Qpushbutton * controller;

Qwidget * rendertarget;

Qprocess * mplayerprocess; // main thing

Bool isplaying;

Qslider * timeline;

Qtimer * poller;

Qtextedit * log;

 

 

Protected:

Void closeevent (qcloseevent * E)

{

Stopmplayer ();

E-> Accept ();

}

 

PRIVATE:

Bool startmplayer ()

{

If (isplaying)

Return true;

 

Qstringlist ARGs; // parameter required to run mplayer

 

// On demande? Utiliser mplayer comme backend

// Are asked to use mplayer as backend

ARGs <"-slave ";

// Et on veut ne pas avoir trop de chose? Parser

// And we want to not having too parse things to parser :)

ARGs <"-quiet ";

# Ifdef q_ws_win

// Reinterpret_cast obligatoire, winid () ne se laissant pas convertiogentiment

// Reinterpret_cast mandatory winid () not allowing convert nicely ;)

ARGs <"-wid" <qstring: Number (reinterpret_cast <long> (rendertarget-> winid ()));

ARGs <"-vo" <"DirectX: noaccel ";

// ARGs <"-nocache ";

// ARGs <"-framedrop ";

# Else

// Sur Linux, aucun driver n'a? T? N? Cessaire et pas de manip pour wid

// On Linux, no driver has been necessary and no manip for WID :)

ARGs <"-wid" <qstring: Number (rendertarget-> winid ());

 

Log-> append ("video output driver may not be necessary for your platform. Check: http://www.mplayerhq.hu/DOCS/man/en/mplayer.1.html at the video output drivers section .");

# Endif

 

ARGs <moviefile;

 

// On parse la stdout et stderr au M? Me endroit, donc On demande? "Fusionnner" Les 2 flux

// Parse the stdout and stderr in the same place, so we are asking "fusionnner"

Mplayerprocess-> setprocesschannelmode (qprocess: mergedchannels );

Mplayerprocess-> Start (mplayerpath, argS); // start the process

If (! Mplayerprocess-& gt; waitforstarted (100 ))

{

Qdebug ("can not start mplayer! ");

Return false;

}

 

// Retrieve Basic Information

Mplayerprocess-> write ("get_video_resolution ");

Mplayerprocess-> write ("get_time_length ");

 

Poller-> Start (0 );

 

Isplaying = true;

 

Return true;

}

 

Bool stopmplayer ()

{

If (! Isplaying)

Return true;

 

Mplayerprocess-> write ("quit ");

If (! Mplayerprocess-& gt; waitforfinished (100 ))

{

Qdebug ("zomg ,? A plante :(");

Return false;

}

 

Return true;

}

 

Private slots:

 

// Response to the readyreadstandardoutput message

Void catchoutput ()

{

While (mplayerprocess-> canreadline ())

{

Qbytearray buffer (mplayerprocess-> Readline ());

Log-> append (qstring (buffer ));

 

// On V? Rifie Si On A eu des R? Ponses

// It checks if we had answers

// R? Ponse? Get_video_resolution: ans_video_resolution = 'X'

// Response to get_video_resolution: ans_video_resolution

If (buffer. startswith ("ans_video_resolution "))

{

Buffer. Remove (0, 21); // vire ans_video_resolution =

Buffer. Replace (qbytearray ("'"), qbytearray (""));

Buffer. Replace (qbytearray (""), qbytearray (""));

Buffer. Replace (qbytearray (""), qbytearray (""));

Buffer. Replace (qbytearray (""), qbytearray (""));

Int sepindex = buffer. indexof ('x ');

Int resx = buffer. Left (sepindex). toint ();

Int Resy = buffer. mid (sepindex + 1). toint ();

Rendertarget-> setminimumsize (resx, Resy );

}

// R? Ponse? Get_time_length: ans_length = XX. yy

// Response to get_time_length: ans_length =

Else if (buffer. startswith ("ans_length "))

{

Buffer. Remove (0, 11); // vire ans_length =

Buffer. Replace (qbytearray ("'"), qbytearray (""));

Buffer. Replace (qbytearray (""), qbytearray (""));

Buffer. Replace (qbytearray (""), qbytearray (""));

Buffer. Replace (qbytearray (""), qbytearray (""));

Float maxtime = buffer. tofloat ();

Timeline-> setmaximum (static_cast <long> (maxtime + 1 ));

}

// R? Ponse? Get_time_pos: ans_time_position = XX. Y

// Response to get_time_pos: ans_time_position = 2.4

Else if (buffer. startswith ("ans_time_position "))

{

Buffer. Remove (0, 18); // vire ans_time_position =

Buffer. Replace (qbytearray ("'"), qbytearray (""));

Buffer. Replace (qbytearray (""), qbytearray (""));

Buffer. Replace (qbytearray (""), qbytearray (""));

Buffer. Replace (qbytearray (""), qbytearray (""));

Float currtime = buffer. tofloat ();

Timeline-> setvalue (static_cast <long> (currtime + 1 ));

}

// Qapp-> processevents ();

}

}

 

Void pollcurrenttime ()

{

Mplayerprocess-> write ("get_time_pos ");

}

 

// Dirige la timeline

// Directs the timeline

Void timelinechanged (int pos)

{

Mplayerprocess-> write (qstring ("Seek" + qstring: Number (POS) + "2"). toutf8 ());

}

 

// Play/stop

Void switchplaystate ()

{

If (! Isplaying)

{

If (! Startmplayer ())

Return;

 

Log-> clear ();

Controller-> settext ("stop ");

Isplaying = true;

}

Else

{

If (! Stopmplayer ())

Return;

 

Poller-> stop ();

Log-> clear ();

Controller-> settext ("play ");

Isplaying = false;

}

}

 

Void mplayerended (INT exitcode, qprocess: exitstatus)

{

Isplaying = false;

Controller-> settext ("play ");

Poller-> stop ();

}

 

 

};

 

Int main (INT argc, char ** argv)

{

Qapplication app (argc, argv );

 

Playerwidget * PW = new playerwidget;

PW-> show ();

 

Return app.exe C ();

}

 

# Include "Hello. MOC"

 
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.