ffplay

Ответить
ya
^-^
Сообщения: 2336
Зарегистрирован: 16 дек 2021, 19:56

ffplay

Сообщение ya »

video_play_with_duration.cpp

Код: Выделить всё

#include <iostream>
#include <iostream>
#include <string>
#include <cstdio>
#include <memory>


double get_video_duration(const std::string& filepath)
{
    char buffer[128];
    std::string command = "ffprobe -v error -show_entries format=duration -of default=nw=1:nk=1 \"" + filepath + "\"";
    std::shared_ptr<FILE> pipe(popen(command.c_str(), "r"), pclose);
    if (!pipe) throw std::runtime_error("popen() failed!");
    double video_duration = 0.0;
    if (fgets(buffer, 128, pipe.get()) != nullptr) {
        video_duration = std::stod(buffer);
    }
    return video_duration;
}

int main(int argc, char* argv[])
{
    //using namespace std::literals;
    if (argc != 2 ) exit (1);
    std::string str1 = "ffplay -i ";

    // Obtain video duration
    double video_duration = get_video_duration(argv[1]);
    std::string formatted_duration = std::to_string(video_duration);

    // Creating a string using string literal
	std::string str2 = " -vf \"scale=1100:-1,drawtext=text='%{pts\\:hms}/" + formatted_duration + "':box=1:fontcolor=black:shadowcolor=white:shadowx=1:shadowy=1:fontsize=16:x=(w-tw)-(lh):y=h-(2*lh)\" -autoexit -stats ";

    // Concatenating strings
    std::string str3 = str1 + "\"" + argv[1] + "\"" + str2;

    // Print out the result
    std::cout << str3 << '\n';

    const char * c = str3.c_str();
    system(c);
}

g++ -o video_play_with_duration -std=c++11 video_play_with_duration.cpp

./video_play_with_duration <path_to_video_file>
ya
^-^
Сообщения: 2336
Зарегистрирован: 16 дек 2021, 19:56

Re: ffplay

Сообщение ya »

Код: Выделить всё

#include <iostream>
#include <string>
#include <cstdio>
#include <memory>
#include <iomanip>
#include <sstream>

double get_video_duration(const std::string& filepath)
{
    char buffer[128];
    std::string command = "ffprobe -v error -show_entries format=duration -of default=nw=1:nk=1 \"" + filepath + "\"";
    std::shared_ptr<FILE> pipe(popen(command.c_str(), "r"), pclose);
    if (!pipe) throw std::runtime_error("popen() failed!");
    double video_duration = 0.0;
    if (fgets(buffer, 128, pipe.get()) != nullptr) {
        video_duration = std::stod(buffer);
    }
    return video_duration;
}

std::string format_duration(double duration) {
    int hours = static_cast<int>(duration / 3600);
    duration -= hours * 3600;
    int minutes = static_cast<int>(duration / 60);
    duration -= minutes * 60;
    int seconds = static_cast<int>(duration);

    std::ostringstream formatted_duration;
    formatted_duration << std::setfill('0') << std::setw(2) << hours << "\\:"
                       << std::setw(2) << minutes << "\\:"
                       << std::setw(2) << seconds;

    return formatted_duration.str();
}


int main(int argc, char* argv[])
{
    //using namespace std::literals;
    if (argc != 2 ) exit (1);
    std::string str1 = "ffplay -i ";

    // Obtain video duration
    double video_duration = get_video_duration(argv[1]);
    //std::string formatted_duration = std::to_string(video_duration);
	std::string formatted_duration = format_duration(video_duration);

    // Creating a string using string literal
	std::string str2 = " -vf \"scale=1100:-1,drawtext=text='%{pts\\:hms}/" + formatted_duration + "':box=1:fontcolor=black:shadowcolor=white:shadowx=1:shadowy=1:fontsize=16:x=(w-tw)-(lh):y=h-(2*lh)\" -autoexit -stats ";

    // Concatenating strings
    std::string str3 = str1 + "\"" + argv[1] + "\"" + str2;

    // Print out the result
    std::cout << str3 << '\n';

    const char * c = str3.c_str();
    system(c);
}
ya
^-^
Сообщения: 2336
Зарегистрирован: 16 дек 2021, 19:56

Re: ffplay

Сообщение ya »

Код: Выделить всё

#include <iostream>
#include <string>
#include <cstdio>
#include <memory>
#include <iomanip>
#include <sstream>
#include <sys/stat.h>

