Skip to content

Commit

Permalink
feat: add custom materialFunc
Browse files Browse the repository at this point in the history
feat: add custom materialFunc
  • Loading branch information
drawcall committed Oct 25, 2024
1 parent 0c266f3 commit c979cfa
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 14 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ Since FFAIVideo relies on FFmpeg for its functionality, it is essential that you
| subtitleMaxWidth | number | - | Maximum subtitle width |
| debug | boolean | false | Debug mode |
| lastTime | number | 5 | Last time |
| materialFunc | function | null | A custom material synthesis |
| removeCache | boolean | true | Whether to remove cache |

## Reference Project
Expand Down
3 changes: 2 additions & 1 deletion examples/simple.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { generateVideo } = require('../dist');
const { generateVideo, Logger } = require('../dist');
const path = require('path');

Logger.enabled = true;
generateVideo(
{
provider: 'g4f',
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ffaivideo",
"version": "1.3.3",
"version": "1.3.7",
"description": "",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
6 changes: 6 additions & 0 deletions src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ interface VideoConfig {
lastTime?: number;
removeCache?: boolean;

materialFunc?: (
searchTerm: string,
maxClipDuration: number,
index: number,
cacheDir?: string,
) => Promise<any[]>;
insertClips?: InsertClip[];
[key: string]: any;
}
Expand Down
2 changes: 1 addition & 1 deletion src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ const generateVideo = async (
subtitleFile = '';
}
progress(40);

const downloadedVideos: string[] = await downloadVideos(
videoTerms,
videoDuration,
Expand Down
38 changes: 28 additions & 10 deletions src/material.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import fs from 'fs-extra';
import md5 from 'md5';
import path from 'path';
import axios from 'axios';
import { isEmpty, forEach } from 'lodash';
import { VideoAspect } from './config/constant';
import { VideoConfig } from './config/config';
import { toResolution } from './utils/video-aspect';
import { getEnumKeyByValue } from './utils/utils';
import { writeFileWithStream } from './utils/file';
import { appequal } from './utils/utils';
import { httpGet } from './utils/http';
import { httpGet, buildApiUrl } from './utils/http';
import { toJson } from './utils/json';
import { Logger } from './utils/log';
import { uuid } from './utils/utils';
Expand All @@ -27,13 +28,17 @@ const searchVideos = async (
const { videoAspect = VideoAspect.Portrait } = config;
const videoOrientation: string = getEnumKeyByValue(VideoAspect, videoAspect);
const [videoWidth, videoHeight] = toResolution(videoAspect);

const params = new URLSearchParams();
params.append('query', searchTerm);
params.append('per_page', '20');
params.append('orientation', videoOrientation);
const queryUrl = `https://api.pexels.com/videos/search?${params.toString()}`;
const data = await httpGet(queryUrl, {}, config.pexels!);
const searchData = {
query: searchTerm,
per_page: '20',
orientation: videoOrientation,
};
const queryUrl = `https://api.pexels.com/videos/search`;
const data = await httpGet(
buildApiUrl(queryUrl, searchData),
{},
config.pexels!,
);
if (!data) return [];

const videoItems: MaterialInfo[] = [];
Expand Down Expand Up @@ -107,8 +112,21 @@ const downloadVideos = async (
const validVideoUrls: string[] = [];
let foundDuration = 0.0;

for (const searchTerm of searchTerms) {
const videoItems = await searchVideos(searchTerm, maxClipDuration, config);
for (const [index, searchTerm] of searchTerms.entries()) {
let videoItems = [];
if (config.materialFunc) {
videoItems = await config.materialFunc(
searchTerm,
index,
maxClipDuration,
cacheDir
);
if (isEmpty(videoItems)) {
videoItems = await searchVideos(searchTerm, maxClipDuration, config);
}
} else {
videoItems = await searchVideos(searchTerm, maxClipDuration, config);
}

for (const item of videoItems) {
if (!validVideoUrls.includes(item.url)) {
Expand Down
16 changes: 15 additions & 1 deletion src/utils/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,18 @@ const httpGet = async (queryUrl: string, options = {}, site: MaterialSite) => {
return null;
};

export { httpGet };
const buildApiUrl = (baseUrl: string, data = {}) => {
const params = new URLSearchParams();

// 遍历 data 对象,将所有键值对添加到 URLSearchParams
for (const [key, value] of Object.entries(data)) {
if (value !== undefined && value !== null) {
params.append(key, value.toString());
}
}

// 如果 params 不为空,则添加 '?' 和参数字符串;否则返回原始 baseUrl
return params.toString() ? `${baseUrl}?${params.toString()}` : baseUrl;
};

export { httpGet, buildApiUrl };

0 comments on commit c979cfa

Please sign in to comment.