Practical development of unmanned live broadcast system (with source code)

Unmanned live broadcast system



Preface

Have you ever seen a live broadcast room that continuously plays video and music in a loop? Or the kind of live broadcast room that broadcasts movies live? There are also endless articles with titles similar to the following:

"How to build a 24-hour non-stop live broadcast room? Spend xxxx yuan!"
"24-hour movie live broadcast room, receive xxx~xxxx accounts every day, no sideline business, Everyone can do it!"
"The 50 yuan cloud server live streaming can make me earn more than a thousand per month? It is easy to create a passive income of my own"
"The unmanned live broadcast loops for x hours and the cumulative transaction amount is xxxx yuan"

Are you excited when you see the titles of these articles? Wait, this article is not about how to use these technologies to make money, but about exploring the principles and how these money-making technologies work.

2. Terminology

Live broadcast: Open the live broadcast software, then face the camera alone, hold a microphone, wear a miniskirt, dance gracefully, and then we can enjoy it happily.

Live streaming: A person faces the camera, holds a microphone, dances gracefully in a miniskirt, and uses third-party software to push the real-time picture to the live broadcast room, and then we can enjoy it happily.

Unattended live streaming: Record the video in advance and save it as a file, such as mp4. When you want to start broadcasting, use self-developed software to push the mp4 file to us for our happy appreciation.

What's the difference between the above? To put it simply, normal live broadcast: when we watch the live broadcast, the anchor is dancing; unattended live broadcast: when we watch the live broadcast, the anchor is actually sleeping.

3. Demand

Based on the above analysis, we develop a software that as long as we prepare the video to be broadcast:

You can broadcast it whenever you want;

Play it as many times as you want;

During the live broadcast, we can go to sleep, masturbate, date, go to work...

4. Analysis

Let’s take station B as an example to analyze how to implement the above software.

Open the broadcast settings of station B. We pay attention to the instructions in the red box below. In fact, the software we want to develop can be regarded as "third-party software". The server address and push code mentioned in the red box , are the parameters we want to use. The popular understanding is that we push the prepared video data to this server address to achieve real-time live broadcast capability.
Insert image description here
When we click "Start Live Broadcast", you can see the content in the red box in the picture below, which is the server address and password we need:

Insert image description here
How to push the flow next? It's the turn of the famous ffmpeg to come on stage. This is a powerful open source computer program that can be used to record, convert digital audio and video, and convert it into streams. The technical content of this tool is too high. I estimate that it has three or four layers to how high it is. The building is so high.

So, so far, our live streaming implementation steps have been determined:

The first step is to make the video in advance

Step 2. Install ffmpeg software

Step 3: Find our push server and secret key

Step 4: Call the ffmpeg function to push the video to the live broadcast room

Step 5: Close the refrigerator door... No, enjoy our live broadcast room

5. Coding

The above implementation plan has been determined. The coding stage is the simplest. We can use any language for coding implementation, or even use a few CMD commands or Shell commands to achieve it.

Next, I will use Java to implement it and create a "friendly" configuration interface:
Insert image description here
The page code will not be pasted. Here is the back-end code:

@RequestMapping("/live")
@ResponseBody
public Result<String> live(@RequestBody LiveConfig live){
    
    
    List<String> commands = new ArrayList<>();
    commands.add(ffmpegPath);
    commands.add("-re");
    commands.add("-stream_loop");
    commands.add(live.getLoop().toString());
    commands.add("-i");
    commands.add(baseDir+"/test.mp4");
    commands.add("-vcodec");
    commands.add("copy");
    commands.add("-acodec");
    commands.add("copy");
    commands.add("-f");
    commands.add("flv");
    commands.add(live.getUrl()+live.getSecret());
    logger.info(StringUtils.join(commands," "));
    ProcessBuilder builder = new ProcessBuilder();
    builder.redirectErrorStream(true);
    builder.command(commands);
    Thread thread = new Thread(new Runnable() {
    
    
        @Override
        public void run() {
    
    
            InputStream inputStream = null;
            try {
    
    
                Process process = builder.start();
                inputStream = process.getInputStream();
                logger.info("开始推流");
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
                String line;
                while ((line = reader.readLine()) != null) {
    
    
                    logger.info(line);
                }
                int result=process.waitFor();
                logger.info("推流结果:{}",result);
            } catch (Exception e) {
    
    
                logger.info("推流失败:{}", ExceptionUtils.getStackTrace(e));
            }finally {
    
    
                if (inputStream != null){
    
    
                    try {
    
    
                        inputStream.close();
                    } catch (IOException e) {
    
    
 
                    }
                }
            }
        }
    });
    thread.start();
    return Result.success(null);
}

My live broadcast room started broadcasting happily like this:
Insert image description here
6. Deployment
Next, deploy it to my Tencent Cloud to make my wireless Let's start broadcasting in the manned live broadcast room. It will broadcast endlessly, broadcast broadcasts on eating, sleeping, work, sports... Anyway, it is all kinds of broadcasts.

I already have a Tencent Cloud server, so I can use it directly. As a programmer, cloud servers should now be standard equipment. Students can use them to learn, newbies can use them to practice, and veterans can occasionally make some money by playing interesting things. If you want to buy a cloud server for fun, here is the direct link toTencent Cloud Discount Zone:

7. Summary

Unattended and live streaming sounds high-end, but in fact, there are only two points:

First, get the push interface

Second, load ffmpeg and start pushing

That’s it for this sharing, hurry up and start your unattended live broadcast.

Source code (free points): https://download.csdn.net/download/u012071890/88437963

Or here: https://github.com/shenmejianghu/bili-downloader

Guess you like

Origin blog.csdn.net/zyq880625/article/details/133950181