Skip to content

Commit

Permalink
navigate through viewable files
Browse files Browse the repository at this point in the history
  • Loading branch information
Like4Schnitzel committed Feb 24, 2024
1 parent 5c7b47b commit b2acc7f
Show file tree
Hide file tree
Showing 11 changed files with 241 additions and 57 deletions.
8 changes: 6 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.5)
set(CMAKE_CXX_STANDARD 20)
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread -stdlib=libc++ -std=c++20" )
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread" )
project( video-to-terminal )
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
Expand All @@ -18,5 +18,9 @@ target_link_libraries( decoder stdc++fs )
add_executable( img-viewer img-viewer/main.cpp img-viewer/imgviewer.cpp libs/variousutils.cpp )
target_link_libraries( img-viewer stdc++fs ${OpenCV_LIBS} )

add_executable( media-viewer media-viewer/main.cpp libs/termutils.cpp libs/variousutils.cpp libs/binaryutils.cpp )
add_executable (
media-viewer media-viewer/main.cpp media-viewer/mediaviewer.cpp
decoder/vtdidecoder.cpp
libs/termutils.cpp libs/variousutils.cpp libs/binaryutils.cpp libs/_kbhit.cpp
)
target_link_libraries( media-viewer stdc++fs ${OpenCV_LIBS} )
2 changes: 1 addition & 1 deletion src/decoder/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ int main(int argc, char** argv)
}

VTDIDecoder player = VTDIDecoder(videoPath);
player.getStaticInfo();
player.readStaticInfo();

std::cout << "VTDI Info:\n";
std::cout << "Version: " << player.getVersion() << "\n";
Expand Down
13 changes: 8 additions & 5 deletions src/decoder/vtdidecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@