bool file_exists(const std::string& filepath) {
    struct stat buffer;
    return (stat(filepath.c_str(), &buffer) == 0);
}

double get_video_duration(const std::string& filepath) {
    char buffer[128];
    std::string command = "ffprobe -v error -show_entries format=duration -of default=nw=1:nk=1 -i \"" + filepath + "\"";
    std::shared_ptr<FILE> pipe(popen(command.c_str(), "r"), pclose);
    if (!pipe) throw std::runtime_error("popen() failed!");
    double video_duration = 0.0;
    if (fgets(buffer, 128, pipe.get()) != nullptr) {
        video_duration = std::stod(buffer);
    }
    return video_duration;
}

std::string format_duration(double duration) {
    int hours = static_cast<int>(duration / 3600);
    duration -= hours * 3600;
    int minutes = static_cast<int>(duration / 60);
    duration -= minutes * 60;
    int seconds = static_cast<int>(duration);

    std::ostringstream formatted_duration;
    formatted_duration << std::setfill('0') << std::setw(2) << hours << "\\:"
                       << std::setw(2) << minutes << "\\:"
                       << std::setw(2) << seconds;

    return formatted_duration.str();
}

void play_video(const std::string &filepath) {
    double video_duration = get_video_duration(filepath);
    std::string formatted_duration = format_duration(video_duration);

    std::string command = "ffplay -i \"" + filepath + "\""
        " -vf \"scale=1100:-1,drawtext=text='%{pts\\:hms}/" + formatted_duration +
        "':box=1:fontcolor=black:shadowcolor=white:shadowx=1:shadowy=1:fontsize=16:x=(w-tw)-(lh):y=h-(2*lh)\""
        " -autoexit -stats ";

    system(command.c_str());
}

int main(int argc, char* argv[]) {
    if (argc != 2) {
        std::cerr << "Usage: " << argv[0] << " <video-file>" << std::endl;
        return 1;
    }

    std::string filepath = argv[1];
    if (!file_exists(filepath)) {
        std::cerr << "Error: The provided file does not exist." << std::endl;
        return 1;
    }

    try {
        play_video(filepath);
    } catch (const std::runtime_error &e) {
        std::cerr << "Error: " << e.what() << std::endl;
        return 1;
    }

    return 0;
}
ya
^-^
Сообщения: 2336
Зарегистрирован: 16 дек 2021, 19:56

ffplay win7

Сообщение ya »

Код: Выделить всё

#include <iostream>
#include <string>
#include <cstdio>
#include <memory>
#include <iomanip>
#include <sstream>
#include <sys/stat.h>

bool file_exists(const std::string& filepath) {
    struct stat buffer;
    return (stat(filepath.c_str(), &buffer) == 0);
}

double get_video_duration(const std::string& filepath) {
    char buffer[128];
    std::string command = "ffprobe -v error -show_entries format=duration -of default=nw=1:nk=1 -i \"" + filepath + "\"";
    std::shared_ptr<FILE> pipe(popen(command.c_str(), "r"), pclose);
    if (!pipe) throw std::runtime_error("popen() failed!");
    double video_duration = 0.0;
    if (fgets(buffer, 128, pipe.get()) != nullptr) {
        video_duration = std::stod(buffer);
    }
    return video_duration;
}

std::string format_duration(double duration) {
    int hours = static_cast<int>(duration / 3600);
    duration -= hours * 3600;
    int minutes = static_cast<int>(duration / 60);
    duration -= minutes * 60;
    int seconds = static_cast<int>(duration);

    std::ostringstream formatted_duration;
    formatted_duration << std::setfill('0') << std::setw(2) << hours << "\\:"
                       << std::setw(2) << minutes << "\\:"
                       << std::setw(2) << seconds;

    return formatted_duration.str();
}

void play_video(const std::string &filepath) {
    double video_duration = get_video_duration(filepath);
    std::string formatted_duration = format_duration(video_duration);

    std::string command = "ffplay -i \"" + filepath + "\""
        " -vf \"scale=1100:-1,drawtext=text='%{pts\\:gmtime\\:0\\:%H\\\\\\:%M\\\\\\:%S}/" + formatted_duration +
        "':box=1:fontcolor=black:shadowcolor=white:shadowx=1:shadowy=1:fontsize=16:x=(w-tw)-(lh):y=h-(2*lh)\""
        " -autoexit -stats ";

    system(command.c_str());
}

int main(int argc, char* argv[]) {
    if (argc != 2) {
        std::cerr << "Usage: " << argv[0] << " <video-file>" << std::endl;
        return 1;
    }

    std::string filepath = argv[1];
    if (!file_exists(filepath)) {
        std::cerr << "Error: The provided file does not exist." << std::endl;
        return 1;
    }

    try {
        play_video(filepath);
    } catch (const std::runtime_error &e) {
        std::cerr << "Error: " << e.what() << std::endl;
        return 1;
    }

    return 0;
}
ya
^-^
Сообщения: 2336
Зарегистрирован: 16 дек 2021, 19:56

ffplay deb9

Сообщение ya »

ffplay deb9
Вложения
play4.cpp
(2.35 КБ) 2261 скачивание
play4.cpp
(2.35 КБ) 2261 скачивание
ya
^-^
Сообщения: 2336
Зарегистрирован: 16 дек 2021, 19:56

Re: ffplay

Сообщение ya »

Код: Выделить всё

#include <iostream>
#include <string>
#include <cstdio>
#include <memory>
#include <iomanip>
#include <sstream>
#include <sys/stat.h>

bool file_exists(const std::string& filepath) {
    struct stat buffer;
    return (stat(filepath.c_str(), &buffer) == 0);
}

double get_video_duration(const std::string& filepath) {
    char buffer[128];
    std::string command = "ffprobe -v error -show_entries format=duration -of default=nw=1:nk=1 -i \"" + filepath + "\"";
    std::shared_ptr<FILE> pipe(popen(command.c_str(), "r"), pclose);
    if (!pipe) throw std::runtime_error("popen() failed!");
    double video_duration = 0.0;
    if (fgets(buffer, 128, pipe.get()) != nullptr) {
        video_duration = std::stod(buffer);
    }
    return video_duration;
}

std::string format_duration(double duration) {
    int hours = static_cast<int>(duration / 3600);
    duration -= hours * 3600;
    int minutes = static_cast<int>(duration / 60);
    duration -= minutes * 60;
    int seconds = static_cast<int>(duration);

    std::ostringstream formatted_duration;
    formatted_duration << std::setfill('0') << std::setw(2) << hours << "\\:"
                       << std::setw(2) << minutes << "\\:"
                       << std::setw(2) << seconds;

    return formatted_duration.str();
}

void play_video(const std::string &filepath) {
    double video_duration = get_video_duration(filepath);
    std::string formatted_duration = format_duration(video_duration);

    std::string command = "ffplay -i \"" + filepath + "\""
        " -vf \"scale='min(1280,iw)':min'(850,ih)':force_original_aspect_ratio=decrease,drawtext=text='%{pts\\:gmtime\\:0\\:%H\\\\\\:%M\\\\\\:%S}/" + formatted_duration +
        "':box=1:fontcolor=black:shadowcolor=white:shadowx=1:shadowy=1:fontsize=16:x=(w-tw)-(lh):y=h-(2*lh)\""
        " -autoexit -stats ";

    system(command.c_str());
}

int main(int argc, char* argv[]) {
    if (argc != 2) {
        std::cerr << "Usage: " << argv[0] << " <video-file>" << std::endl;
        return 1;
    }

    std::string filepath = argv[1];
    if (!file_exists(filepath)) {
        std::cerr << "Error: The provided file does not exist." << std::endl;
        return 1;
    }

    try {
        play_video(filepath);
    } catch (const std::runtime_error &e) {
        std::cerr << "Error: " << e.what() << std::endl;
        return 1;
    }

    return 0;
}
ya
^-^
Сообщения: 2336
Зарегистрирован: 16 дек 2021, 19:56

Re: ffplay

Сообщение ya »

ffplay crossplatform
Вложения
play3.4.1.cpp
(2.99 КБ) 2271 скачивание
play3.4.1.cpp
(2.99 КБ) 2271 скачивание
ya
^-^
Сообщения: 2336
Зарегистрирован: 16 дек 2021, 19:56

Re: ffplay

Сообщение ya »

play3.4.2

Код: Выделить всё

#include <iostream>
#include <string>
#include <cstdio>
#include <memory>
#include <iomanip>
#include <sstream>
#include <sys/stat.h>

bool file_exists(const std::string& filepath)
{
	struct stat buffer;
	return (stat(filepath.c_str(), &buffer) == 0);
}

