Skip to content

Instantly share code, notes, and snippets.

@asins
Last active April 17, 2019 16:33
Show Gist options
  • Select an option

  • Save asins/c2a54246ced68c0d24b021c09188a362 to your computer and use it in GitHub Desktop.

Select an option

Save asins/c2a54246ced68c0d24b021c09188a362 to your computer and use it in GitHub Desktop.

Revisions

  1. asins revised this gist Apr 17, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion main.js
    Original file line number Diff line number Diff line change
    @@ -18,7 +18,7 @@ function setVideoTags(filePath, cb) {
    '-codec', 'copy',
    '-metadata', `title=${pathParse.name}`,
    '-metadata', `album=${ALBUM}`,
    '-metadata', `composer=${AUTHOR}`,
    '-metadata', `author=${AUTHOR}`,
    path.resolve(distPath, pathParse.base)
    ]);

  2. asins created this gist Apr 17, 2019.
    66 changes: 66 additions & 0 deletions main.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,66 @@
    const fs = require('fs');
    const path = require('path');
    const { spawn } = require( 'child_process' );

    const sourcePath = path.resolve('./video/');
    const distPath = path.resolve('./new/');
    const ALBUM = '我修的可能是假仙';
    const AUTHOR = '牛大宝';

    let videoFileList;

    function setVideoTags(filePath, cb) {
    const pathParse = path.parse(filePath);

    const ls = spawn( 'ffmpeg', [
    '-i', filePath,
    '-b:a', '65k',
    '-codec', 'copy',
    '-metadata', `title=${pathParse.name}`,
    '-metadata', `album=${ALBUM}`,
    '-metadata', `composer=${AUTHOR}`,
    path.resolve(distPath, pathParse.base)
    ]);

    // ls.stdout.on( 'data', data => {
    // console.log( `stdout: ${data}` );
    // });
    //
    // ls.stderr.on( 'data', data => {
    // console.log( `stderr: ${data}` );
    // });

    ls.on( 'close', code => {
    console.log( `${filePath}, Code: ${code}` );
    cb && cb();
    });
    }

    function clearDistDir(directory) {
    fs.readdir(directory, (err, files) => {
    if (err) throw err;

    for (const file of files) {
    fs.unlink(path.join(directory, file), err => {
    if (err) throw err;
    });
    }
    });
    }
    function handleVideo(index) {
    const videoPath = videoFileList[index];
    if(!videoPath) return;
    // if(index > 2) return;

    setVideoTags(path.resolve(sourcePath, videoPath), function() {
    handleVideo(++index);
    });
    }


    // 遍历文件夹
    fs.readdir(sourcePath, function(err, items) {
    videoFileList = items;
    clearDistDir(distPath);
    handleVideo(0);
    });