2

I'm getting the following error when I run my code (the error occurs at the *** line):

Exception has occurred: PermissionError
[WinError 32] The process cannot access the file because it is being used by another process: 'pexels-joshua-woroniecki-9073157.mp4' #this file is one of two files found in my stockFootage directory

I'm trying to delete a file, but it's telling me I got a permission error. Does anyone know how I could fix this? Any help would be greatly appreciated. Thank you.

My Code:

chdir(r'C:\Users\jack_l\Documents\REDDIT_TO_YOUTUBE_PYTHON_SELENIUM\redditVideo\stockFootage')
myStockFootage = next(walk(r'C:\Users\jack_l\Documents\REDDIT_TO_YOUTUBE_PYTHON_SELENIUM\redditVideo\stockFootage'), (None, None, []))[2]
stockFootage = VideoFileClip(myStockFootage[0], target_resolution=(1080, 1920))
stockFootage = stockFootage.without_audio()
stockFootage = stockFootage.loop(duration = mergedVideos.duration)
stockFootage.write_videofile('loopedStock.mp4', fps = 30)

for counter24 in range(len(myStockFootage)):
    if 'loopedStock' not in myStockFootage[counter24]:
***     os.remove(myStockFootage[counter24])

chdir(r'C:\Users\jack_l\Documents\REDDIT_TO_YOUTUBE_PYTHON_SELENIUM\redditVideo\finalVideo')
finalVideo = CompositeVideoClip([stockFootage, commentVideo])
finalVideo.write_videofile('finalVideo.mp4', fps=30)

print('Finished')
  • Your script is trying to delete the file in the error message, but it can't since some program still has an active handle to it. Perhaps it's open in a player somewhere? Or perhaps some previous software you used to write or edit it didn't close properly? Does the problem persist if you restart your computer? Can you move / delete the file outside your script? – Grismar May 25 '22 at 22:58
  • @Grismar Once the program stops running I can do whatever I want with the file (delete, move, etc). – THEMOUNTAINSANDTHESKIES May 25 '22 at 23:13
  • Then it seems likely that your call to `VideoFileClip()` is causing the lock and you should dispose of the object before trying to remove the file. Try calling `close()` on it, or use a context manager, if it supports it. – Grismar May 25 '22 at 23:15
  • @Grismar By calling close, do you mean do something like ```myStockFootage[counter24].close()``` inside of that if statement and before the os.remove? Because when I do that, I get an error saying str isn't compatible with .close() – THEMOUNTAINSANDTHESKIES May 25 '22 at 23:16
  • `stockFootage.close()` – Grismar May 25 '22 at 23:28

1 Answers1

1

From the comments it is clear that you're either not sharing the code causing the actual problem, or making more changes than was suggested in the comments.

Take this:

from shutil import copy
from os import remove
import moviepy.video.io.VideoFileClip as VideoFileClip

copy('test.mp4', 'new_test.mp4')
stock_footage = VideoFileClip.VideoFileClip(r'new_test.mp4', target_resolution=(1080, 1920))
try:
    remove('new_test.mp4')
except PermissionError:
    print('as expected')
stock_footage .close()
try:
    remove('new_test.mp4')
    print('success')
except PermissionError:
    print('you will not see this')

Output (assuming you have a test.mp4 in the same location as the script):

as expected
success

Which shows that VideoFileClip locks the file it opens and calling .close() on it resolves the issue.

Grismar
  • 27,561
  • 4
  • 31
  • 54