[英]Python script doesn't work properly when ran through terminal, but works fine in Jupyter and Visual Studio
I have a script to extract the audio from all video files in a folder. 我有一个脚本,可以从文件夹中的所有视频文件中提取音频。
The folder with videos is located at: /Users/MyName/Downloads/Video_Audio_files 包含视频的文件夹位于:/ Users / MyName / Downloads / Video_Audio_files
When I try to run it through terminal and I'm prompted for the folder path folder = input("Path to folder:")
, I drag and drop it there (which is how I got the above path), but the script doesn't seem to be working (stuck at 0 out of 7 and no output files). 当我尝试通过终端运行它并提示
folder = input("Path to folder:")
文件夹路径folder = input("Path to folder:")
,我将其拖放到此处(这是我获得上述路径的方式),但是脚本没有似乎不起作用(卡在7中的0,并且没有输出文件)。
When I input exactly the same path when prompted in Jupyter Notebook or in Visual Studio it works perfectly! 当我在Jupyter Notebook或Visual Studio中提示时输入完全相同的路径时,它可以完美运行!
Edit: I think I have found the issue, when I drag and drop the folder, there is an extra space ('Downloads/folder ' instead of 'Downloads/folder'). 编辑:我想我已经找到了问题,当我拖放文件夹时,有一个额外的空间(“下载/文件夹”而不是“下载/文件夹”)。
pbar = ProgressBar()
files = []
extensions = []
folder = input("Path to folder:")
#folder = 'Video_Audio_files'
pathlist = Path(folder).glob('**/*.mp4')
for path in pathlist:
path_in_str = str(path)
name = path_in_str.split("/")[1]
files.append(path_in_str.split(".")[0])
extensions.append(path_in_str.split(".")[1])
os.system('cd ' + folder)
for i in pbar(range(len(files))):
video_format = extensions[i]
video_name = files[i]
output_format = 'flac'
output_name = video_name + '_audio'
bashCommand = 'ffmpeg -i ' + video_name + '.' + video_format + ' -f ' + output_format + ' -ab 192000 -vn ' + output_name + '.' + output_format
#should be of this format: bashCommand = 'ffmpeg -i Video.mp4 -f flac -ab 192000 -vn ExtractedAudio.flac'
os.system(bashCommand)
如编辑中所述找到问题后,我只是实现了以下内容以消除多余的空间:
folder = (input("Path to folder:")[:-1])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.