├── .gitignore ├── AUTHORS ├── LICENSE ├── README.md ├── README_en.md ├── ali_audio_to_srt ├── AliOss.py ├── AliTrans.py ├── __init__.py └── __main__.py ├── assets └── Sponsor.png ├── requirements.txt ├── setup.py └── 打包 └── Pyinstaller 编译和打包.py /.gitignore: -------------------------------------------------------------------------------- 1 | # ============python 缓存相关============ 2 | __pycache__ 3 | *.spec 4 | *.pyc 5 | *.log 6 | *.spec 7 | *info 8 | 9 | # ============IDE 相关============ 10 | .idea 11 | .DS_Store 12 | 13 | # ============虚拟环境============ 14 | Lib/* 15 | Scripts/* 16 | pyvenv.cfg 17 | 18 | # ============测试代码============ 19 | */test/* 20 | 21 | # ============打包生成文件============ 22 | *.7z 23 | */dist/* 24 | */build/* 25 | build/* 26 | dist/* 27 | 28 | # ============数据库、配置文件============ 29 | *.db 30 | *.ini 31 | 32 | # ============媒体文件============ 33 | *.mp4 34 | *.mkv 35 | *.wav 36 | 37 | # ============图片============ 38 | *.afphoto 39 | icon*.png 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | Main authors: 2 | 3 | Haujet Zhao 4 | 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Mozilla Public License Version 2.0 2 | ================================== 3 | 4 | 1. Definitions 5 | -------------- 6 | 7 | 1.1. "Contributor" 8 | means each individual or legal entity that creates, contributes to 9 | the creation of, or owns Covered Software. 10 | 11 | 1.2. "Contributor Version" 12 | means the combination of the Contributions of others (if any) used 13 | by a Contributor and that particular Contributor's Contribution. 14 | 15 | 1.3. "Contribution" 16 | means Covered Software of a particular Contributor. 17 | 18 | 1.4. "Covered Software" 19 | means Source Code Form to which the initial Contributor has attached 20 | the notice in Exhibit A, the Executable Form of such Source Code 21 | Form, and Modifications of such Source Code Form, in each case 22 | including portions thereof. 23 | 24 | 1.5. "Incompatible With Secondary Licenses" 25 | means 26 | 27 | (a) that the initial Contributor has attached the notice described 28 | in Exhibit B to the Covered Software; or 29 | 30 | (b) that the Covered Software was made available under the terms of 31 | version 1.1 or earlier of the License, but not also under the 32 | terms of a Secondary License. 33 | 34 | 1.6. "Executable Form" 35 | means any form of the work other than Source Code Form. 36 | 37 | 1.7. "Larger Work" 38 | means a work that combines Covered Software with other material, in 39 | a separate file or files, that is not Covered Software. 40 | 41 | 1.8. "License" 42 | means this document. 43 | 44 | 1.9. "Licensable" 45 | means having the right to grant, to the maximum extent possible, 46 | whether at the time of the initial grant or subsequently, any and 47 | all of the rights conveyed by this License. 48 | 49 | 1.10. "Modifications" 50 | means any of the following: 51 | 52 | (a) any file in Source Code Form that results from an addition to, 53 | deletion from, or modification of the contents of Covered 54 | Software; or 55 | 56 | (b) any new file in Source Code Form that contains any Covered 57 | Software. 58 | 59 | 1.11. "Patent Claims" of a Contributor 60 | means any patent claim(s), including without limitation, method, 61 | process, and apparatus claims, in any patent Licensable by such 62 | Contributor that would be infringed, but for the grant of the 63 | License, by the making, using, selling, offering for sale, having 64 | made, import, or transfer of either its Contributions or its 65 | Contributor Version. 66 | 67 | 1.12. "Secondary License" 68 | means either the GNU General Public License, Version 2.0, the GNU 69 | Lesser General Public License, Version 2.1, the GNU Affero General 70 | Public License, Version 3.0, or any later versions of those 71 | licenses. 72 | 73 | 1.13. "Source Code Form" 74 | means the form of the work preferred for making modifications. 75 | 76 | 1.14. "You" (or "Your") 77 | means an individual or a legal entity exercising rights under this 78 | License. For legal entities, "You" includes any entity that 79 | controls, is controlled by, or is under common control with You. For 80 | purposes of this definition, "control" means (a) the power, direct 81 | or indirect, to cause the direction or management of such entity, 82 | whether by contract or otherwise, or (b) ownership of more than 83 | fifty percent (50%) of the outstanding shares or beneficial 84 | ownership of such entity. 85 | 86 | 2. License Grants and Conditions 87 | -------------------------------- 88 | 89 | 2.1. Grants 90 | 91 | Each Contributor hereby grants You a world-wide, royalty-free, 92 | non-exclusive license: 93 | 94 | (a) under intellectual property rights (other than patent or trademark) 95 | Licensable by such Contributor to use, reproduce, make available, 96 | modify, display, perform, distribute, and otherwise exploit its 97 | Contributions, either on an unmodified basis, with Modifications, or 98 | as part of a Larger Work; and 99 | 100 | (b) under Patent Claims of such Contributor to make, use, sell, offer 101 | for sale, have made, import, and otherwise transfer either its 102 | Contributions or its Contributor Version. 103 | 104 | 2.2. Effective Date 105 | 106 | The licenses granted in Section 2.1 with respect to any Contribution 107 | become effective for each Contribution on the date the Contributor first 108 | distributes such Contribution. 109 | 110 | 2.3. Limitations on Grant Scope 111 | 112 | The licenses granted in this Section 2 are the only rights granted under 113 | this License. No additional rights or licenses will be implied from the 114 | distribution or licensing of Covered Software under this License. 115 | Notwithstanding Section 2.1(b) above, no patent license is granted by a 116 | Contributor: 117 | 118 | (a) for any code that a Contributor has removed from Covered Software; 119 | or 120 | 121 | (b) for infringements caused by: (i) Your and any other third party's 122 | modifications of Covered Software, or (ii) the combination of its 123 | Contributions with other software (except as part of its Contributor 124 | Version); or 125 | 126 | (c) under Patent Claims infringed by Covered Software in the absence of 127 | its Contributions. 128 | 129 | This License does not grant any rights in the trademarks, service marks, 130 | or logos of any Contributor (except as may be necessary to comply with 131 | the notice requirements in Section 3.4). 132 | 133 | 2.4. Subsequent Licenses 134 | 135 | No Contributor makes additional grants as a result of Your choice to 136 | distribute the Covered Software under a subsequent version of this 137 | License (see Section 10.2) or under the terms of a Secondary License (if 138 | permitted under the terms of Section 3.3). 139 | 140 | 2.5. Representation 141 | 142 | Each Contributor represents that the Contributor believes its 143 | Contributions are its original creation(s) or it has sufficient rights 144 | to grant the rights to its Contributions conveyed by this License. 145 | 146 | 2.6. Fair Use 147 | 148 | This License is not intended to limit any rights You have under 149 | applicable copyright doctrines of fair use, fair dealing, or other 150 | equivalents. 151 | 152 | 2.7. Conditions 153 | 154 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted 155 | in Section 2.1. 156 | 157 | 3. Responsibilities 158 | ------------------- 159 | 160 | 3.1. Distribution of Source Form 161 | 162 | All distribution of Covered Software in Source Code Form, including any 163 | Modifications that You create or to which You contribute, must be under 164 | the terms of this License. You must inform recipients that the Source 165 | Code Form of the Covered Software is governed by the terms of this 166 | License, and how they can obtain a copy of this License. You may not 167 | attempt to alter or restrict the recipients' rights in the Source Code 168 | Form. 169 | 170 | 3.2. Distribution of Executable Form 171 | 172 | If You distribute Covered Software in Executable Form then: 173 | 174 | (a) such Covered Software must also be made available in Source Code 175 | Form, as described in Section 3.1, and You must inform recipients of 176 | the Executable Form how they can obtain a copy of such Source Code 177 | Form by reasonable means in a timely manner, at a charge no more 178 | than the cost of distribution to the recipient; and 179 | 180 | (b) You may distribute such Executable Form under the terms of this 181 | License, or sublicense it under different terms, provided that the 182 | license for the Executable Form does not attempt to limit or alter 183 | the recipients' rights in the Source Code Form under this License. 184 | 185 | 3.3. Distribution of a Larger Work 186 | 187 | You may create and distribute a Larger Work under terms of Your choice, 188 | provided that You also comply with the requirements of this License for 189 | the Covered Software. If the Larger Work is a combination of Covered 190 | Software with a work governed by one or more Secondary Licenses, and the 191 | Covered Software is not Incompatible With Secondary Licenses, this 192 | License permits You to additionally distribute such Covered Software 193 | under the terms of such Secondary License(s), so that the recipient of 194 | the Larger Work may, at their option, further distribute the Covered 195 | Software under the terms of either this License or such Secondary 196 | License(s). 197 | 198 | 3.4. Notices 199 | 200 | You may not remove or alter the substance of any license notices 201 | (including copyright notices, patent notices, disclaimers of warranty, 202 | or limitations of liability) contained within the Source Code Form of 203 | the Covered Software, except that You may alter any license notices to 204 | the extent required to remedy known factual inaccuracies. 205 | 206 | 3.5. Application of Additional Terms 207 | 208 | You may choose to offer, and to charge a fee for, warranty, support, 209 | indemnity or liability obligations to one or more recipients of Covered 210 | Software. However, You may do so only on Your own behalf, and not on 211 | behalf of any Contributor. You must make it absolutely clear that any 212 | such warranty, support, indemnity, or liability obligation is offered by 213 | You alone, and You hereby agree to indemnify every Contributor for any 214 | liability incurred by such Contributor as a result of warranty, support, 215 | indemnity or liability terms You offer. You may include additional 216 | disclaimers of warranty and limitations of liability specific to any 217 | jurisdiction. 218 | 219 | 4. Inability to Comply Due to Statute or Regulation 220 | --------------------------------------------------- 221 | 222 | If it is impossible for You to comply with any of the terms of this 223 | License with respect to some or all of the Covered Software due to 224 | statute, judicial order, or regulation then You must: (a) comply with 225 | the terms of this License to the maximum extent possible; and (b) 226 | describe the limitations and the code they affect. Such description must 227 | be placed in a text file included with all distributions of the Covered 228 | Software under this License. Except to the extent prohibited by statute 229 | or regulation, such description must be sufficiently detailed for a 230 | recipient of ordinary skill to be able to understand it. 231 | 232 | 5. Termination 233 | -------------- 234 | 235 | 5.1. The rights granted under this License will terminate automatically 236 | if You fail to comply with any of its terms. However, if You become 237 | compliant, then the rights granted under this License from a particular 238 | Contributor are reinstated (a) provisionally, unless and until such 239 | Contributor explicitly and finally terminates Your grants, and (b) on an 240 | ongoing basis, if such Contributor fails to notify You of the 241 | non-compliance by some reasonable means prior to 60 days after You have 242 | come back into compliance. Moreover, Your grants from a particular 243 | Contributor are reinstated on an ongoing basis if such Contributor 244 | notifies You of the non-compliance by some reasonable means, this is the 245 | first time You have received notice of non-compliance with this License 246 | from such Contributor, and You become compliant prior to 30 days after 247 | Your receipt of the notice. 248 | 249 | 5.2. If You initiate litigation against any entity by asserting a patent 250 | infringement claim (excluding declaratory judgment actions, 251 | counter-claims, and cross-claims) alleging that a Contributor Version 252 | directly or indirectly infringes any patent, then the rights granted to 253 | You by any and all Contributors for the Covered Software under Section 254 | 2.1 of this License shall terminate. 255 | 256 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all 257 | end user license agreements (excluding distributors and resellers) which 258 | have been validly granted by You or Your distributors under this License 259 | prior to termination shall survive termination. 260 | 261 | ************************************************************************ 262 | * * 263 | * 6. Disclaimer of Warranty * 264 | * ------------------------- * 265 | * * 266 | * Covered Software is provided under this License on an "as is" * 267 | * basis, without warranty of any kind, either expressed, implied, or * 268 | * statutory, including, without limitation, warranties that the * 269 | * Covered Software is free of defects, merchantable, fit for a * 270 | * particular purpose or non-infringing. The entire risk as to the * 271 | * quality and performance of the Covered Software is with You. * 272 | * Should any Covered Software prove defective in any respect, You * 273 | * (not any Contributor) assume the cost of any necessary servicing, * 274 | * repair, or correction. This disclaimer of warranty constitutes an * 275 | * essential part of this License. No use of any Covered Software is * 276 | * authorized under this License except under this disclaimer. * 277 | * * 278 | ************************************************************************ 279 | 280 | ************************************************************************ 281 | * * 282 | * 7. Limitation of Liability * 283 | * -------------------------- * 284 | * * 285 | * Under no circumstances and under no legal theory, whether tort * 286 | * (including negligence), contract, or otherwise, shall any * 287 | * Contributor, or anyone who distributes Covered Software as * 288 | * permitted above, be liable to You for any direct, indirect, * 289 | * special, incidental, or consequential damages of any character * 290 | * including, without limitation, damages for lost profits, loss of * 291 | * goodwill, work stoppage, computer failure or malfunction, or any * 292 | * and all other commercial damages or losses, even if such party * 293 | * shall have been informed of the possibility of such damages. This * 294 | * limitation of liability shall not apply to liability for death or * 295 | * personal injury resulting from such party's negligence to the * 296 | * extent applicable law prohibits such limitation. Some * 297 | * jurisdictions do not allow the exclusion or limitation of * 298 | * incidental or consequential damages, so this exclusion and * 299 | * limitation may not apply to You. * 300 | * * 301 | ************************************************************************ 302 | 303 | 8. Litigation 304 | ------------- 305 | 306 | Any litigation relating to this License may be brought only in the 307 | courts of a jurisdiction where the defendant maintains its principal 308 | place of business and such litigation shall be governed by laws of that 309 | jurisdiction, without reference to its conflict-of-law provisions. 310 | Nothing in this Section shall prevent a party's ability to bring 311 | cross-claims or counter-claims. 312 | 313 | 9. Miscellaneous 314 | ---------------- 315 | 316 | This License represents the complete agreement concerning the subject 317 | matter hereof. If any provision of this License is held to be 318 | unenforceable, such provision shall be reformed only to the extent 319 | necessary to make it enforceable. Any law or regulation which provides 320 | that the language of a contract shall be construed against the drafter 321 | shall not be used to construe this License against a Contributor. 322 | 323 | 10. Versions of the License 324 | --------------------------- 325 | 326 | 10.1. New Versions 327 | 328 | Mozilla Foundation is the license steward. Except as provided in Section 329 | 10.3, no one other than the license steward has the right to modify or 330 | publish new versions of this License. Each version will be given a 331 | distinguishing version number. 332 | 333 | 10.2. Effect of New Versions 334 | 335 | You may distribute the Covered Software under the terms of the version 336 | of the License under which You originally received the Covered Software, 337 | or under the terms of any subsequent version published by the license 338 | steward. 339 | 340 | 10.3. Modified Versions 341 | 342 | If you create software not governed by this License, and you want to 343 | create a new license for such software, you may create and use a 344 | modified version of this License if you rename the license and remove 345 | any references to the name of the license steward (except to note that 346 | such modified license differs from this License). 347 | 348 | 10.4. Distributing Source Code Form that is Incompatible With Secondary 349 | Licenses 350 | 351 | If You choose to distribute Source Code Form that is Incompatible With 352 | Secondary Licenses under the terms of this version of the License, the 353 | notice described in Exhibit B of this License must be attached. 354 | 355 | Exhibit A - Source Code Form License Notice 356 | ------------------------------------------- 357 | 358 | This Source Code Form is subject to the terms of the Mozilla Public 359 | License, v. 2.0. If a copy of the MPL was not distributed with this 360 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 361 | 362 | If it is not possible or desirable to put the notice in a particular 363 | file, then You may include the notice in a location (such as a LICENSE 364 | file in a relevant directory) where a recipient would be likely to look 365 | for such a notice. 366 | 367 | You may add additional accurate notices of copyright ownership. 368 | 369 | Exhibit B - "Incompatible With Secondary Licenses" Notice 370 | --------------------------------------------------------- 371 | 372 | This Source Code Form is "Incompatible With Secondary Licenses", as 373 | defined by the Mozilla Public License, v. 2.0. 374 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [中文](./README.md) | [English](./README_en.md) 2 | 3 | [Gitee](https://gitee.com/haujet/ali-audio-to-srt.git) | [Github](https://github.com/HaujetZhao/ali-audio-to-srt) 4 | 5 | # 阿里云音频转字幕 6 | 7 | ## ⭐ 简介 8 | 9 | 功能: 使用阿里云智能语音服务中的录音文件识别 API,实现将视频、音频文件转写出 srt 字幕 10 | 11 | 下载: 12 | 13 | - 发行版下载地址: [Releases](../../releases) 14 | 15 | ## 📝 背景 16 | 17 | QuickCut 上的转字幕效果不好,有的句子太长,需要优化,但没时间做 GUI,所以做了这个命令行工具。 18 | 19 | 使用后,会自动生成 `config.ini` 配置文件,请根据提示,在配置文件里填写上阿里云 API 相关参数。 20 | 21 | 阿里云 api 的获取,可以参考这个 [视频教程](https://www.bilibili.com/video/BV18T4y1E7FF?p=11) 22 | ## ✨ 特性 23 | 24 | 两种运行方式: 25 | 26 | * 直接运行,文字引导 27 | * 命令行运行 28 | 29 | ## 🛠️ 安装 30 | 31 | ### 📦 发行版 32 | 33 | 需要提前安装上 FFmpeg 34 | 35 | 已为 Windows 64 位打包成可直接双击运行的包,请到 [本仓库的 Releases](../../releases) 界面下载。将 7z 压缩包解压后,文件夹内有一个 exe 文件,双击即可运行。 36 | 37 | 也可以从命令行运行: 38 | 39 | ``` 40 | ali-audio-to-srt 41 | ``` 42 | 43 | 我没有其他系统(例如 Linux、MacOS)的电脑,所以无法为其他系统打包,这些系统的用户需要从源代码或 pip 安装使用。(参见下文) 44 | 45 | ### ⚙️ 用 pip 安装运行 46 | 47 | 需要提前安装上 FFmpeg 和 Python3 48 | 49 | #### 用 pip 从 pypi 安装 50 | 51 | 还未上传 52 | 53 | #### 用 pip 从源代码安装 54 | 55 | 将仓库克隆下来,进入仓库文件夹,运行: 56 | 57 | ``` 58 | pip install . 59 | ``` 60 | 61 | 就安装上了。然后就可以运行以下命令使用了: 62 | 63 | ``` 64 | ali-audio-to-srt 65 | ``` 66 | 67 | ### 📄 从源代码直接运行 68 | 69 | 将仓库克隆下来,进入仓库文件夹,先安装依赖库: 70 | 71 | ``` 72 | pip install -r requirements.txt 73 | ``` 74 | 75 | 然后就可以以模块的方式运行: 76 | 77 | ``` 78 | python -m ali_audio_to_srt 79 | ``` 80 | 81 | ## 💡 使用 82 | 83 | 注意:识别的音频或视频文件时长不能超过 4 个半小时。 84 | 85 | ``` 86 | python -m ali_audio_to_srt 87 | ``` 88 | 89 | ```shell 90 | python -m ali_audio_to_srt 音频1.mp3 视频2.mkv 91 | ``` 92 | 93 | 第一种方式是直接运行,会有文字提示引导你: 94 | 95 | ``` 96 | > python -m ali_audio_to_srt 97 | 98 | ``` 99 | 100 | 第二种方式是命令行传递参数运行: 101 | 102 | ``` 103 | > python -m ali_audio_to_srt -h 104 | usage: __main__.py [-h] [--version] [-l 语言] Media [Media ...] 105 | 106 | 功能:使用阿里云的录音文件识别服务将视频或音频文件生成 SRT 字幕文件 107 | 108 | positional arguments: 109 | Media 可一次识别多个文件 110 | 111 | optional arguments: 112 | -h, --help show this help message and exit 113 | --version show program's version number and exit 114 | -l 语言, --language 语言 使用什么引擎,默认是配置文件中的第一个 (default: ) 115 | ``` 116 | 117 | ## 🔋 打赏 118 | 119 | 本软件完全开源,用爱发电,如果你愿意,可以以打赏的方式为我充电: 120 | 121 | ![sponsor](assets/Sponsor.png) 122 | 123 | ## 😀 交流 124 | 125 | 如果有软件方面的反馈可以提交 issues,或者加入 [QQ 群:1146626791](https://qm.qq.com/cgi-bin/qm/qr?k=DgiFh5cclAElnELH4mOxqWUBxReyEVpm&jump_from=webapi) -------------------------------------------------------------------------------- /README_en.md: -------------------------------------------------------------------------------- 1 | [中文](./README.md) | [English](./README_en.md) 2 | 3 | [Gitee](https://gitee.com/haujet/audio-video-resync) | [Github](https://github.com/HaujetZhao/audio-video-resync) 4 | 5 | # Purpose 6 | 7 | Not written 8 | 9 | # Introduce: 10 | 11 | Not written -------------------------------------------------------------------------------- /ali_audio_to_srt/AliOss.py: -------------------------------------------------------------------------------- 1 | import oss2, urllib 2 | 3 | 4 | class AliOss(): 5 | def __init__(self): 6 | pass 7 | 8 | def auth(self, bucketName, endpointDomain, accessKeyId, accessKeySecret): 9 | self.bucketName = bucketName 10 | self.endpointDomain = endpointDomain 11 | self.accessKeyId = accessKeyId 12 | self.accessKeySecret = accessKeySecret 13 | self.auth = oss2.Auth(self.accessKeyId, self.accessKeySecret) 14 | self.bucket = oss2.Bucket(self.auth, self.endpointDomain, self.bucketName) 15 | 16 | def create(self): 17 | # 这面这行用于创建,并设置存储空间为私有读写权限。 18 | self.bucket.create_bucket(oss2.models.BUCKET_ACL_PRIVATE) 19 | 20 | def upload(self, source, destination): 21 | # 这个是上传文件到 oss 22 | # destination 上传文件到OSS时需要指定包含文件后缀在内的完整路径,例如abc/efg/123.jpg。 23 | # source 由本地文件路径加文件名包括后缀组成,例如/users/local/myfile.txt。 24 | # 要返回远程文件的链接 25 | self.bucket.put_object_from_file(destination, source) 26 | remoteLink = r'https://' + urllib.parse.quote( 27 | '%s.%s/%s' % (self.bucketName, self.endpointDomain, destination)) 28 | return remoteLink 29 | 30 | def download(self, source, destination): 31 | # 以下代码用于将指定的OSS文件下载到本地文件: 32 | # source 从OSS下载文件时需要指定包含文件后缀在内的完整路径,例如abc/efg/123.jpg。 33 | # destination由本地文件路径加文件名包括后缀组成,例如/users/local/myfile.txt。 34 | self.bucket.get_object_to_file(source, destination) 35 | 36 | def delete(self, cloudFile): 37 | # cloudFile 表示删除OSS文件时需要指定包含文件后缀在内的完整路径,例如abc/efg/123.jpg。string 格式哦 38 | self.bucket.delete_object(cloudFile) 39 | -------------------------------------------------------------------------------- /ali_audio_to_srt/AliTrans.py: -------------------------------------------------------------------------------- 1 | import os, subprocess, time, json, srt, datetime, re 2 | 3 | from pprint import pprint 4 | 5 | from aliyunsdkcore.acs_exception.exceptions import ClientException 6 | from aliyunsdkcore.acs_exception.exceptions import ServerException 7 | from aliyunsdkcore.client import AcsClient 8 | from aliyunsdkcore.request import CommonRequest 9 | 10 | from icecream import ic 11 | 12 | class AliTrans(): 13 | def __init__(self, appKey, language, accessKeyId, accessKeySecret): 14 | self.appKey = appKey 15 | # 地域ID,常量内容,请勿改变 16 | self.REGION_ID = "cn-shanghai" 17 | self.PRODUCT = "nls-filetrans" 18 | self.DOMAIN = "filetrans.cn-shanghai.aliyuncs.com" 19 | self.API_VERSION = "2018-08-17" 20 | self.POST_REQUEST_ACTION = "SubmitTask" 21 | self.GET_REQUEST_ACTION = "GetTaskResult" 22 | # 请求参数key 23 | self.KEY_APP_KEY = "appkey" 24 | self.KEY_FILE_LINK = "file_link" 25 | self.KEY_VERSION = "version" 26 | self.KEY_ENABLE_WORDS = "enable_words" 27 | # 是否开启智能分轨 28 | self.KEY_AUTO_SPLIT = "auto_split" 29 | # 响应参数key 30 | self.KEY_TASK = "Task" 31 | self.KEY_TASK_ID = "TaskId" 32 | self.KEY_STATUS_TEXT = "StatusText" 33 | self.KEY_RESULT = "Result" 34 | # 状态值 35 | self.STATUS_SUCCESS = "SUCCESS" 36 | self.STATUS_RUNNING = "RUNNING" 37 | self.STATUS_QUEUEING = "QUEUEING" 38 | # 创建AcsClient实例 39 | self.client = AcsClient(accessKeyId, accessKeySecret, self.REGION_ID) 40 | 41 | self.查询请求 = '' 42 | 43 | pass 44 | 45 | def 提交任务(self, 文件链接): 46 | # 提交录音文件识别请求 47 | postRequest = CommonRequest() 48 | postRequest.set_domain(self.DOMAIN) 49 | postRequest.set_version(self.API_VERSION) 50 | postRequest.set_product(self.PRODUCT) 51 | postRequest.set_action_name(self.POST_REQUEST_ACTION) 52 | postRequest.set_method('POST') 53 | # 新接入请使用4.0版本,已接入(默认2.0)如需维持现状,请注释掉该参数设置 54 | # 设置是否输出词信息,默认为false,开启时需要设置version为4.0 55 | task = {self.KEY_APP_KEY: self.appKey, 56 | self.KEY_FILE_LINK: 文件链接, 57 | self.KEY_VERSION: "4.0", 58 | self.KEY_ENABLE_WORDS: True, 59 | 'max_single_segment_time': 10000} 60 | # 开启智能分轨,如果开启智能分轨 task中设置KEY_AUTO_SPLIT : True 61 | # task = {KEY_APP_KEY : appKey, KEY_FILE_LINK : fileLink, KEY_VERSION : "4.0", KEY_ENABLE_WORDS : False, KEY_AUTO_SPLIT : True} 62 | task = json.dumps(task) 63 | # print(task) 64 | postRequest.add_body_params(self.KEY_TASK, task) 65 | 任务回执 = "" 66 | try: 67 | postResponse = self.client.do_action_with_exception(postRequest) 68 | postResponse = json.loads(postResponse) 69 | print(f'postResponse: {postResponse}') 70 | 71 | statusText = postResponse[self.KEY_STATUS_TEXT] 72 | 73 | if statusText == self.STATUS_SUCCESS: 74 | self.任务回执 = postResponse[self.KEY_TASK_ID] 75 | print(f'录音文件识别请求成功响应!\n statusText: {statusText}\n keyTaskId: {self.任务回执}') 76 | return True 77 | elif statusText == 'USER_BIZDURATION_QUOTA_EXCEED': 78 | print(f'你今天的阿里云识别额度已用完!\n statusText: {statusText}') 79 | return False 80 | else: 81 | print( 82 | f'录音文件识别请求失败,失败原因是:{statusText},你可以将这个代码复制,到 “https://help.aliyun.com/document_detail/90727.html” 查询具体原因\n') 83 | return False 84 | except Exception as e: 85 | print(f'错误信息:\n {e}') 86 | return False 87 | 88 | def 查询任务详情(self): 89 | if not self.查询请求: 90 | # 创建CommonRequest,设置任务ID 91 | self.查询请求 = CommonRequest() 92 | self.查询请求.set_domain(self.DOMAIN) 93 | self.查询请求.set_version(self.API_VERSION) 94 | self.查询请求.set_product(self.PRODUCT) 95 | self.查询请求.set_action_name(self.GET_REQUEST_ACTION) 96 | self.查询请求.set_method('GET') 97 | self.查询请求.add_query_param(self.KEY_TASK_ID, self.任务回执) 98 | # 提交录音文件识别结果查询请求 99 | # 以轮询的方式进行识别结果的查询,直到服务端返回的状态描述符为"SUCCESS"、"SUCCESS_WITH_NO_VALID_FRAGMENT", 100 | # 或者为错误描述,则结束轮询。 101 | try: 102 | 任务详情 = self.client.do_action_with_exception(self.查询请求) 103 | self.任务详情 = json.loads(任务详情) 104 | except Exception as e: 105 | print(e) 106 | return False 107 | return True 108 | 109 | def 轮询任务(self): 110 | while True: 111 | 成功查询 = 查询任务详情() 112 | if not 成功查询: break 113 | statusText = self.任务详情[KEY_STATUS_TEXT] 114 | if statusText == STATUS_RUNNING or statusText == STATUS_QUEUEING: 115 | # 继续轮询 116 | if statusText == STATUS_QUEUEING: 117 | print(f'任务 {任务回执} 正在排队中\n') 118 | elif statusText == STATUS_RUNNING: 119 | print(f'任务 {任务回执} 正在音频转文字中\n') 120 | time.sleep(5) 121 | else: 122 | break 123 | return 成功查询 124 | 125 | def 结果转srt(self): 126 | 127 | def 将本句字幕添加到列表(): 128 | 开始秒数 = 开始时间 // 1000 129 | 开始毫秒数 = 开始时间 % 1000 * 1000 130 | 结束秒数 = 结束时间 // 1000 131 | 结束毫秒数 = 结束时间 % 1000 * 1000 132 | 133 | # 设定字幕起始时间 134 | if 开始秒数 == 0: 135 | srt开始时间 = datetime.timedelta(microseconds=开始毫秒数) 136 | else: 137 | srt开始时间 = datetime.timedelta(seconds=开始秒数, microseconds=开始毫秒数) 138 | 139 | # 设定字幕终止时间 140 | if 结束秒数 == 0: 141 | srt结束时间 = datetime.timedelta(microseconds=结束毫秒数) 142 | else: 143 | srt结束时间 = datetime.timedelta(seconds=结束秒数, microseconds=结束毫秒数) 144 | 145 | 字幕列表.append(srt.Subtitle(index=i, start=srt开始时间, end=srt结束时间, content=本句字幕内容)) 146 | # print(f'任务详情:') 147 | 148 | # pprint(self.任务详情) 149 | 150 | # 新建一个列表,用于存放字幕 151 | 152 | 字幕全部文本 = '' 153 | for i in range(len(self.任务详情['Result']['Sentences'])): 154 | 字幕全部文本 = 字幕全部文本 + self.任务详情['Result']['Sentences'][i]['Text'] 155 | print(f'字幕文本:\n{字幕全部文本}') 156 | 157 | 单词合并 = '' 158 | for i in range(len(self.任务详情['Result']['Words'])): 159 | 单词合并 = 单词合并 + self.任务详情['Result']['Words'][i]['Word'] 160 | print(f'单词合并:\n{单词合并}') 161 | 162 | 字幕列表 = [] 163 | 词语数量 = len(self.任务详情['Result']['Words']) 164 | for i in range(词语数量): 165 | if i > 0: 166 | lastEndTime = EndTime 167 | Word = self.任务详情['Result']['Words'][i]['Word'] 168 | BeginTime = self.任务详情['Result']['Words'][i]['BeginTime'] 169 | EndTime = self.任务详情['Result']['Words'][i]['EndTime'] 170 | 171 | # 如果是英文,它在句子中有空格,但这个 word 就没有,因此要给它补上 172 | # 不然会变成一个单词输出一个句子 173 | if 字幕全部文本[0] == ' ': 174 | Word = ' ' + Word 175 | 176 | if i == 0: 177 | 开始时间 = BeginTime 178 | 本句字幕内容 = Word 179 | 字幕全部文本 = 字幕全部文本[len(Word):] 180 | continue 181 | 182 | if 字幕全部文本[0] == Word[0]: 183 | 本句字幕内容 += Word 184 | 字幕全部文本 = 字幕全部文本[len(Word):] 185 | if i + 1 == 词语数量: 186 | 结束时间 = EndTime 187 | 将本句字幕添加到列表() 188 | 189 | else: 190 | if Word not in 字幕全部文本: 191 | continue 192 | 193 | 结束时间 = lastEndTime if i + 1 < 词语数量 else EndTime 194 | 195 | 将本句字幕添加到列表() 196 | 197 | 本句字幕内容 = Word 198 | 开始时间 = BeginTime 199 | 删除次数 = 0 200 | while 字幕全部文本[0] != Word[0] and len(字幕全部文本) > len(Word): 201 | 字幕全部文本 = 字幕全部文本[1:] 202 | 删除次数 += 1 203 | 字幕全部文本 = 字幕全部文本[len(Word):] 204 | 205 | # 以防有 bug,在这里中断: 206 | # if 删除次数 > 5: 207 | # print('出问题了,返回的词和句子内容不同!') 208 | # break 209 | 210 | 字幕内容列表 = [] 211 | for i in 字幕列表: 212 | 字幕内容列表.append(i.content) 213 | print(f'优化后的字幕内容合并:') 214 | pprint(字幕列表) 215 | pprint(字幕内容列表) 216 | 217 | 218 | return srt.compose(字幕列表, reindex=True, start_index=1, strict=True) 219 | -------------------------------------------------------------------------------- /ali_audio_to_srt/__init__.py: -------------------------------------------------------------------------------- 1 | __all__ = ['AliOss', 'AliTrans'] 2 | -------------------------------------------------------------------------------- /ali_audio_to_srt/__main__.py: -------------------------------------------------------------------------------- 1 | # This Source Code Form is subject to the terms of the Mozilla Public 2 | # License, v. 2.0. If a copy of the MPL was not distributed with this 3 | # file, You can obtain one at https://mozilla.org/MPL/2.0/. 4 | # 5 | # Copyright (c) 2021 Haujet Zhao 6 | 7 | # 内存分析: 8 | # @profile 9 | # python -m memory_profiler __main__.py 10 | 11 | import argparse 12 | import os 13 | import shlex 14 | import subprocess 15 | import sys 16 | import configparser 17 | import platform 18 | import threading 19 | import time 20 | import srt 21 | from pathlib import Path 22 | from icecream import ic 23 | from threading import Thread 24 | 25 | from .AliOss import AliOss 26 | from .AliTrans import AliTrans 27 | 28 | # 这里从相对路径导入,在被 pyinstaller 打包时,需要换成绝对路径 29 | # from .moduel import * 30 | 31 | 32 | 33 | def main(): 34 | 配置文件 = Path(Path(__file__).absolute().parent) / 'config.ini' 35 | config = 检查配置文件(配置文件) 36 | 37 | 不马上退出 = False 38 | if len(sys.argv) == 1: 39 | 不马上退出 = True 40 | 41 | print(f''' 42 | 你没有输入任何文件,因此进入文字引导。 43 | 程序的用处主要是使用阿里云的录音文件识别服务 44 | 将视频或音频文件生成 SRT 字幕文件 45 | ''') 46 | print(f'\n请输入要处理的视频或音频文件') 47 | sys.argv.append(得到输入文件()) 48 | 49 | print(f'\n所使用的配置文件路径:{配置文件}') 50 | print(f'可选的引擎有:') 51 | for i, 引擎名 in enumerate(config.sections()): 52 | print(f'{i + 1} {config.sections()[i]}') 53 | 用户选择序号 = 得到整数('默认选择第一个引擎,如果有其它选择,请输入要选择的序号:', 54 | 1, 1, len(config.sections())) 55 | 引擎序号 = 用户选择序号 - 1 56 | sys.argv.insert(1, f'{config.sections()[引擎序号]}') 57 | sys.argv.insert(1, '-l') 58 | 59 | parser = argparse.ArgumentParser( 60 | description='''功能:使用阿里云的录音文件识别服务将视频或音频文件生成 SRT 字幕文件''', 61 | formatter_class=argparse.ArgumentDefaultsHelpFormatter 62 | ) 63 | parser.add_argument('Media', nargs='+', type=str, help='可一次识别多个文件') 64 | 65 | parser.add_argument('--version', action='version', version='%(prog)s 1.0') 66 | parser.add_argument('-l', '--language', metavar='语言', type=str, default='', help='使用什么引擎,默认是配置文件中的第一个') 67 | 68 | args = parser.parse_args() 69 | 70 | if args.language == '': 71 | 引擎 = config[config.sections()[0]] 72 | elif args.language not in config.sections(): 73 | print(f'无法在配置文件中找到所指定语言 {args.language} 的 Api ,已配置的语言有:{config.sections()},使用默认语言:{config.sections()[0]}') 74 | 引擎 = config[config.sections()[0]] 75 | else: 76 | 引擎 = config[args.language] 77 | print(f'所使用的配置文件路径:{配置文件}') 78 | print(f'\n使用引擎:{引擎.name}') 79 | 80 | # 检查引擎项是否有空项,如果有,就请用户重新填 81 | if not all(list(map(lambda x: x[1], list(引擎.items())))): 82 | print(f'\n检测到配置文件中的引擎有空项,请先在配置文件中将引擎信息填好,再重新运行') 83 | print(f'阿里云 api 的获取,可以参考这个视频教程:https://www.bilibili.com/video/BV18T4y1E7FF?p=11') 84 | if 不马上退出: input(f'\n按下回车结束程序') 85 | sys.exit() 86 | 87 | # if not any(list(引擎.items())) 88 | 89 | 处理文件(args.Media, 引擎=引擎) 90 | 91 | if 不马上退出: 92 | input('\n所有任务处理完毕,按下回车结束程序') 93 | else: 94 | print('\n所有任务处理完毕') 95 | 96 | def 检查配置文件(配置文件): 97 | config = configparser.ConfigParser() 98 | config.read(配置文件, encoding='utf-8') 99 | while not config.sections(): 100 | with open(配置文件, 'w', encoding='utf-8') as f: 101 | config = configparser.ConfigParser() 102 | for section in ['中文', '英语']: 103 | config[section] = {} 104 | items = ['ali_Oss_Bucket_Name', 105 | 'ali_Oss_Endpoint_Domain', 106 | 'ali_Oss_Access_Key_Id', 107 | 'ali_Oss_Access_Key_Secret', 108 | 'ali_Api_App_Key', 109 | 'ali_Api_Access_Key_Id', 110 | 'ali_Api_Access_Key_Secret', 111 | ] 112 | for item in items: 113 | config[section][item] = '' 114 | config.write(f) 115 | print(f'\n检测到还没有配置文件,已生成配置模板,请先将配置文件填好,再进行识别\n配置文件路径:{配置文件}') 116 | print(f'阿里云 api 的获取,可以参考这个视频教程:https://www.bilibili.com/video/BV18T4y1E7FF?p=11') 117 | if platform.system() == 'Windows': 118 | os.system(f'explorer /select, "{配置文件}"') 119 | input('按回车继续') 120 | config.read(配置文件, encoding='utf-8') 121 | return config 122 | 123 | def 得到输入文件(): 124 | while True: 125 | 用户输入 = input(f'请输入文件路径 或 直接拖入:') 126 | if 用户输入 == '': 127 | continue 128 | if os.path.exists(用户输入.strip('\'"')): 129 | 输入文件 = 用户输入.strip('\'"') 130 | break 131 | else: 132 | print('输入的文件不存在,请重新输入') 133 | return 输入文件 134 | 135 | def 处理文件(files, 引擎): 136 | 删除oss文件 = True 137 | 线程列表 = [] 138 | 线程数 = 16 139 | files = list(filter(lambda x: os.path.exists(x), files)) 140 | for index, file in enumerate(files): 141 | while len(threading.enumerate()) >= 线程数: 142 | time.sleep(1) 143 | print(f'\n总共有 {len(files)} 个文件需要识别,正在转码上传第 {index + 1} 个:{file}') 144 | # 生成 wav 145 | wav路径 = f'{os.path.splitext(file)[0]}_16000hz.wav' 146 | 命令 = f'ffmpeg -y -hide_banner -i "{file}" -ac 1 -ar 16000 "{wav路径}"' 147 | subprocess.run(shlex.split(命令), stderr=subprocess.DEVNULL, stdout=subprocess.DEVNULL) 148 | print(f'转码完成') 149 | 150 | # oss 初始化 151 | oss = 得到oss(ali_Oss_Bucket_Name = 引擎['ali_Oss_Bucket_Name'], 152 | ali_Oss_Endpoint_Domain = 引擎['ali_Oss_Endpoint_Domain'], 153 | ali_Oss_Access_Key_Id = 引擎['ali_Oss_Access_Key_Id'], 154 | ali_Oss_Access_Key_Secret = 引擎['ali_Oss_Access_Key_Secret']) 155 | if not oss: return False 156 | 157 | # 上传 wav,得到 远程链接 158 | oss文件路径, 文件url链接 = 上传oss(wav路径, oss) 159 | print(f'上传完成,文件链接:{文件url链接}') 160 | 161 | # 删除本地 wav 文件 162 | os.remove(wav路径) 163 | 164 | # 初始化识别引擎 165 | 识别引擎 = AliTrans(appKey=引擎['ali_Api_App_Key'], 166 | language='', 167 | accessKeyId=引擎['ali_Api_Access_Key_Id'], 168 | accessKeySecret=引擎['ali_Api_Access_Key_Secret']) 169 | 170 | # 提交任务 171 | 识别引擎.提交任务(文件url链接) 172 | print(f'第 {index + 1} 个文件 {file} 识别任务已提交') 173 | 174 | # 用新线程等待识别完成 175 | 线程 = Wait_For_Response_To_Generate_Srt(file, 识别引擎, oss, oss文件路径, 文件url链接, 删除oss文件) 176 | 线程列表.append(线程) 177 | 线程.start() 178 | 179 | # 定时检测线程列表的状态 180 | print(f'\n所有任务线程已提交,等待识别完成') 181 | 有线程在工作 = True 182 | lastTime = time.time() 183 | while 有线程在工作: 184 | thisTime = time.time() 185 | if thisTime - lastTime > 5: 186 | lastTime = thisTime 187 | print(f'\n各线程状况:') 188 | for index, 线程 in enumerate(线程列表): 189 | print(f' {index}: {线程.状态} ({线程.文件})') 190 | ... 191 | if all(list(map(lambda x: not x.is_alive(), 线程列表))): 有线程在工作 = False 192 | time.sleep(0.2) 193 | 194 | 195 | def 得到oss(ali_Oss_Bucket_Name, 196 | ali_Oss_Endpoint_Domain, 197 | ali_Oss_Access_Key_Id, 198 | ali_Oss_Access_Key_Secret): 199 | oss = AliOss() 200 | try: 201 | oss.auth(bucketName=ali_Oss_Bucket_Name, 202 | endpointDomain=ali_Oss_Endpoint_Domain, 203 | accessKeyId=ali_Oss_Access_Key_Id, 204 | accessKeySecret=ali_Oss_Access_Key_Secret) 205 | except Exception as e: 206 | print(f'oss验证出错:{e}\n请检查配置文件') 207 | return False 208 | return oss 209 | 210 | def 上传oss(file, oss): 211 | # 确定当前日期 212 | localTime = time.localtime(time.time()) 213 | year = localTime.tm_year 214 | month = localTime.tm_mon 215 | day = localTime.tm_mday 216 | 217 | 文件名 = os.path.basename(file) 218 | 219 | 上传目标路径 = f'{year}/{month}/{day}/{文件名}' 220 | 221 | # 上传音频文件 upload audio to cloud 222 | print(f'上传音频中\n 本地文件:{file}\n 目标路径:{上传目标路径}') 223 | 文件url链接 = oss.upload(file, 上传目标路径) 224 | 225 | return 上传目标路径, 文件url链接 226 | 227 | def 得到整数(提示语, 默认值: int, 最小值: int, 最大值: int): 228 | while True: 229 | 数值 = input(提示语 + f'\n (默认值:{默认值} 有效数值:{最小值} ~ {最大值})\n') 230 | if 数值 == '': 231 | return 默认值 232 | try: 233 | 数值 = int(数值) 234 | except: 235 | print('您的输入不是有效数字,请重新输入') 236 | continue 237 | if 数值 < 最小值 or 数值 > 最大值: 238 | print('您输入的值不在有效范围内,请重新输入') 239 | continue 240 | break 241 | return 数值 242 | 243 | class Wait_For_Response_To_Generate_Srt(Thread): 244 | def __init__(self, 文件, 识别引擎, oss, oss文件路径, 文件url链接, 删除oss文件): 245 | super().__init__() 246 | self.文件 = 文件 247 | self.识别引擎 = 识别引擎 248 | self.oss = oss 249 | self.oss文件路径 = oss文件路径 250 | self.文件url链接 = 文件url链接 251 | self.删除oss文件 = 删除oss文件 252 | self.状态 = '排队中' 253 | 254 | def 轮询(self): 255 | while True: 256 | 成功查询 = self.识别引擎.查询任务详情() 257 | if not 成功查询: break 258 | statusText = self.识别引擎.任务详情[self.识别引擎.KEY_STATUS_TEXT] 259 | if statusText == self.识别引擎.STATUS_RUNNING or statusText == self.识别引擎.STATUS_QUEUEING: 260 | # 继续轮询 261 | if statusText == self.识别引擎.STATUS_QUEUEING: 262 | self.状态 = '排队中' 263 | elif statusText == self.识别引擎.STATUS_RUNNING: 264 | self.状态 = '音频转文字中' 265 | time.sleep(3) 266 | else: 267 | self.状态 = '转换完成' 268 | break 269 | 270 | def run(self): 271 | 轮询成功 = self.轮询() 272 | 273 | print(f'\n删除 oss 远端文件:{self.oss文件路径}') 274 | self.oss.delete(self.oss文件路径) 275 | 276 | 任务详情 = self.识别引擎.任务详情 277 | srt内容 = self.识别引擎.结果转srt() 278 | 279 | 280 | srt文件 = os.path.splitext(self.文件)[0] + '.srt' 281 | print(f'写入文件:{srt文件}') 282 | with open(srt文件, 'w', encoding='utf-8') as f: 283 | f.write(srt内容) 284 | 285 | srt文本文件 = os.path.splitext(self.文件)[0] + '.txt' 286 | print(f'写入文本文件:{srt文本文件}') 287 | with open(srt文本文件, 'w', encoding='utf-8') as f: 288 | srt载入 = srt.parse(srt内容) 289 | for 字幕 in srt载入: 290 | f.write(f'{字幕.content}\n') 291 | 292 | 293 | 294 | 295 | if __name__ == '__main__': 296 | main() -------------------------------------------------------------------------------- /assets/Sponsor.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HaujetZhao/ali-audio-to-srt/41a00badd2127f4d0c05d377e005ed92dad95dae/assets/Sponsor.png -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | srt 2 | aliyun-python-sdk-core 3 | oss2 4 | icecream -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | # coding=utf-8 2 | # 程序名 3 | # 4 | # This Source Code Form is subject to the terms of the Mozilla Public 5 | # License, v. 2.0. If a copy of the MPL was not distributed with this 6 | # file, You can obtain one at https://mozilla.org/MPL/2.0/. 7 | # 8 | # Copyright (c) 2021 Haujet Zhao 9 | 10 | from setuptools import setup 11 | 12 | # python setup.py build sdist clean install & audio-video-resync 13 | # twine upload -u USERNAME -p PASSWORD "dist/audio-video-resync-0.5.0.tar.gz" 14 | 15 | setup( 16 | name='ali-audio-to-srt', 17 | version='0.1.0', 18 | description='使用阿里云服务的录音文件识别,将音频识别为 srt 字幕', 19 | author='Haujet Zhao', 20 | author_email='haujetzhao@qq.com', 21 | url='https://github.com/HaujetZhao/***', 22 | license='MPL-2.0 License', 23 | 24 | # 依赖包 25 | install_requires=['srt', 26 | 'aliyun-python-sdk-core', 27 | 'oss2', 28 | 'icecream' 29 | ], 30 | 31 | # 含有 py 文件的文件夹(源码文件夹) 32 | packages=['ali_audio_to_srt' 33 | ], 34 | 35 | # 每个本地包中需要包含的另外的文件 36 | package_data={ 37 | 'src': ['*.md'] 38 | }, 39 | 40 | # 安装后,命令行使用的入口 41 | entry_points={ # Options: console_scripts gui_scripts 42 | 'console_scripts': [ 43 | 'ali-audio-to-srt=ali_audio_to_srt.__main__:main', 44 | 'AliAudioToSrt=ali_audio_to_srt.__main__:main', 45 | '阿里云音频转字幕=ali_audio_to_srt.__main__:main', 46 | '阿里云转字幕=ali_audio_to_srt.__main__:main' 47 | ] 48 | }, 49 | 50 | platforms=["all"], 51 | 52 | classifiers=[ 53 | # Optional 54 | # How mature is this project? Common values are 55 | # 3 - Alpha 56 | # 4 - Beta 57 | # 5 - Production/Stable 58 | 'Development Status :: 4 - Beta', 59 | 60 | # Pick your license as you wish 61 | 'License :: OSI Approved :: Mozilla Public License 2.0 (MPL 2.0)', 62 | 63 | # Specify the Python versions you support here. In particular, ensure 64 | # that you indicate you support Python 3. These classifiers are *not* 65 | # checked by 'pip install'. See instead 'python_requires' below. 66 | 'Programming Language :: Python :: 3' 67 | ], 68 | 69 | # python 版本要求 70 | python_requires='>=3', 71 | 72 | ) 73 | 74 | -------------------------------------------------------------------------------- /打包/Pyinstaller 编译和打包.py: -------------------------------------------------------------------------------- 1 | # This Source Code Form is subject to the terms of the Mozilla Public 2 | # License, v. 2.0. If a copy of the MPL was not distributed with this 3 | # file, You can obtain one at https://mozilla.org/MPL/2.0/. 4 | # 5 | # Copyright (c) 2021 Haujet Zhao 6 | 7 | import os 8 | import sys 9 | from icecream import ic 10 | from shutil import copy, move, copytree, rmtree 11 | import shlex 12 | import subprocess 13 | import glob, re 14 | from pathlib import Path 15 | from icecream import ic 16 | from pprint import pprint 17 | import platform 18 | 19 | # 有两种打包方式 20 | # 第一种是打包为一个大包 21 | # 第二种是分成两个包,这种情况依赖于虚拟环境 22 | # 两种都需要把相对导入改为绝对导入 23 | 24 | # 当使用虚拟环境时两种方式都可以用, 25 | # 当不使用虚拟环境时,只能用第一种。 26 | 27 | # 对于小软件,使用第一种方式,打包成一个包就可以。 28 | # 比如用到了人工智能库,打包出来可能有 1GB 大小,每次更新的时候都重新打包一遍就很浪费 29 | # 就可以打包为两个包,其中一个是依赖库,另一个是源代码,每次更新只要更新原代码包就可以 30 | 31 | 使用虚拟环境 = True 32 | 源码分开打包 = True 33 | 依赖文件重新7z打包 = False 34 | 35 | 软件名字 = '阿里云音频转字幕' 36 | 37 | # 源文件夹不要带空格,不要带减号,不要带中文,否则 pip 打包会失败 38 | 源码文件夹名 = 'ali_audio_to_srt' 39 | 源码文件夹路径 = f'../{源码文件夹名}' 40 | 图标路径 = f'{源码文件夹名}/misc/icon.ico' 41 | 隐藏控制台 = False 42 | 43 | 需要单独复制的包 = [ 44 | ] 45 | 46 | # ====================函数区======================== 47 | 48 | def 复制(源, 目标, 过滤规则列表:list=None): 49 | # 得到源地址所有文件路径 50 | 源文件集合 = set(glob.glob(str(Path(源) / '**'), recursive=True)) 51 | 52 | # 将文件夹路径剃除 53 | for item in 源文件集合.copy(): 54 | if os.path.isdir(item): 55 | 源文件集合.remove(item) 56 | 57 | # 将要过滤的文件路径剃除 58 | if 过滤规则列表: 59 | for 过滤规则 in 过滤规则列表: 60 | 过滤规则路径 = str(Path(源) / 过滤规则) 61 | 过滤文件集合 = set(glob.glob(过滤规则路径, recursive=False)) 62 | 源文件集合 -= 过滤文件集合 63 | 64 | print('\n要复制的文件源路径:') 65 | pprint(源文件集合) 66 | print('\n') 67 | 68 | # 创建任务列表[ 69 | # [源1, 目标1] 70 | # [源2, 目标2] 71 | # ... 72 | # ] 73 | 复制任务列表 = [] 74 | for 源文件路径 in 源文件集合: 75 | 目标文件路径 = str(Path(目标) / (Path(源文件路径).relative_to(源))) 76 | 复制任务列表.append([源文件路径, 目标文件路径]) 77 | 78 | for 任务 in 复制任务列表: 79 | if not Path(Path(任务[1]).parent).exists(): os.makedirs(Path(任务[1]).parent) 80 | copy(任务[0], 任务[1]) 81 | ... 82 | 83 | def 压缩到7z(源, 目标): 84 | if os.path.exists(目标): os.remove(目标) 85 | 压缩命令 = f'7z a -t7z "{目标}" "{源}" -mx=9 -ms=200m -mf -mhc -mhcf -mmt -r' 86 | 命令参数 = shlex.split(压缩命令) 87 | subprocess.run(命令参数) 88 | 89 | def 读取文本内容(文件路径): 90 | try: 91 | with open(文件路径, 'r', encoding='utf-8') as f: 92 | 文本内容 = f.read() 93 | except: 94 | with open(文件路径, 'r', encoding='gbk') as f: 95 | 文本内容 = f.read() 96 | return 文本内容 97 | 98 | def 正则批量替换文件内容(文件名通配符, 搜索内容, 替换内容): 99 | for path in glob.glob(文件名通配符, recursive=True): 100 | print(f'\n开始正则替换:{path}\n搜索内容:{搜索内容}\n替换内容:{替换内容}') 101 | if not os.path.isfile(path): continue 102 | 文本内容 = 读取文本内容(path) 103 | 文本内容 = re.sub(搜索内容, 替换内容, 文本内容, flags=re.M) 104 | with open(path, 'w', encoding='utf-8') as f: 105 | f.write(文本内容) 106 | 107 | # ====================代码区======================== 108 | 109 | 110 | print(f'使用虚拟环境:{使用虚拟环境}') 111 | print(f'源码分开打包:{源码分开打包}') 112 | print(f'隐藏控制台:{隐藏控制台}') 113 | 114 | 启动器 = 'launcher' 115 | 启动器文件名 = f'{启动器}.py' 116 | 117 | if not 使用虚拟环境: 118 | 源码分开打包 = False 119 | 120 | # 如果使用虚拟环境,就检查虚拟环境,如果没有,就创建,并安装依赖包 121 | if 使用虚拟环境 and not os.path.exists('../pyvenv.cfg'): 122 | print(f'使用虚拟环境,但未发现虚拟环境,开始新建虚拟环境') 123 | 命令 = f'python -m venv .' 124 | 命令参数 = shlex.split(命令) 125 | subprocess.run(命令, cwd='..') 126 | 127 | print(f'更新 pip') 128 | 命令 = f'"Scripts/pip" install -i https://pypi.tuna.tsinghua.edu.cn/simple pip -U' 129 | 命令参数 = shlex.split(命令) 130 | subprocess.run(命令, cwd='..') 131 | 132 | print(f'开始在虚拟环境中安装依赖包') 133 | 命令 = f'"Scripts/pip" install -l https://pypi.tuna.tsinghua.edu.cn/simple -r requirements.txt' 134 | 命令参数 = shlex.split(命令) 135 | subprocess.run(命令, cwd='..') 136 | 137 | 命令 = f'"Scripts/pip" install -l https://pypi.tuna.tsinghua.edu.cn/simple pyinstaller' 138 | 命令参数 = shlex.split(命令) 139 | subprocess.run(命令, cwd='..') 140 | 141 | ... 142 | 143 | # 如果是虚拟环境 144 | # 如果是分开打包 145 | # 检查是否有依赖包 146 | # 如果有就移动到备用地方 147 | # 如果没有就拷贝一份 148 | if 使用虚拟环境: 149 | if 源码分开打包: 150 | if os.path.exists(f'./dist/{启动器}/site-packages'): 151 | move(f'./dist/{启动器}/site-packages', f'./dist/site-packages') 152 | # move(f'./dist/{启动器名}/bin', f'./dist/bin') 153 | else: 154 | if not os.path.exists(f'./dist/site-packages'): 155 | print(f'正在从虚拟环境复制一份依赖包……') 156 | copytree(f'../Lib/site-packages', f'./dist/site-packages') 157 | 158 | # 准备临时原文件,将相对导入替换为绝对导入 159 | 临时源文件夹 = './dist/src' 160 | if os.path.exists(临时源文件夹): rmtree(临时源文件夹) 161 | 排除列表 = [ 162 | '/bin/**', 163 | '__init__.py', 164 | '**/**.pyc', 165 | '**.ini', 166 | '**.db' 167 | ] 168 | if Path(临时源文件夹).exists(): rmtree(临时源文件夹) 169 | 复制(源码文件夹路径, 临时源文件夹, 过滤规则列表=排除列表) 170 | 正则批量替换文件内容(f'{临时源文件夹}/**.py', r'(^\s*)import\s+\.', r'\1import ') 171 | 正则批量替换文件内容(f'{临时源文件夹}/**.py', r'(^\s*)from\s+\.', r'\1from ') 172 | 173 | # 如果是虚拟环境 174 | # 如果是分开打包,准备 launcher.py,打包 175 | # 如果是一个包,将 __main__.py 做成 launcher.py,打包 176 | # 如果不是虚拟环境,将 __main__.py 做成 launcher.py,打包 177 | 启动器路径 = f'{临时源文件夹}/{启动器文件名}' 178 | if 使用虚拟环境: 179 | if 源码分开打包: 180 | 启动器内容 = '''import os 181 | import sys 182 | import pathlib 183 | 184 | # 将 site-packages 目录导入 python 寻找 package 和 moduel 的变量 185 | sys.path.append(str(pathlib.Path(os.path.abspath(__file__)).parent / 'site-packages')) 186 | 187 | import main 188 | main.main()''' 189 | with open(启动器路径, 'w', encoding='utf-8') as f: 190 | f.write(启动器内容) 191 | 192 | 隐藏控制台选项 = '-w' if 隐藏控制台 else '' 193 | 图标选项 = f'-i "{图标路径}"' if os.path.exists(图标路径) else '' 194 | 命令 = f'''"../Scripts/pyinstaller" --noconfirm {隐藏控制台选项} {图标选项} 195 | --hidden-import distutils.version 196 | --hidden-import uuid 197 | --hidden-import distutils.version 198 | --hidden-import imp 199 | --hidden-import unittest.mock 200 | --hidden-import cProfile 201 | --hidden-import xml.etree 202 | --hidden-import http.cookies 203 | --hidden-import json 204 | --hidden-import timeit 205 | --hidden-import math 206 | --hidden-import cgi 207 | "{启动器路径}" 208 | ''' 209 | else: # 一起打包 210 | move(f'{临时源文件夹}/__main__.py', 启动器路径) 211 | 隐藏控制台选项 = '-w' if 隐藏控制台 else '' 212 | 图标选项 = f'-i "{图标路径}"' if os.path.exists(图标路径) else '' 213 | 命令 = f'''"../Scripts/pyinstaller" --noconfirm {隐藏控制台选项} {图标选项} 214 | --hidden-import http.cookies 215 | --hidden-import json 216 | --hidden-import timeit 217 | --hidden-import math 218 | "{启动器路径}" 219 | ''' 220 | else: # 非虚拟环境打包 221 | move(f'{临时源文件夹}/__main__.py', 启动器路径) 222 | 隐藏控制台选项 = '-w' if 隐藏控制台 else '' 223 | 图标选项 = f'-i "{图标路径}"' if os.path.exists(图标路径) else '' 224 | 命令 = f'''pyinstaller --noconfirm {隐藏控制台选项} {图标选项} 225 | --hidden-import http.cookies 226 | --hidden-import json 227 | --hidden-import timeit 228 | --hidden-import math 229 | "{启动器路径}" 230 | ''' 231 | 命令参数 = shlex.split(命令) 232 | subprocess.run(命令参数) 233 | os.remove(f'{临时源文件夹}/{启动器文件名}') # 删除 launcher.py 234 | rmtree(f'{临时源文件夹}/__pycache__') if os.path.exists(f'{临时源文件夹}/__pycache__') else ... 235 | if 使用虚拟环境: 236 | if 源码分开打包: 237 | move(f'{临时源文件夹}/__main__.py', f'{临时源文件夹}/main.py') 238 | 复制(f'{临时源文件夹}', f'./dist/{启动器}') # 将源码相关文件复制到打包输出目录 239 | 240 | if not 源码分开打包: 241 | 242 | # ic(需要单独复制的包) 243 | for package in 需要单独复制的包: 244 | for path in sys.path: 245 | if os.path.isdir(path): 246 | if package in os.listdir(path): 247 | if os.path.exists(f'./dist/{启动器}/{package}'): rmtree(f'./dist/{启动器}/{package}') 248 | copytree(Path(path) / package, f'./dist/{启动器}/{package}') 249 | break 250 | 251 | # 将依赖包移动到打包目录 252 | if 源码分开打包: 253 | move(f'./dist/site-packages', f'./dist/{启动器}/site-packages') 254 | 255 | # exe 重命名 256 | exe文件名 = f'_{软件名字}.exe' 257 | move(f'./dist/{启动器}/{启动器}.exe', f'./dist/{启动器}/{exe文件名}') 258 | 259 | if 源码分开打包: 260 | # 7z 压缩依赖文件,如果依赖文件 7z 包存在,那就不压缩了 261 | 262 | 包名 = f'依赖文件-{软件名字}_{platform.system()}.7z' 263 | if 依赖文件重新7z打包: 264 | if os.path.exists(包名): os.remove(包名) 265 | print(f'7z 压缩依赖包') 266 | 压缩到7z(f'./dist/{启动器}/*', 包名) 267 | # 依赖文件-AudioSyncVideo_Windows.7z 268 | # 依赖文件-AudioSyncVideo_Windows.7z 269 | 270 | # 7z 压缩源文件 271 | 包名 = f'源代码-{软件名字}_{platform.system()}.7z' 272 | if os.path.exists(包名): os.remove(包名) 273 | print(f'7z 压缩源代码') 274 | 压缩到7z(f'{临时源文件夹}/*', 包名) 275 | 276 | else: # 只打包一个包 277 | 包名 = f'{软件名字}_{platform.system()}.7z' 278 | if 依赖文件重新7z打包: 279 | if os.path.exists(包名): os.remove(包名) 280 | print(f'7z 压缩整包') 281 | 压缩到7z(f'./dist/{启动器}/*', 包名) 282 | if platform.system() == 'Windows': 283 | exe绝对路径 = Path('.') / f'dist/{启动器}/{exe文件名}' 284 | os.system(f'explorer /select, "{exe绝对路径}"') 285 | --------------------------------------------------------------------------------