video.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include "sxiv.h"
  2. // I know... Dirty methods... If you have better ideas
  3. // pull requests are more than welcomed
  4. bool is_video(const char *file) {
  5. #if 1
  6. // because the other way is extreamly slow...
  7. // TODO: find a proper way
  8. const char *ext = file + strlen(file) - 4;
  9. return
  10. strcmp(".mp4", ext) == 0 ||
  11. strcmp(".3gp", ext) == 0 ||
  12. strcmp(".mkv", ext) == 0;
  13. #else
  14. bool res = false;
  15. const char *tmpl = "file -b --mime-type '%s'";
  16. char *command = (char *) malloc(strlen(file) + strlen(tmpl) + 1);
  17. sprintf(command, tmpl, file);
  18. char buffer[128];
  19. FILE *fpipe = (FILE *) popen(command, "r");
  20. if (fpipe) {
  21. while (fgets(buffer, sizeof buffer, fpipe) != NULL);
  22. char *startswith = "video";
  23. res = strncmp(startswith, buffer, strlen(startswith)) == 0;
  24. }
  25. free(command);
  26. pclose(fpipe);
  27. return res;
  28. #endif
  29. }
  30. char *get_video_thumb(char *file) {
  31. const char *script = "f=\"%s\" \n"
  32. "t=$(echo \"$f\" | md5sum | awk '{print $1}') \n"
  33. "t=\"$HOME/.cache/sxiv/vid-$t.jpg\" \n"
  34. "if [ ! -f \"$t\" ] || [ \"$t\" -ot \"$f\" ]; then \n"
  35. " rm -f \"$t\" \n"
  36. " ffmpegthumbnailer -i \"$f\" -o \"$t\" -s 0 >/dev/null 2>/dev/null || exit 1 \n"
  37. "fi \n"
  38. "printf '%%s' \"$t\" \n";
  39. char *command = (char *) malloc(strlen(file) + strlen(script) + 1);
  40. sprintf(command, script, file);
  41. size_t buffer_size = 1024;
  42. char *buffer = (char *) malloc(buffer_size);
  43. FILE *fpipe = (FILE *) popen(command, "r");
  44. if (fpipe) {
  45. while (fgets(buffer, buffer_size, fpipe) != NULL);
  46. }
  47. free(command);
  48. pclose(fpipe);
  49. return buffer;
  50. }