double get_video_duration(const std::string& filepath)
{
	char buffer[128];
	std::string command = "ffprobe -v error -show_entries format=duration -of default=nw=1:nk=1 -i \"" + filepath + "\"";
	std::shared_ptr<FILE> pipe(popen(command.c_str(), "r"), pclose);
	if (!pipe) throw std::runtime_error("popen() failed!");
	double video_duration = 0.0;
	if (fgets(buffer, 128, pipe.get()) != nullptr) {
		video_duration = std::stod(buffer);
	}
	return video_duration;
}

std::string format_duration(double duration)
{
	int hours = static_cast<int>(duration / 3600);
	duration -= hours * 3600;
	int minutes = static_cast<int>(duration / 60);
	duration -= minutes * 60;
	int seconds = static_cast<int>(duration);

	std::ostringstream formatted_duration;
	formatted_duration << std::setfill('0') << std::setw(2) << hours << "\\:"
	                   << std::setw(2) << minutes << "\\:"
	                   << std::setw(2) << seconds;

	return formatted_duration.str();
}

void play_video(const std::string &filepath)
{
	double video_duration = get_video_duration(filepath);
	std::string formatted_duration = format_duration(video_duration);


#ifdef _WIN32
	std::string command = "ffplay -i \"" + filepath + "\""
	                      " -vf \"scale='min(1280,iw)':min'(850,ih)':force_original_aspect_ratio=decrease,drawtext=font='Courier New':text='%{pts\\:gmtime\\:0\\:%H\\\\\\:%M\\\\\\:%S}/" + formatted_duration +
	                      "':box=1:fontcolor=black:shadowcolor=white:shadowx=1:shadowy=1:fontsize=16:x=(w-tw)-(lh):y=h-(2*lh)\""
	                      " -autoexit -stats ";
#else
	std::string command = "ffplay -i \"" + filepath + "\""
	                      " -vf \"scale='min(1280,iw)':min'(850,ih)':force_original_aspect_ratio=decrease,drawtext=font='Courier':text='%{pts\\:gmtime\\:0\\:%H\\\\\\\\\\\\:%M\\\\\\\\\\\\:%S}/" + formatted_duration +
	                      "':box=1:fontcolor=black:shadowcolor=white:shadowx=1:shadowy=1:fontsize=16:x=(w-tw)-(lh):y=h-(2*lh)\""
	                      " -autoexit -stats ";
#endif

	system(command.c_str());

/*
 std::string command = "ffplay -i \"" + filepath + "\""
        " -vf \"scale=1100:-1,drawtext=text='%{pts\\:gmtime\\:0\\:%H\\\\\\\\\\\\:%M\\\\\\\\\\\\:%S}/" + formatted_duration +
        "':box=1:fontcolor=black:shadowcolor=white:shadowx=1:shadowy=1:fontsize=16:x=(w-tw)-(lh):y=h-(2*lh)\""
        " -autoexit -stats ";

*/
}

int main(int argc, char* argv[])
{
	if (argc != 2) {
		std::cerr << "Usage: " << argv[0] << " <video-file>" << std::endl;
		return 1;
	}

	std::string filepath = argv[1];
	if (!file_exists(filepath)) {
		std::cerr << "Error: The provided file does not exist." << std::endl;
		return 1;
	}

	try {
		play_video(filepath);
	} catch (const std::runtime_error &e) {
		std::cerr << "Error: " << e.what() << std::endl;
		return 1;
	}

	return 0;
}
Откомпилировать:
g++ -std=c++11 -o play3.4.2 play3.4.2.cpp

Для зауска необходим установленный ffmpeg
./play3.4.2 видео.mp4
для win ffplay можно скачать с https://ffmpeg.org -> https://github.com/BtbN/FFmpeg-Builds/releases
и добавить в переменную среды окружения path путь на каталог, в котором находится ffmpeg.exe
Win+R -> sysdm.cpl -> вкладка "Дополнительно" -> переменные среды -> системные переменные -> path

Проверить правильность установки системной переменной path можно командой win+R -> cmd

Код: Выделить всё

ffmpeg -v
Посмотреть информацию о видеофайле можно командой:

Код: Выделить всё

ffprobe -i видеофайл.mp4
Зарегистрировать как приложение для Linux:
/usr/share/applications/play.desktop

Код: Выделить всё

