我试图在Python脚本中使用下面的命令,但是我看到这是一个PowerShell问题,因为它似乎找不到我指向的视频文件。
我在以下位置的云驱动器Z:
上有一个视频文件(为了再现性,将其更改为您想要的任何路径,其中包含视频文件):
Z:Udemy_And_Misc_DownloadsTensorFlow Developer Certificate in 2021 Zero to Mastery3. Neural network regression with TensorFlow18. Setting up TensorFlow modelling experiments part 2 (increasing complexity).mp4
注意文件名中的空格和特殊字符。
我要做的是使用以下命令将视频文件降采样到较小的大小:
ffmpeg -i "Z:Udemy_And_Misc_DownloadsTensorFlow Developer Certificate in 2021 Zero to Mastery[TutsNode.com] - TensorFlow Developer Certificate in 2021 Zero to Mastery3. Neural network regression with TensorFlow18. Setting up TensorFlow modelling experiments part 2 (increasing complexity).mp4" -y -vcodec libx264 -acodec ac3 -threads 1 "Z:Udemy_And_Misc_DownloadsTensorFlow Developer Certificate in 2021 Zero to Mastery[TutsNode.com] - TensorFlow Developer Certificate in 2021 Zero to Mastery3. Neural network regression with TensorFlow18. Setting up TensorFlow modelling experiments part 2 (increasing complexity)DOWNSAMPLED.mp4"
我知道它很长,因为它主要被文件名消耗,但我得到的错误是:
Z:Udemy_And_Misc_DownloadsTensorFlow Developer Certificate in 2021 Zero to Mastery[TutsNode.com] - TensorFlow Developer Certificate in 2021 Zero to Mastery3. Neural network regression with TensorFlow18. Setting up TensorFlow modelling experiments part 2 (increasing complexity).mp4: No such file or directory
路径是正确的,因为它是从文件夹的URL栏直接复制粘贴的。
我尝试过的事情包括:
- 在文件名 周围使用引号
- 文件名 不使用引号
- 在文件名前加上" "(我收到消息说"你是说文件:r吗?")
- 在文件名前加上"file: ">
- 在文件路径 中使用的双斜杠
有没有一个powershell专业人员可以告诉我为什么它不能"找到"?这个文件吗?
如果我找到一个文件名,命令运行:
$oldvids = Get-ChildItem *.mp4, *mov, *wmv, *avi -Recurse
foreach ($oldvid in $oldvids)
{
$newvid = [io.path]::ChangeExtension($oldvid.FullName, '_.mp4')
ffmpeg -i $oldvid.FullName -y -vcodec libx264 -acodec ac3 -threads 1 $newvid
}
当我打印$oldvid.name
和$oldvid.FullName
时,路径/文件名看起来像这样:
Z:Udemy_And_Misc_DownloadsTensorFlow Developer Certificate in 2021 Zero to Mastery5. Computer Vision and Convolutional Neu
ral Networks in TensorFlow35. Multi-class CNN's part 9 Making predictions with our model on custom images.mp4
35. Multi-class CNN's part 9 Making predictions with our model on custom images.mp4
…第一个是我试图在我发布的代码中使用的精确副本。那么有什么区别呢?
第二更新我认为问题在于该文件位于C:
以外的驱动器上。只是通过将文件移到我的计算机上并运行命令来测试它,它工作了。所以我猜,有没有办法告诉PowerShell使用Z:
驱动器来查找文件?
我发现问题是一些文件名中有特殊字符(可能来自另一个操作系统),如'
。一旦我删除了这些,问题就消失了。是我的错。
但是,我已经创建了一个简化的脚本,可能会帮助其他人,如果他们需要它。我会通过添加一些错误检查来改进这个脚本,检查已经下载的视频文件(我在文件名中使用._
表示,但任何东西都可以使用),等等,但是这里你去:
import subprocess
import os.path
import os
# ------------------------------------------------------------------------------------------------ #
# GLOBAL VARIABLES #
# ------------------------------------------------------------------------------------------------ #
types = (".mp4", ".mov", ".wmv", ".avi") # the tuple of file types
directory_to_recurse = r"Z:Python_ProjectsMiscVideo_Reformatter"
# ------------------------------------------------------------------------------------------------ #
# HELPER FUNCTION - RUN A POWERSHELL COMMAND #
# ------------------------------------------------------------------------------------------------ #
def run_powershell_command(cmd):
completed = subprocess.run(["powershell", "-Command", cmd], capture_output=True)
return completed
# ------------------------------------------------------------------------------------------------ #
# SIMPLIFIED TEST #
# ------------------------------------------------------------------------------------------------ #
# Recursively iterate through all folders in "directory_to_recurse", finding video files
# along the way
for root, dirs, files in os.walk(directory_to_recurse):
# Inspect
print(root, "n", dirs, "n", files, "n")
for file in files:
# If we've found a video file...
if file.endswith(types):
# ...parse some data
full_path_and_file = os.path.join(root, file)
filename_only = os.path.splitext(file)[0]
extension_only = os.path.splitext(file)[1]
# Inspect
print(full_path_and_file,"n", filename_only,"n", extension_only,"n")
# Run the ffmpeg downsampling command
result = run_powershell_command("ffmpeg -i '" + full_path_and_file +
"' -y -vcodec libx264 -acodec ac3 -threads 2 '" +
str(root) + "\" + str(filename_only) + "._" +
str(extension_only) + "'")
# Inspect the result object (if desired)
print("result:", result)
# If we receive a return code of anything over 0, exit
if result.returncode >= 1:
print("ERROR!")
exit(result.returncode)
# Once we've successfully downsampled the video, delete the original
os.remove(os.path.join(root, full_path_and_file))
continue