1
0

playlist4cut.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include <cstring>
  2. #include <cstdio>
  3. #include <cctype>
  4. #include <vector>
  5. #include <algorithm>
  6. #define LINE_SIZE 1024
  7. struct Timestamp {
  8. char start[64];
  9. char end[64];
  10. char name[512];
  11. };
  12. char * strtrim(char *s);
  13. int main(int argc, char* argv[])
  14. {
  15. if (2 > argc) {
  16. puts("Usage: playlist4cut.exe playlist.txt [timelist.txt] ");
  17. return -1;
  18. }
  19. // 时间戳数据
  20. Timestamp one = { "00:08", "01:18", "Audiomachine - Age of Dragons" };
  21. std::vector<Timestamp> vec_one;
  22. char line[LINE_SIZE]; // 读取一行 字符串
  23. char* pch;
  24. FILE* input = fopen(argv[1], "r");
  25. if (input == NULL)
  26. return -1;
  27. while (fgets(line, LINE_SIZE, input)) {
  28. if (pch = strtok(line, " \t\n")) {
  29. strcpy(one.start, pch);
  30. pch = strtok(NULL, "\n\r");
  31. pch = strtrim(pch);
  32. strcpy(one.name, pch);
  33. vec_one.push_back(one); // 把读取的时间戳装载到容器
  34. }
  35. }
  36. // 副本偏移,用来修改 Timestamp.end
  37. std::vector<Timestamp> vec_copy = vec_one;
  38. vec_copy.push_back(one);
  39. vec_copy.erase(vec_copy.begin());
  40. FILE* output;
  41. if (3 == argc) // 如果没有输出文件,输出到屏幕
  42. output = fopen(argv[2], "w"); // 输出结果文件
  43. else
  44. output = stdout;
  45. for (auto it = vec_one.begin(), itcp = vec_copy.begin(); it != vec_one.end(); ++it, ++itcp) {
  46. strcpy(it->end, itcp->start);
  47. fprintf(output, "::M4ACUT:: %s %s \"%s.m4a\"\n", it->start, it->end, it->name);
  48. }
  49. fprintf(output, "\n## 注意最后一行时间结束时间要手工修改 ##");
  50. return 0;
  51. }
  52. // strtrim 去掉字符串前后的空格和制表符
  53. char* strtrim(char* s)
  54. {
  55. char* p = s;
  56. while (isspace(*p))
  57. ++p;
  58. char* end = s + strlen(s) - 1;
  59. while (isspace(*end))
  60. --end;
  61. *(end + 1) = '\0';
  62. strcpy(s, p);
  63. return s;
  64. }