Python converts m3u8 format to mp4 video format, pythonm3u8

Source: Internet
Author: User

Python converts m3u8 format to mp4 video format, pythonm3u8

Open the engine: Recently, some videos have been downloaded using the mobile QQ browser, and more videos occupy the phone memory. So I want to upload the downloaded videos to my computer and save them, later, we found that all these videos are in m3u8 format and all the videos in this format are sliced. it is not convenient to view these videos on a computer, so I want to convert it to other complete videos that can be played directly. I found some tools on the Internet, which were not very useful. Later I found a "buffer merge tool" on a mobile phone ", it can be used, but the merged video sequence is messy. You need to manually adjust the video sequence of the fragments, which is too time-consuming. Therefore, you plan to write a conversion tool.

Directly run the Code: (the program file name is convert_m3u8.py)

#-*-Coding: UTF-8-*-import sys import OS from glob import glob # obtain the path def get_user_path (argv_dir) to be converted: if OS. path. isdir (argv_dir): return argv_dir elif OS. path. isabs (argv_dir): return argv_dir else: return False # Sort the converted TS files def get_sorted_ts (user_path): ts_list = glob (OS. path. join (user_path ,'*. ts ') # print (ts_list) boxer = [] for ts in ts_list: if OS. path. exists (ts): # print (OS. path. splitext (OS. Path. basename (ts) file, _ = OS. path. splitext (OS. path. basename (ts) boxer. append (int (file) boxer. sort () # print (boxer) return boxer # file merge def convert_m3u8 (boxer, o_file_name): # pai_arg = str (ts0) + "+" + str (ts1) + "" + o_file_name tmp = [] for ts in boxer: tmp. append (str (ts) + '. ts ') into _str =' + '. join (tmp) exec_str = "copy/B" + pai_str + ''+ o_file_name # print (" copy/B "+ pai_str +'' + o_file_name) OS. system (Exec_str) if _ name __= = '_ main _': # print (sys. argv [1:]) argv_len = len (sys. argv) if argv_len = 3: o_dir, o_file_name = sys. argv [1:] # print (o_dir + ":" + o_file_name) user_path = get_user_path (o_dir) # print (user_path) if not user_path: print ("the path you entered is incorrect,:-("); else: if OS. path. exists (OS. path. join (user_path, o_file_name): print ('The target file already exists and the program stops running. ') Exit (0) OS. chdir (user_path) # convert_m3u8 ('2. ts ', '4. ts ', o_file_name) boxer = get_sorted_ts (user_path) convert_m3u8 (boxer, o_file_name) # print (OS. getcwd () else: print ("the number of parameters is invalid ");

Note: There are not many codes, and the principle is very simple. It is to merge the fragments into a video file in sequence.
The usage is as follows:

Python convert_m3u8.py m3u8 video directory name of the file to be converted

The first parameter is the m3u8 video directory (the m3u8 fragment video is hidden on the mobile phone. You need to display the hidden file before exporting the file to your computer, hide the folder in front of the Android phone. the second parameter is the name of the file to be converted.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.