Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File not closed after merging audio #101

Closed
Eloar opened this issue Dec 11, 2014 · 11 comments
Closed

File not closed after merging audio #101

Eloar opened this issue Dec 11, 2014 · 11 comments
Labels
audio Related to AudioClip, or handling of audio in general. bug Issues that report (apparent) bugs.

Comments

@Eloar
Copy link
Contributor

Eloar commented Dec 11, 2014

I was extracting audio from one clip to add it to another. Using temporary mp3 file felt bit useless so I tried to make it directly.

imSeq = ImageSequenceClip(sequence_dir, fps)
auSeq = VideoFileClip(audio_file)
imSeq.audio = auSeq.audio.subclip(start_time, imSeq.duration + start_time)
imSeq.write_videofile(outfile, verbose=False)

The only problem audio file is no longer needed so I try to delete it. I tried to do it same way I did it after extraction to mp3 file:

del imSeq.audio.reader
del imSeq.audio
del imSeq

del auSeq.audio.reader
del auSeq.audio
del auSeq

But it doesn't work here. This centipede is result of many unsuccesfull tries.

@Zulko
Copy link
Owner

Zulko commented Dec 11, 2014

I am not sure I understand the problem, what is exactly the code that produces the error and what is the error produced ? Also for the record can you give your Python version/System/moviepy version (I assume it's the last version).

And a remark: you can directly write

auSeq = AudioFileClip(audio_file) # even works when audio_file is a video
imSeq.audio = auSeq # no .audio needed, auSeq is already an audioclip.

@Eloar
Copy link
Contributor Author

Eloar commented Dec 17, 2014

The problem is, I can't delete file after all work is done and it is no longer neaded. The file is opened by other process (I assume it is ffmpeg in the background)

My code at the moment:

    ac = AudioFileClip(audio_file)
    vc.audio = ac.subclip(self._audio_start, self._audio_start + vc.duration) if ac.duration + self._audio_start <= vc.duration else afx.audio_loop(ac, duration=vc.duration)
    vc.write_videofile(out_file)

    del vc.audio
    del vc

    del ac.reader
    del ac

vc is VideoFileClip at the moment and I can not delete file from which it was created. Same applies to ac. Those are both temporary files and I would like delete those immediatly.

@Zulko
Copy link
Owner

Zulko commented Dec 17, 2014

This works fine for me (if I add os.remove(audio_file) at the end it will remove the audio file without problem, even if I don't write all the "del"). Again: can you give a code that produces the error, a copy of the entire error, your OS, and your Python version ? Otherwise I can't help you.

@Eloar
Copy link
Contributor Author

Eloar commented Dec 17, 2014

Ok, so in my code there is error as <= should be changed to >= this means I make loop instead of subclip. It revealed error. audio_loop made imposible to remove audio file

ac = afx.audio_loop(ac, duration=vc.duration)

@Zulko
Copy link
Owner

Zulko commented Dec 17, 2014

I have no problem, even with audio_loop. Maybe you can try to make me a very simple script so that I can reproduce the problem. And please give the error, I still don't know which error you really have. And your OS, and your Python version.

@Eloar
Copy link
Contributor Author

Eloar commented Dec 17, 2014

Errors are:

PermissionError: [WinError 32]
OSError: [WinError 6]

Second one is just ignered exception in del:
Exception ignored in: <bound method FFMPEG_AudioReader.__del__ of <moviepy.audio.io.readers.FFMPEG_AudioReader object at 0x02FB1D90>>

The problem is, handler and all looks just fine...

Simple example code:

from pytube import YouTube
from moviepy.editor import *
import os

if __name__ == "__main__":
    yt1 = YouTube()
    yt2 = YouTube()
    yt1.url = "https://www.youtube.com/watch?v=Z8QKGgVU9zQ"
    yt2.url = "https://www.youtube.com/watch?v=0NvnWJU5Ak8"

    yt1.filter('mp4')[-1].download("d:\\eloar")
    yt2.filter('mp4')[-1].download("d:\\eloar")

    vc = VideoFileClip("d:\\eloar\\BB-8 Droid - Star Wars - The Force Awakens.mp4")
    ac = AudioFileClip("d:\\eloar\\Rammstein - Ich tu dir weh (Official Video) HD.mp4")

    vc = vfx.loop(vc, duration=250)
    vc.audio = afx.audio_loop(ac, duration=268).subclip(18, 268)

    vc.write_videofile("d:\\eloar\\test.mp4", verbose=False)

    try:
        os.remove("d:\\eloar\\BB-8 Droid - Star Wars - The Force Awakens.mp4")
    except PermissionError as e:
        print(e)
    try:
        os.remove("d:\\eloar\\Rammstein - Ich tu dir weh (Official Video) HD.mp4")
    except PermissionError as e:
        print(e)

So this way I can not remove any of downloaded files. I checked if pytube left them open, and it did not. I used audio_loop and subclip, as this is one of usages for my final project.

System: Microsoft Windows 7 Professional N [Version 6.1.7601]
Python: Python 3.4.1
MoviePy: 0.2.1.9.07

@Eloar
Copy link
Contributor Author

Eloar commented Dec 22, 2014

Using close_proc() and close() before del let me get rid of [WinError 6], but I was still getting [WinError 32]. As one last try I added wait() after terminate() and got rid of [WinError 32]. I made just some tries, but it might be it, and it might be help for problem in #73. I had to ad wait inside close_proc() and close() so, how about adding it to moviepy?

@Zulko
Copy link
Owner

Zulko commented Dec 22, 2014

Thanks for the potential fix ! It's strange, it looks like a potential different behaviour of the Subprocess module in Python3.4, The problem is that "wait()" also used to hang python forever in some systems, I'll investigate when I have time.

@Eloar
Copy link
Contributor Author

Eloar commented Dec 22, 2014

Well it always have potential of creating deadlock. I might be testing my project in some different environments, so I will report any troubles with this change if faced.

@keikoro
Copy link
Collaborator

keikoro commented Feb 18, 2017

It's unclear to me if the suggested changes were added to MoviePy. Leaving this open for now.

@keikoro keikoro added the bug Issues that report (apparent) bugs. label Feb 18, 2017
@keikoro keikoro added the audio Related to AudioClip, or handling of audio in general. label Nov 25, 2017
@tburrows13
Copy link
Collaborator

Closing files in moviepy has changed so much since 0.2.1.9.07 that this issue is no longer valid. Please re-open a new issue if this behaviour is still causing a problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
audio Related to AudioClip, or handling of audio in general. bug Issues that report (apparent) bugs.
Development

No branches or pull requests

4 participants