[Desktop Entry]
Type=Application
Name=play3.4.2 Media Player
Name[ca]=Reproductor multimèdia play3.4.2
Name[cs]=play3.4.2 přehrávač
Name[da]=play3.4.2-medieafspiller
Name[fr]=Lecteur multimédia play3.4.2
Name[pl]=Odtwarzacz mpv
Name[ru]=Проигрыватель play3.4.2
Name[zh_CN]=play3.4.2 媒体播放器
Name[zh_TW]=play3.4.2 媒體播放器
GenericName=Multimedia player
GenericName[cs]=Multimediální přehrávač
GenericName[da]=Multimedieafspiller
GenericName[fr]=Lecteur multimédia
GenericName[zh_CN]=多媒体播放器
GenericName[zh_TW]=多媒體播放器
Comment=Play movies and songs
Comment[ca]=Reproduïu vídeos i cançons
Comment[cs]=Přehrává filmy a hudbu
Comment[da]=Afspil film og sange
Comment[de]=Filme und Musik abspielen
Comment[es]=Reproduzca vídeos y canciones
Comment[fr]=Lire des vidéos et des musiques
Comment[it]=Lettore multimediale
Comment[pl]=Odtwarzaj filmy i muzykę
Comment[ru]=Воспроизвести фильмы и музыку
Comment[zh_CN]=播放电影和歌曲
Comment[zh_TW]=播放電影和歌曲
Icon=play3.4.2
TryExec=/usr/bin/play3.4.2
Exec=/usr/bin/play3.4.2 -- %U
Terminal=false
Categories=AudioVideo;Audio;Video;Player;TV;
MimeType=application/x-matroska;application/ogg;application/x-ogg;application/mxf;application/sdp;application/smil;application/x-smil;application/streamingmedia;application/x-streamingmedia;application/vnd.rn-realmedia;application/vnd.rn-realmedia-vbr;audio/aac;audio/x-aac;audio/vnd.dolby.heaac.1;audio/vnd.dolby.heaac.2;audio/aiff;audio/x-aiff;audio/m4a;audio/x-m4a;application/x-extension-m4a;audio/mp1;audio/x-mp1;audio/mp2;audio/x-mp2;audio/mp3;audio/x-mp3;audio/mpeg;audio/mpeg2;audio/mpeg3;audio/mpegurl;audio/x-mpegurl;audio/mpg;audio/x-mpg;audio/rn-mpeg;audio/musepack;audio/x-musepack;audio/ogg;audio/scpls;audio/x-scpls;audio/vnd.rn-realaudio;audio/wav;audio/x-pn-wav;audio/x-pn-windows-pcm;audio/x-realaudio;audio/x-pn-realaudio;audio/x-ms-wma;audio/x-pls;audio/x-wav;video/mpeg;video/x-mpeg2;video/x-mpeg3;video/mp4v-es;video/x-m4v;video/mp4;application/x-extension-mp4;video/divx;video/vnd.divx;video/msvideo;video/x-msvideo;video/ogg;video/quicktime;video/vnd.rn-realvideo;video/x-ms-afs;video/x-ms-asf;audio/x-ms-asf;application/vnd.ms-asf;video/x-ms-wmv;video/x-ms-wmx;video/x-ms-wvxvideo;video/x-avi;video/avi;video/x-flic;video/fli;video/x-flc;video/flv;video/x-flv;video/x-theora;video/x-theora+ogg;video/x-matroska;video/mkv;audio/x-matroska;application/x-matroska;video/webm;audio/webm;audio/vorbis;audio/x-vorbis;audio/x-vorbis+ogg;video/x-ogm;video/x-ogm+ogg;application/x-ogm;application/x-ogm-audio;application/x-ogm-video;application/x-shorten;audio/x-shorten;audio/x-ape;audio/x-wavpack;audio/x-tta;audio/AMR;audio/ac3;audio/eac3;audio/amr-wb;video/mp2t;audio/flac;audio/mp4;application/x-mpegurl;video/vnd.mpegurl;application/vnd.apple.mpegurl;audio/x-pn-au;video/3gp;video/3gpp;video/3gpp2;audio/3gpp;audio/3gpp2;video/dv;audio/dv;audio/opus;audio/vnd.dts;audio/vnd.dts.hd;audio/x-adpcm;application/x-cue;audio/m3u;
X-KDE-Protocols=ftp,http,https,mms,rtmp,rtsp,sftp,smb
Keywords=mpv;media;player;video;audio;tv;
обновить mime-базу

Код: Выделить всё

sudo update-mime-database /usr/share/mime
Чтобы проверить скомпилированное на ошибки можно воспользоваться валгриндом:

Код: Выделить всё

valgrind play3.4.2
Вложения
play3.4.2.zip
откомпилированная версия для win
(4.91 МБ) 1484 скачивания
play3.4.2.zip
откомпилированная версия для win
(4.91 МБ) 1484 скачивания
Ответить