“This is the third day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021.”

Ffmpeg is a powerful media file conversion tool, often used for transcoding, optional commands are very many, encoder, video duration, frame rate, resolution, pixel format, sampling format, bit rate, cropping options, channel number and so on. See Thor for more details

idea

Push streaming video to local server using NodeJS.

steps

Node-media-server is a node streaming media service. Fluent-ffmpeg is a library that can use ffmpeg command in nodeJS.

2. Import modules and create configurations

const NodeMediaServer = require("node-media-server");
const ffmpeg = require('fluent-ffmpeg');
let command = ffmpeg();

const config = {
    rtmp: {
        port: 1935,
        chunk_size: 60000,
        gop_cache: true,
        ping: 60,
        ping_timeout: 30,
    },
    http: {
        port: 8900,
        allow_origin: "*",
    },
};
Copy the code

3. Create and start the service

let nms = new NodeMediaServer(config);
nms.run();
Copy the code

4. Create an FFMPEG configuration

let options = [ "-re", "-i", url, "-vcodec", "copy", "-codec", "copy", "-f", "flv", ] let url = './2.mp4'; Exe command.outputoptions (options).setffmpegPath ("./ffmpeg.exe").save(url).on("start", function (e) { console.log("stream is start: " + e); }) .on("end", function (e) { console.log("ffmpeg is end"); }) .on("error", function (err) { console.log("ffmpeg is error! " + err); });Copy the code

5. Use the node or nodemon run this js file can be watched the flash player can use ali online players play http://127.0.0.1:8900/live/stream.flv can also download VLC player play RTMP flow directly, recommended tools: obs.

Ffmpeg can also directly push the current camera and microphone stream more please refer to the online post, with Fluent – FFmpeg can implement a video screenshot widget.

saveThumbnail() {
    ffmpeg(./2.mp4)
        .setFfmpegPath("./ffmpeg.exe")
        .noAudio()
        .setStartTime(1)
        .frames(1)
        .save("./img.png");
}
Copy the code

Fluent-ffmpeg and FFMPEG parameters are not different, please go to the official website for details. Ffmpeg is a very powerful tool that allows you to directly view and change the encoding and format of a video by adding parameters on the command line. It can be implemented as follows:

1. Get information about the video

ffmpeg -i video.avi
Copy the code

2. Combine image sequences into video


ffmpeg -f image2 -i image%d.jpg video.mpg
Copy the code

The above command will place images in the current directory (names such as image1.jpg.image2.jpg. , etc…). Merged into video. MPG

3. Break down the video into photo sequences


ffmpeg -i video.mpg image%d.jpg
Copy the code

The above command generates image1.jpg.image2.jpg….

The supported image formats are PGm.ppm.pam.pgmyuv.jpeg. GIF. PNG

Can realize too many processing video, audio function, more detailed please refer to Thor’s post.

conclusion

This is just the first taste of FFMPEG.