Clip Web videos.
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
1.8 KiB

  1. import { spawnSync } from 'child_process';
  2. import { convertDurationToSeconds, convertSecondsToDuration } from '../../duration';
  3. const normalizeStartSeconds = (start?: number | string) => {
  4. if (typeof start === 'undefined') {
  5. return 0;
  6. }
  7. if (typeof start === 'string') {
  8. return convertDurationToSeconds(start);
  9. }
  10. return start;
  11. };
  12. const FORMAT_DEFAULT_ARGS = [
  13. '--youtube-skip-dash-manifest',
  14. '-f', 'bestvideo+bestaudio',
  15. ];
  16. const constructDownloadSectionsRegex = (start?: number | string, end?: number | string) => {
  17. const startSeconds = normalizeStartSeconds(start);
  18. if (typeof end !== 'undefined') {
  19. const endSeconds = (
  20. typeof end === 'string'
  21. ? convertDurationToSeconds(end)
  22. : end
  23. );
  24. return `*${convertSecondsToDuration(startSeconds)}-${convertSecondsToDuration(endSeconds)}`;
  25. }
  26. if (startSeconds > 0) {
  27. return `*${convertSecondsToDuration(startSeconds)}-inf`;
  28. }
  29. return null;
  30. };
  31. export const getFileExtension = (downloaderExecutablePath: string, url: string) => {
  32. const result = spawnSync(
  33. downloaderExecutablePath,
  34. [
  35. ...FORMAT_DEFAULT_ARGS,
  36. '--print', 'filename',
  37. '-o', '%(ext)s',
  38. url,
  39. ],
  40. );
  41. if (result.error) {
  42. throw result.error;
  43. }
  44. return result.stdout.toString('utf-8').trim();
  45. };
  46. export const constructDefaultDownloadArgs = (
  47. outputFilename: string,
  48. url: string,
  49. start?: number | string,
  50. end?: number | string,
  51. ) => {
  52. const defaultDownloadArgs = [
  53. ...FORMAT_DEFAULT_ARGS,
  54. '-o', outputFilename,
  55. ];
  56. const downloadSectionsRegex = constructDownloadSectionsRegex(start, end);
  57. if (typeof downloadSectionsRegex === 'string') {
  58. return [
  59. ...defaultDownloadArgs,
  60. '--force-keyframes-at-cuts',
  61. '--download-sections', downloadSectionsRegex,
  62. url,
  63. ];
  64. }
  65. return [
  66. ...defaultDownloadArgs,
  67. url,
  68. ];
  69. };