Skip to content

Instantly share code, notes, and snippets.

@wolfgangkarall
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wolfgangkarall/d96b181292738e0278d8 to your computer and use it in GitHub Desktop.
Save wolfgangkarall/d96b181292738e0278d8 to your computer and use it in GitHub Desktop.
Calculating hashes for video files as used by MythTV in the DB table videometadata
// on Debian/Ubuntu:
// - install libqt4-dev
// - put file into a directory called mythhash
// - run:
// qmake -project
// qmake
// make
// C++ headers
#include <iostream>
using namespace std;
// Qt headers
#include <QtCore>
#include <QDataStream>
#include <QFileInfo>
#include <QFile>
#include <QDebug>
QString FileHash(QString filename)
{
QFile file(filename);
QFileInfo fileinfo(file);
qint64 initialsize = fileinfo.size();
quint64 hash = 0;
if (initialsize == 0)
return QString("NULL");
if (file.open(QIODevice::ReadOnly))
hash = initialsize;
else
{
return QString("NULL");
}
file.seek(0);
QDataStream stream(&file);
stream.setByteOrder(QDataStream::LittleEndian);
for (quint64 tmp = 0, i = 0; i < 65536/sizeof(tmp); i++)
{
stream >> tmp;
hash += tmp;
}
file.seek(initialsize - 65536);
for (quint64 tmp = 0, i = 0; i < 65536/sizeof(tmp); i++)
{
stream >> tmp;
hash += tmp;
}
file.close();
QString output = QString("%1").arg(hash, 0, 16);
return output;
}
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
if (argc == 1) {
std::cout << "Usage: mythhash <file>" << std::endl;
exit(1);
} else {
QString filename = QString::fromUtf8(argv[1]);
QString filehash = FileHash(filename);
std::string utf8_text = filehash.toUtf8().constData();
std::cout << utf8_text << std::endl;
exit(0);
}
}
/* vim: set expandtab tabstop=4 shiftwidth=4: */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment