In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains the "Python how to achieve batch conversion of video and audio sampling rate", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Python how to achieve batch conversion of video and audio sampling rate" bar!
Environmental dependence
Ffmpeg environment installation
Ffmpy installation:
Pip install ffmpy-I https://pypi.douban.com/simple code
No nonsense, go to the code.
#! / user/bin/env python# coding=utf-8 "" @ project: csdn@author: swordsman A Liang _ ALiang@file: transfor_mp4_audio_sampling_rate.py@ide: PyCharm@time: 2021-11-24 13:48:08 "from ffmpy import FFmpegimport os def transfor (video_path: str, tmp_dir: str Result_dir: str): file_name = os.path.basename (video_path) base_name = file_name.split ('.) [0] file_ext = file_name.split ('.) [- 1] ext = 'wav' audio_path = os.path.join (tmp_dir,' {}. {} '.format (base_name, ext)) print (' File name: {} Extract audio '.format (audio_path) ff = FFmpeg (inputs= {video_path: None}) Outputs= {audio_path:'- f {}-vn-ac 1-ar 16000-y'.format ('wav')}) print (ff.cmd) ff.run () if os.path.exists (audio_path) is False: return None video_tmp_path = os.path.join (tmp_dir,' {} _ 1. {} '.format (base_name File_ext)) ff_video = FFmpeg (inputs= {video_path: None}, outputs= {video_tmp_path:'- an'}) print (ff_video.cmd) ff_video.run () result_video_path = os.path.join (result_dir, file_name) ff_fuse = FFmpeg (inputs= {video_tmp_path: None, audio_path: None} Outputs= {result_video_path:'- map 0 map v-map 1Parade a-c video_dir v copy-C V aac-shortest'}) print (ff_fuse.cmd) ff_fuse.run () return result_video_path def handle (video_dir: str, tmp_dir: str Result_dir: str): if os.path.isdir (video_dir): for file in os.listdir (video_dir): try: result = transfor (os.path.join (video_dir, file), tmp_dir Result_dir) except Exception as e: print (e) continue if result: print (result) if _ _ name__ ='_ _ main__': handle ('CGV _ main__': handle') 'Corexpact UsersUnixUsersAccording to Desktopripple result')
Code description
1. The input parameters of handle method are: video directory, temporary directory, and final result video directory.
2. The handle method traverses the video directory and performs transfor processing on the video at one time. The steps are: (1) extract the video audio and convert it into defined sampling rate audio; (2) extract the video without audio in the video; (3) fuse the new video and the new audio to synthesize the final video to the specified result directory.
3. No audio exception is discarded directly, and no conversion is required.
4. The file name of the final result directory is the same as that of the original video directory.
5. The code is mainly converted to 16k sampling rate, which can be adjusted according to the demand.
Verify it.
Original video sampling rate
Results Video sampling rate
Thank you for reading, the above is the content of "Python how to achieve batch conversion of video and audio sampling rate". After the study of this article, I believe you have a deeper understanding of how Python achieves batch conversion of video and audio sampling rate, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.