文章目录
- 1. 前言
- 2. Gadgets 0.1介绍
- 2.1 使用过程概述
- 3. Gadgets 0.1功能实现
- 3.1 创建文件
- 3.2 功能编程
- 3.2.1 安装
- 3.2.2 导库
- 3.2.3 定义函数并实现功能
- 3.3 主程序
- 3.3.1 向用户问好并提供指示
- 3.3.2 填入功能
- 3.4 装饰
- 加载
- 4. 源码展示
- 4.1 extract_music.py
- 4.2 loading.py
- 4.3 Gadgets0.1.py
1. 前言
大家好啊,我是落枕!
最近一直比较忙哈,没有写文章,不好意思……
我今天就和大家分享一个我自己做的项目,名字就叫小工具,Gadgets,希望大家可以喜欢
2. Gadgets 0.1介绍
我的这个小工具在第0版中只有一个功能,就是把音乐从视频里提取出来的功能
开发环境:python3.7
开发工具:pycharm64.exe
2.1 使用过程概述
用户在点开这个文件(或者说是用命令打开了这个文件,后面会细讲),随后会最后向用户确认用户是否确定需要进行下一步(此处是为了防止误触),随后会提示用户输入视频所在的路径(在下一版本中会改进),最后生成mp3文件。(后续的更改文件名过程将会在下一版本中改进)
3. Gadgets 0.1功能实现
3.1 创建文件
这一部很简单,创建一个文件即可。
(此处为iPad备份存档截图)
3.2 功能编程
我们需要用到一个第三方库,pip安装即可
3.2.1 安装
我们要用的是moviepy库,使用pip安装
pip install moviepy
一直等到安装成功即可。
3.2.2 导库
import moviepy.editor as mp
3.2.3 定义函数并实现功能
def extract_music(path):my_clip = mp.VideoFileClip(path)my_clip.audio.write_audiofile('{0}.mp3'.format(path))
最基本的功能就好了,但是在用户输入的路径有误的时候会引发OsError
,我们可以使用try-except避免程序终止
import os
import time
def extract_music(path):try:my_clip = mp.VideoFileClip(videos_file_path)my_clip.audio.write_audiofile('{0}.mp3'.format(path))os.system("cls")print("-Completed-")print("-You Can Change The File Name By Yourself")print("-We Will Further Develop This Function-")except OSError:time.sleep(1)os.system("cls")print("-There's Something Wrong-")print("-Please Check Your Path And Try Again-")
3.3 主程序
主程序的命名就用我们整个小工具的名字,Gadgets 0.1
新建一个py文件:
3.3.1 向用户问好并提供指示
我们先定义一个函数,叫做main(),(当然,各位在if __name__ ==“__main__”:
下进行程序编写也是可以的)
那么我们的问号环节就不和大家详细说了,随便写就可以,以下只是做一个参考,不喜勿喷。
import timedef main():print("-Welcome Using Gadgets 0.1!-")time.sleep(1)print("-One Service-")print("-1.Extract Music-")a = input("Be Ready To Enter The Program?\n(Y=Yes Other Key=No):")#进行最后的确认,以防用户误触,如果觉得累赘的话,可以删去a = a.upper()if a == 'Y':. . .#此处需要刚刚我们完成的功能填入else:print("-Thanks For Using-")if __name__ == "__main__":main()
3.3.2 填入功能
刚才的功能其实我们已经实现了,但是缺少人性化的体验,所以我们再一次打开刚刚的extract_music.py
,进行功能的人性化设计。
我们定义一个函数,叫做course(),(即“过程”的意思),专门用来存储用户使用过程的设计。以下依旧是仅作参考,不喜勿喷。
def course():os.system('cls')path = input("-Please Give Me The Path Which The Movie Is In-\nEnter Here:)")print("-It May Take For Some Minutes-")print("-When The System is Extracting-")print("-Let's Have A Break-")extract_music(path)
那么我们整个extract_music.py
就大功告成了,文末我会附上整个代码。
接下来进行填入
import time
from extract_music import course #这一句千万别漏掉了def main():print("-Welcome Using Gadgets 0.1!-")time.sleep(1)print("-One Service-")print("-1.Extract Music-")a = input("Be Ready To Enter The Program?\n(Y=Yes Other Key=No):")a = a.upper()if a == 'Y':course()else:print("-Thanks For Using-")if __name__ == "__main__":main()
大部分的功能就好了,我们在做一点细节,比如说加载界面。
3.4 装饰
加载
ps:这一部分并非必要,不喜欢的可以删去,觉得简陋的可以自己改一改
我们再再再再创建一个文件,叫做loading.py
里面填上下面这一段代码,就可以实现加载进度条啦!
import os
import timedef load():print("-Please Wait-")time.sleep(1)print("-We Are Loading For You-")os.system("cls")print("- |")print("Loading Now")time.sleep(1)os.system("cls")print("-- |")print("Loading Now")time.sleep(1)os.system("cls")print("--- |")print("Loading Now")time.sleep(1)os.system("cls")print("---- |")print("Loading Now")time.sleep(1)os.system("cls")print("----- |")print("Loading Now")time.sleep(1)os.system("cls")print("------ |")print("Loading Now")time.sleep(1)os.system("cls")print("------- |")print("Loading Now")time.sleep(1)os.system("cls")print("-------- |")print("Loading Now")time.sleep(1)os.system("cls")print("---------|")print("Loading Completed")time.sleep(1)os.system("cls")
对了,我们还得在主程序里加上这个函数才行。
from loading import load
from extract_music import course
import timedef main():load()time.sleep(1)print("-Welcome Using Gadgets 0.1!-")time.sleep(1)print("-One Service-")print("-1.Extract Music-")a = input("Be Ready To Enter The Program?\n(Y=Yes Other Key=No):")a = a.upper()if a == 'Y':course()else:print("-Thanks For Using-")if __name__ == "__main__":main()
大功告成!
4. 源码展示
4.1 extract_music.py
import moviepy.editor as mp
import time
import osdef extract_music(path):try:my_clip = mp.VideoFileClip(path)my_clip.audio.write_audiofile('{0}.mp3'.format(path))os.system("cls")print("-Completed-")print("-You Can Change The File Name By Yourself")print("-We Will Further Develop This Function-")except OSError:time.sleep(1)os.system("cls")print("-There's Something Wrong-")print("-Please Check Your Path And Try Again-")def course():os.system('cls')path = input("-Please Give Me The Path Which The Movie Is In-\nEnter Here:")print("-It May Take For Some Minutes-")print("-When The System is Extracting-")print("-Let's Have A Break-")extract_music(path)
4.2 loading.py
import os
import timedef load():print("-Please Wait-")time.sleep(1)print("-We Are Loading For You-")os.system("cls")print("- |")print("Loading Now")time.sleep(1)os.system("cls")print("-- |")print("Loading Now")time.sleep(1)os.system("cls")print("--- |")print("Loading Now")time.sleep(1)os.system("cls")print("---- |")print("Loading Now")time.sleep(1)os.system("cls")print("----- |")print("Loading Now")time.sleep(1)os.system("cls")print("------ |")print("Loading Now")time.sleep(1)os.system("cls")print("------- |")print("Loading Now")time.sleep(1)os.system("cls")print("-------- |")print("Loading Now")time.sleep(1)os.system("cls")print("---------|")print("Loading Completed")time.sleep(1)os.system("cls")
4.3 Gadgets0.1.py
from loading import load
from extract_music import course
import timedef main():load()time.sleep(1)print("-Welcome Using Gadgets 0.1!-")time.sleep(1)print("-One Service-")print("-1.Extract Music-")a = input("Be Ready To Enter The Program?\n(Y=Yes Other Key=No):")a = a.upper()if a == 'Y':course()else:print("-Thanks For Using-")if __name__ == "__main__":main()
邮箱:fkdlzz@163.com
创作不易,不喜勿喷,欢迎指教!
忘了说一件事情了,我也组了一个python交流群,不同的是,这个群是建在了钉钉上,欢迎加入!
群号: 44827065
进不去的话私聊找我