namespace vtt {

VTDIDecoder::VTDIDecoder(std::string path)
VTDIDecoder::VTDIDecoder(std::string path, bool debugInfo)
{
this->version = 0;

this->vtdiFile.open(path);
if (!vtdiFile.good())
{
throw std::runtime_error("Cannot open file to read from.");
std::stringstream errorMessage;
errorMessage << "Cannot open file \"" << path << "\" to read from.";
throw std::runtime_error(errorMessage.str());
}
std::cout << "File opened succesfully.\n";
if (debugInfo)
std::cout << "File opened succesfully.\n";
vtdiFile.close();
vtdiFile.clear();
vtdiPath = path;
Expand All @@ -37,7 +40,7 @@ T applyAssign(T num, int& index, const Byte* sib)
return result;
}

void VTDIDecoder::getStaticInfo()
void VTDIDecoder::readStaticInfo()
{
// these are taken from the spec
const int expectedSig[] = {86, 84, 68, 73};
Expand Down Expand Up @@ -97,7 +100,7 @@ void VTDIDecoder::playVideo()

if (this->version == 0)
{
throw std::runtime_error("It seems static info hasn't been initialized yet. Try running VTDIDecoder.getStaticInfo()");
throw std::runtime_error("It seems static info hasn't been initialized yet. Try running VTDIDecoder.readStaticInfo()");
}

auto terminalDimensions = TermUtils::getTerminalDimensions();
Expand Down
4 changes: 2 additions & 2 deletions src/decoder/vtdidecoder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ class VTDIDecoder {
float FPS;
uint16_t version;
public:
VTDIDecoder(std::string path);
void getStaticInfo();
VTDIDecoder(std::string path, bool debugInfo = true);
void readStaticInfo();
void playVideo();
void readAndDisplayNextFrame(bool display=true, bool save=true);
void displayCurrentFrame();
Expand Down
22 changes: 8 additions & 14 deletions src/libs/_kbhit.h → src/libs/_kbhit.cpp
Original file line number Diff line number Diff line change
@@ -1,29 +1,22 @@
#pragma once
#include "_kbhit.hpp"

#ifdef WIN32
#ifndef WIN32

// _kbhit() is originally from this library but it is windows only
#include <conio.h>

#else
// taken from https://www.flipcode.com/archives/_kbhit_for_Linux.shtml
// very slightly modified (#include <strops.h> to #include <sys/ioctl.h>)
// very slightly modified in the header file (#include <strops.h> to #include <sys/ioctl.h>)

/**
Linux (POSIX) implementation of _kbhit().
Morgan McGuire, [email protected]
*/

#include <stdio.h>
#include <sys/select.h>
#include <termios.h>
#include <sys/ioctl.h>

int _kbhit() {
int _kbhit()
{
static const int STDIN = 0;
static bool initialized = false;

if (! initialized) {
if (! initialized)
{
// Use termios to turn off line buffering
termios term;
tcgetattr(STDIN, &term);
Expand All @@ -37,4 +30,5 @@ int _kbhit() {
ioctl(STDIN, FIONREAD, &bytesWaiting);
return bytesWaiting;
}

#endif
17 changes: 17 additions & 0 deletions src/libs/_kbhit.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#ifdef WIN32

// _kbhit() is originally from this library but it is windows only
#include <conio.h>

#else

#include <stdio.h>
#include <sys/select.h>
#include <termios.h>
#include <sys/ioctl.h>

int _kbhit();

#endif
23 changes: 23 additions & 0 deletions src/libs/termutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,29 @@ TermUtils::TermUtils()
hidden = false;
}

KeyPress TermUtils::getKeyPress()
{
KeyPress kp;
kp.keyDown = false;
kp.keyValue = 0;

if (_kbhit())
{
kp.keyDown = true;

char tmp;
std::vector<char> stdinVals;
while (_kbhit())
{
std::cin >> tmp;
stdinVals.push_back(tmp);
}
kp.keyValue = BinaryUtils::byteArrayToUint((Byte*)stdinVals.data(), stdinVals.size());
}

return kp;
}

// taken from https://stackoverflow.com/a/23370070
#ifdef WIN32

Expand Down
13 changes: 12 additions & 1 deletion src/libs/termutils.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
#pragma once

#include <array>
#include <vector>
#include <iostream>
#include "binaryutils.hpp"
#include "_kbhit.hpp"

#ifdef WIN32
#include <windows.h>
#else
Expand All @@ -8,10 +14,14 @@
#include <unistd.h>
#include <termios.h>
#endif
#include <array>

namespace vtt {

struct KeyPress {
bool keyDown;
unsigned int keyValue;
};

class TermUtils {
private:
bool hidden;
Expand All @@ -24,6 +34,7 @@ class TermUtils {
#endif
public:
TermUtils();
static KeyPress getKeyPress();
static std::array<int, 2> getTerminalDimensions();
void hideInput();
void showInput();
Expand Down
46 changes: 16 additions & 30 deletions src/media-viewer/main.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#include <iostream>
#include <termio.h>
#include "mediaviewer.hpp"
#include "../libs/variousutils.hpp"
#include "../libs/termutils.hpp"
#include "../libs/binaryutils.hpp"
#include "../libs/_kbhit.h"

using namespace std;
using namespace vtt;
Expand All @@ -12,51 +10,39 @@ int main(int argc, char** argv)
{
filesystem::path path;
if (argc > 1)
{
path = filesystem::absolute(argv[1]);
if (!filesystem::is_directory(path))
path = path.parent_path();
}
else
path = filesystem::current_path();

auto files = VariousUtils::getFilesInDir(path);
auto mv = MediaViewer(path);

if (mv.empty())
{
cout << "The selected folder " << path << " does not contain viewable files.\n";
return 0;
}

TermUtils tu;
int fileIndex = 0;
cout << "Selected file: " << files[fileIndex].d_name << "\n";
cout << "Selected file: " << mv.current()->path << "\n" << flush;
unsigned char keyValue;
cout << "waiting for a keypress...\npress ESC to close the program.\n" << flush;
tu.hideInput();
while (true)
{
if (_kbhit())
KeyPress kp = TermUtils::getKeyPress();
if (kp.keyDown)
{
std::vector<char> vals;
while (_kbhit())
{
cin >> keyValue;
vals.push_back(keyValue);
}
int combinedKeyCode = BinaryUtils::byteArrayToUint((Byte*)vals.data(), vals.size());

// right arrow
if (combinedKeyCode == 1792835)
{
fileIndex = (fileIndex + 1) % files.size();
cout << "Selected file: " << files[fileIndex].d_name << "\n";
}
if (kp.keyValue == 1792835)
cout << "Selected file: " << mv.next()->path << "\n";

// left arrow
if (combinedKeyCode == 1792836)
{
fileIndex--;
if (fileIndex < 0) fileIndex += files.size();
cout << "Selected file: " << files[fileIndex].d_name << "\n";
}
if (kp.keyValue == 1792836)
cout << "Selected file: " << mv.current()->path << "\n";

// ESC
if (combinedKeyCode == 27)
if (kp.keyValue == 27)
break;
}
}
Expand Down
Loading

0 comments on commit b2acc7f

Please sign in to comment.