“This is the second day of my participation in the Gwen Challenge in November. See details: The Last Gwen Challenge in 2021”
We’re married!
Yes, it’s not a girlfriend this time, it’s a wife!
After nearly a month, my wife let me download Tencent video for her, if according to the content of the last exploration to download, it is possible to download step by step, merge, but very troublesome, programmers are not in order to solve the trouble, so troublesome steps, there is no simple point? There are! Of course, there are many simple tools. Last time, many friends recommended various tools to me, but I didn’t go through them all. I can list them, and if you need them, you can try them, and if you don’t want to try them, you can also take a look at my methods to prepare for laziness.
journey
At first, I thought about loading my previous steps in a headless browser and using the code to download the TS fragment and merge it with FFMPEG on the machine, but it still seemed to be a bit of trouble, so I went to NPM and searched for the keyword: M3U8TOMp4
m3u8-to-mp4
So I clicked on the first package: M3U8-to-MP4
What? There is only one version of this package, three years old, and many downloads per week
So I thought either this bag is really cool, solved the m3U8 to MP4 problem once and for all, so it hasn’t been updated in 3 years, or the author forgot he even had this bag
So I had this bag that no one maintained and no one updated for three years.
Use is also very simple, copy example is good. The code is as follows:
var m3u8ToMp4 = require("m3u8-to-mp4"); var converter = new m3u8ToMp4(); (async function() { var url = "https://apd-666945ea97106754c57813479384d30c.v.smtcdns.com/omts.tc.qq.com/AofRtrergNwkAhpHs4RrxH2_9DWLWSG8xjDMZDQoFGyY/ uwMROfz2r55kIaQXGdGnC2deOm68BrdPrRewQlOzrMAbixNO/svp_50001/cKAgRbCb6Re4BpHkI-IlK_KN1VJ8gQVK2sZtkHEY3vQUIlxVz7AtWmVJRifZr rPfozBS0va-SSJFhQhOFSKVNmqVi165fCQJoPl8V5QZBcGZBDpSIfrpCImJKryoZOdR5C0oGYkzIW77I4his7UkPY9Iwmf1QWjaHwNV2hpKv3aD9ysL_-YBy A/szg_9276_50001_0bc3uuaa2aaafmaff4e3ijqvdjodbwsqadka.f304110.ts.m3u8?ver=4" await converter .setInputFile(url) .setOutputFile("dummy.mp4") .start(); console.log("File converted"); }) ();Copy the code
The video is at v.qq.com/x/page/v331…
And then the video was converted. Wow!
so easy ! so beautiful!
The principle of
Out of curiosity, I’d like to see how the package is converted
So I clicked on the package m3U8-to-mp4
The package file contains the following contents
Only one file?
Then I opened index.js, only 64 lines 😂
The whole code is as follows
/** * @description to MP4 Converter * @author Furkan Inanc * @version 1.0.0 */ let ffmpeg = require("fluent-ffmpeg"); /** * A class to convert M3U8 to MP4 * @class */ class m3u8ToMp4Converter { /** * Sets the input file * @param {String} filename M3U8 file path. You can use remote URL * @returns {Function} */ setInputFile(filename) { if (! filename) throw new Error("You must specify the M3U8 file address"); this.M3U8_FILE = filename; return this; } /** * Sets the output file * @param {String} filename Output file path. Has to be local :) * @returns {Function} */ setOutputFile(filename) { if (! filename) throw new Error("You must specify the file path and name"); this.OUTPUT_FILE = filename; return this; } /** * Starts the process */ start() { return new Promise((resolve, reject) => { if (! this.M3U8_FILE || ! this.OUTPUT_FILE) { reject(new Error("You must specify the input and the output files")); return; } ffmpeg(this.M3U8_FILE) .on("error", error => { reject(new Error(error)); }) .on("end", () => { resolve(); }) .outputOptions("-c copy") .outputOptions("-bsf:a aac_adtstoasc") .output(this.OUTPUT_FILE) .run(); }); } } module.exports = m3u8ToMp4Converter;Copy the code
Check and set the input link and output file name, and then call the fluent-ffmpeg library
?????
Standing on the shoulders of giants, their own package a layer 😂
Then look at the fluent-ffmpeg package, how to achieve conversion
We then search under the package folder for the.run method to locate the specific execution place
With years of CV experience, it feels like it should be in the processor.js file, and then we open the file and navigate to the method
Looking down at the code, I noticed this code
Because are based on ffMPEG this big dad to do tools, so the bottom is also to call FFMPEG command
All of these if judgments are catching exceptions to the result, so let’s put an endpoint in the core code
It looks like several command line arguments were called
So I had a bold idea!
Yes, I manually concatenated this command on the terminal, and it should be ok to run it with my local command, so I tried
Did not expect to succeed, in fact, success is inevitable, because it is with the help of ffMPEG this package, but I am manual to operate, the framework is code to splice this command
The rest of the time, I looked at the fluent-FFmPEG code, it does a lot of things, such as to find the absolute path to ffMPEG ah, ffMPEG results to catch exception information…
subsequent
Now, here’s how you download a video for your wife
- Go to the webpage Tencent video to find m3U8 link
- Paste the URL into my Node application
- Execute the command
- Wife happy! 😄
The method mentioned by the netizen
- N_m3u8DL-CLI
- ChrmoVideo Downloader Professional plug-in.
- Downie
- IDM plug-in
- you-get
- Neat Download Management
About me
WeChat: cjs764901388
Public account: XSTxoo
My official account: Komatsu student oh
Can follow me, learn front-end knowledge together, like to use technology in life to record the place, and research 🧐