简体   繁体   English

通过终端运行时,Python脚本无法正常运行,但在Jupyter和Visual Studio中可以正常运行

[英]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.

相关问题 从终端运行并从Python运行时,脚本的工作方式不同 - Script works differently when ran from the terminal and ran from Python `import pyspark`可在Jupyter中使用,但不适用于python shell / script - `import pyspark` works in Jupyter, but doesn't work with python shell/script 为什么我的 selenium 脚本在我不在正在运行的浏览器中观看时不能正常工作,但当我正常工作时? - Why doesn't my selenium script work properly when im not watching in my running browser but when i am it works fine? Seaborn的Python库在Visual Studio中无法正常工作 - Python library seaborn doesn't work properly in Visual Studio Python脚本不会从存档中删除文件-通过终端打印的命令可以正常工作 - Python script doesn't delete file from archive — printed command via terminal works fine 脚本在终端中有效,但在使用ProcessBuilder运行时不起作用 - Script works in terminal but not when ran using ProcessBuilder Python 脚本仅在 Visual Studio Code 中运行时有效 - Python script only working when ran in Visual Studio Code 我在TextWrangler中运行Python并且它工作正常但在Mac终端上却没有 - I run Python in TextWrangler and it works fine but on Mac Terminal it doesn't 在 python shell 中运行时,我的 Python 乌龟不起作用 - My Python turtle doesn't work when ran in python shell Pyinstaller 在运行 .exe 时给出“'chromedriver.exe' 可执行文件需要在 PATH 中”,但实际的 python 脚本工作正常 - Pyinstaller gives “ 'chromedriver.exe' executable needs to be in PATH” when the .exe is ran, but the actual python script works fine
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM