antiboredom / videogrep

automatic video supercuts with python
https://antiboredom.github.io/videogrep
Other
3.34k stars 257 forks source link

Encountering ValueError during operation #5

Closed AlexMathew closed 2 years ago

AlexMathew commented 10 years ago

First of all, brilliant idea and very well executed !

I was trying to use videogrep to generate a supercut of clips from a movie, but I encountered a ValueError.

I edited 2 lines, FFMPEG_BINARY = '/usr/bin/ffmpeg' usable_extensions = ['mp4', 'avi', 'mov', 'mkv']

The folder structure that I'm trying to run it on - /Fight Club ├── Fight Club.mkv └── Fight Club.srt

$ python videogrep.py --input ~/Stuff/Movies/Fight\ Club --search 'Tyler'

The traceback of the error -

Traceback (most recent call last):
  File "videogrep.py", line 200, in <module>
    videogrep(args.inputfile, args.outputfile, args.search, args.searchtype, args.maxclips, args.padding, args.test, args.randomize)
  File "videogrep.py", line 182, in videogrep
    create_supercut(composition, outputfile, padding)
  File "videogrep.py", line 87, in create_supercut
    video = concatenate(clips)
  File "/home/alexmathew/pyenv/local/lib/python2.7/site-packages/moviepy/video/compositing/concatenate.py", line 55, in concatenate
    w = max([r[0] for r in sizes])
ValueError: max() arg is an empty sequence

Do let me know what the possible problems could be, and how to fix it.

antiboredom commented 10 years ago

thanks for posting this - some other people are getting the same error, but I haven't been able to replicate it yet. Does it work on other videos?

Zulko commented 10 years ago

I'll see what I can do ;) EDIT: On second thought, maybe the problem is not on my side, it just means that "concatenate()" receives a list of 0 clips to put together. Looks more like a problem with the search returning no result (like, the required keyword is not in the srt. Strange though).

shawnr commented 10 years ago

I just got this error using this copy of Night of the Living Dead: https://archive.org/details/Night.Of.The.Living.Dead_1080p (the version here: https://archive.org/download/Night.Of.The.Living.Dead_1080p/NightOfTheLivingDead_720p_512kb.mp4) and this .srt file: http://subscene.com/subtitles/night-of-the-living-dead/english/263003.

I renamed the files to be the same, and running videogrep.py with the test flag (-t) executes successfully. I get the following error when trying to build the supercut:

MoviePy: building video file supercut.mp4.tmp40.mp4
----------------------------------------
Writing audio in supercut.mp4.tmp40TEMP_MPY_to_videofile_SOUND.ogg
|----------| 0/1217   0% [elapsed: 00:00 left: ?, ? iters/sec]Traceback (most recent call last):
  File "videogrep.py", line 191, in <module>
    videogrep(args.inputfile, args.outputfile, args.search, args.searchtype, args.maxclips, args.padding, args.test, args.randomize)
  File "videogrep.py", line 171, in videogrep
    create_supercut_in_batches(composition, outputfile, padding)
  File "videogrep.py", line 110, in create_supercut_in_batches
    video = concatenate(clips)
  File "/Users/riders/.virtualenvs/videogrep/lib/python2.7/site-packages/moviepy/video/compositing/concatenate.py", line 55, in concatenate
    w = max([r[0] for r in sizes])
ValueError: max() arg is an empty sequence

I'm running the following command to invoke videogrep (after renaming the video file to night.mp4 and the srt file to night.srt):

python videogrep.py -i night.mp4 -s "they|They|them" -st re -m 60

AlexMathew commented 10 years ago

I tried it with 5 different movies. 2 of them worked, and the other 3 landed me the same error. Maybe it'll help if you gave a few examples on how to run the program, for each of the available options. And it'll also be helpful if you elaborate on the folder structure for the input.

For the 2 that did work, it was brilliant. However, I am confused on how the supercuts are being saved. For example, I ran videogrep on "The Internship" and looked for the word "Google". I observed the program's execution, and it seems that it builds two supercut videos - the ffmpeg command runs twice. However, there's only one supercut video file, and it has only the output from the first ffmpeg command run. I'm really confused about this. Am I doing something wrong, or is there something that I'm not checking ?

EDIT: I thought I'd show how the execution is running when I tried it out. I've put the entire printed output in this gist. So it seems to be running in two parts. But the result supercut video contains only the clips from the first part. I don't know if it is some problem specific to this one, or if others have had this as well.

shawnr commented 10 years ago

I put an IPDB call in the except block on line 104 of videogrep.py, and when the create_supercut command failed I re-ran it manually in the debugger. I got this error:

ipdb> create_supercut(composition[start_index:end_index], filename, padding)
Creating clips.
139.451 to 142.715: They ought to make the day the time changes the first day of summer.
280.77 to 283.449:  And the caretaker or somebody takes them away.
370.852 to 374.117: They're coming to get you, Barbra.
377.089 to 379.35:  They're coming for you, Barbra.
382.74 to 384.289:  They're coming for you.
384.875 to 387.596: Look! There comes one of them now.
841.817 to 844.831: Probably be a lot more of them as soon as they find out about us.
978.865 to 980.289: Two of them.
985.521 to 988.578: There are two of them out there. Have you seen any more around here?
1136.342 to 1137.766:   They know we're in here now.
1467.035 to 1468.249:   They're not that strong.
1491.272 to 1494.788:   They're afraid now.
1496.295 to 1499.351:   They're afraid of fire. I found that out.
1548.159 to 1550.001:   Now, I didn't see them at first.
1658.753 to 1660.888:   Just plowed right through them.
1660.972 to 1662.73:    They didn't move.
1662.814 to 1668.507:   They didn't run, or... Just stood there, staring at me.
1670.684 to 1672.944:   Just wanted to crush them.
1677.004 to 1681.357:   They... Scattered through the air, like bugs.
1949.428 to 1952.776:   At this hour, we repeat, these are the facts as we know them.

MoviePy: building video file supercut.mp4.tmp0.mp4
----------------------------------------
Writing audio in supercut.mp4.tmp0TEMP_MPY_to_videofile_SOUND.ogg
|----------| 0/1188   0% [elapsed: 00:00 left: ?, ? iters/sec]*** IOError: [Errno 32] Broken pipe

I suspect that something in MoviePy is not liking the audio format of that original file. That causes the create_supercut function to fail, so the clips list is empty, which is the error we are seeing, as @Zulko suspected.

I'm trying to find alternate versions of the video with different audio encoding to see if it works.

Zulko commented 10 years ago

@shawnr For what it's worth your example work perfectly on my computer, it creates a 2:38 video with the expected result.

That broken pipe error is strange. The only times I had this, was when using the IPython Notebook in Pylab mode with MoviePy.

EDIT: now that I think about it, maybe it's because you don't have the codecs required to write "ogg" audio files with FFMPEG ? I doubt that it would be that, but we never know.

EDIT2: I don't think you are looking in the good direction: if it didn't like the original file it would have screamed earlier. Here the problem seems to be with the writing.

ghost commented 10 years ago

Hi guys I'm getting the same error. So far no luck

Creating clips.
Traceback (most recent call last):
  File "videogrep.py", line 191, in <module>
    videogrep(args.inputfile, args.outputfile, args.search, args.searchtype, args.maxclips, args.padding, args.test, args.randomize)
  File "videogrep.py", line 173, in videogrep
    create_supercut(composition, outputfile, padding)
  File "videogrep.py", line 88, in create_supercut
    final_clip = concatenate( cut_clips)
  File "/usr/local/lib/python2.7/dist-packages/moviepy/video/compositing/concatenate.py", line 55, in concatenate
    w = max([r[0] for r in sizes])
ValueError: max() arg is an empty sequence
Zulko commented 10 years ago

@AlexMathew @shawnr @autonomoid Could you guys remove the "try:" and "except:" lines in function create_supercut_in_batches (and de-indent the code inbetween) ?

This should give you more informative error messages about the bug.

ghost commented 10 years ago

I just tried that and the error stays exactly the same.

Zulko commented 10 years ago

Then can you try to add

print len(composition)

at the beginning of the function ? If it prints 0, it may mean that the parser didn't find the expression you were looking for.

ghost commented 10 years ago

I added

print "Composition length = " + str(len(composition))

It returned 0

Edit: Of course that's to be expected - since I was trying to read a .mpg file, which is not in the list of permitted types. That should at least throw an error and quit E.g. "File type not supported".

Edit: I made some minor changes and issued a pull request.

AlexMathew commented 10 years ago

I guess the problem was the file types, and I'd messed up the filenames of the video & subtitle file (it was off by one hyphen). Thank you @autonomoid for mentioning that. Wouldn't have noticed it otherwise. But it still doesn't explain why I'm getting only a part of my expected output for one of my runs. It's running the ffmpeg in separate parts, generating temporary files for each. But the final output file has only the output from the first part. Something like this.

ghost commented 10 years ago

Which version of python and ffmpeg is working for people?

benjaminr commented 10 years ago

@autonomoid I am using:

Python 2.7.7 ffmpeg 2.2.3

I was receiving the same: 'ValueError: max() arg is an empty sequence' error.

ghost commented 10 years ago

For people using Ubuntu 12.04, if you download, build and install the latest version of ffmpeg from github then this error seems to go away. However, I seem to have a new error when the call is made and to write the video to disk.

MoviePy: building video file supercut.mp4
----------------------------------------
Writing audio in supercutTEMP_MPY_to_videofile_SOUND.ogg
Done writing Audio in supercutTEMP_MPY_to_videofile_SOUND.ogg !

Writing video into supercutTEMP_MPY_to_videofile.mp4
|----------| 0/202   0% [elapsed: 00:00 left: ?, ? iters/sec]Traceback (most recent call last):
  File "videogrep.py", line 261, in <module>
    videogrep(args.inputfile, args.outputfile, args.search, args.searchtype, args.maxclips, args.padding, args.test, args.randomize)
  File "videogrep.py", line 241, in videogrep
    create_supercut(composition, outputfile, padding)
  File "videogrep.py", line 94, in create_supercut
    final_clip.to_videofile(outputfile)
  File "/usr/local/lib/python2.7/dist-packages/moviepy/video/VideoClip.py", line 281, in to_videofile
    verbose=verbose)
  File "/usr/local/lib/python2.7/dist-packages/moviepy/video/io/ffmpeg_writer.py", line 143, in ffmpeg_write_video
    writer.write_frame(frame.astype("uint8"))
  File "/usr/local/lib/python2.7/dist-packages/moviepy/video/io/ffmpeg_writer.py", line 108, in write_frame
    self.proc.stdin.write(img_array.tostring())
IOError: [Errno 32] Broken pipe

If you contrive it so that there is only one clip is found, then it still raises this error.

Edit: This error is caused by moviepy. Specifically in ffmpeg_writer.py. As a short term fix, I found that if you delete the parameter '-vcodec', "libx264" from line 87 it will work. I've raised an issue on the moviepy repo regarding this.

Edit: I turns out when I recently rebuilt ffmpeg I forget to configure it to include libx264 support.

antiboredom commented 10 years ago

Hey all - for any who is getting the ValueError: max() arg is an empty sequence - this is an issue where the script either can't find any of the search terms or can't find the actual subtitle track.

ghost commented 10 years ago

In my fork I put some error handling code in for this.

antiboredom commented 10 years ago

These changes are merged now.

shawnr commented 10 years ago

I believe the problem I've run into is just issues with getting full OGG support in ffmpeg. But maybe @Zulko can help me here: This is the error output I get when I remove that try/except block and let the moviepy error bubble up.

MoviePy: building video file supercut.mp4.tmp0.mp4
----------------------------------------
Writing audio in supercut.mp4.tmp0TEMP_MPY_to_videofile_SOUND.ogg
|----------| 0/1188   0% [elapsed: 00:00 left: ?, ? iters/sec]Traceback (most recent call last):
  File "videogrep.py", line 262, in <module>
    videogrep(args.inputfile, args.outputfile, args.search, args.searchtype, args.maxclips, args.padding, args.test, args.randomize, args.sync)
  File "videogrep.py", line 240, in videogrep
    create_supercut_in_batches(composition, outputfile, padding)
  File "videogrep.py", line 105, in create_supercut_in_batches
    create_supercut(composition[start_index:end_index], filename, padding)
  File "videogrep.py", line 94, in create_supercut
    final_clip.to_videofile(outputfile, codec="libx264")
  File "/Users/riders/.virtualenvs/videogrep/lib/python2.7/site-packages/moviepy/video/VideoClip.py", line 275, in to_videofile
    verbose=verbose)
  File "<string>", line 2, in to_audiofile
  File "/Users/riders/.virtualenvs/videogrep/lib/python2.7/site-packages/moviepy/decorators.py", line 60, in requires_duration
    return f(clip, *a, **k)
  File "/Users/riders/.virtualenvs/videogrep/lib/python2.7/site-packages/moviepy/audio/AudioClip.py", line 104, in to_audiofile
    codec=codec, bitrate=bitrate, write_logfile=write_logfile, verbose=verbose)
  File "<string>", line 2, in ffmpeg_audiowrite
  File "/Users/riders/.virtualenvs/videogrep/lib/python2.7/site-packages/moviepy/decorators.py", line 60, in requires_duration
    return f(clip, *a, **k)
  File "/Users/riders/.virtualenvs/videogrep/lib/python2.7/site-packages/moviepy/audio/io/ffmpeg_audiowriter.py", line 125, in ffmpeg_audiowrite
    writer.write_frames(sndarray)
  File "/Users/riders/.virtualenvs/videogrep/lib/python2.7/site-packages/moviepy/audio/io/ffmpeg_audiowriter.py", line 78, in write_frames
    self.proc.stdin.write(frames_array.tostring())
IOError: [Errno 32] Broken pipe
ghost commented 10 years ago

This error means that your FFMPEG build isn't configured to support the audio codec you want.

The best compilation guide I've found is here https://trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu

If you do ffmpeg -version do you see --enable-libvorbis anywhere?

Zulko commented 10 years ago

Hi all, it seems that the sound being exported as .ogg causes a lot of trouble, what would you think of using a less exotic format like .mp3 ? To do so you just change line 94 in videogrep.py to force the audio codec:

final_clip.to_videofile(outputfile, codec='libx264', audio_codec='libmp3lame')

And you must do the same at line 117:

video.to_videofile(outputfile, audio_codec='libmp3lame')

Could the people who had trouble with ogg/vorbis tell me if it works better like this ? If yes, that could be a fix to merge.

antiboredom commented 10 years ago

Thanks @Zulko! @shawnr let me know if this works for you & if so I'll make the change.

shawnr commented 10 years ago

Yes, thanks @Zulko ! I'm testing now, and it definitely improves the situation. Videogrep completes putting together the supercut.mp4 file and it looks great -- but no audio track!

What's interesting is that the MP3s that get created are fine (they sound quite clear) and the temp MP4s look great, but the combination lacks any audio track at all. I'm wondering if the edit for line 117 that @Zulko mentioned above is totally accurate? Or is it once again my installation of FFMpeg? Here's what FFMpeg tells me about version:

$ ffmpeg -version
ffmpeg version 2.2.4
built on Jun 23 2014 16:31:17 with Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
configuration: --prefix=/usr/local/Cellar/ffmpeg/2.2.4 --enable-shared --enable-pthreads --enable-gpl --enable-version3 --enable-nonfree --enable-hardcoded-tables --enable-avresample --enable-vda --cc=clang --host-cflags= --host-ldflags= --enable-libx264 --enable-libfaac --enable-libmp3lame --enable-libxvid
libavutil      52. 66.100 / 52. 66.100
libavcodec     55. 52.102 / 55. 52.102
libavformat    55. 33.100 / 55. 33.100
libavdevice    55. 10.100 / 55. 10.100
libavfilter     4.  2.100 /  4.  2.100
libavresample   1.  2.  0 /  1.  2.  0
libswscale      2.  5.102 /  2.  5.102
libswresample   0. 18.100 /  0. 18.100
libpostproc    52.  3.100 / 52.  3.100

I'm using the same files I linked to in my example above. I will continue to work on things. Thanks to you all for sticking with this.

antiboredom commented 10 years ago

Hey @shawnr what are you using to play the the videos?

Zulko commented 10 years ago

@shawnr When you say "the MP3s" I guess that you have the mp3s of the different parts, but do you also have the mp3 of the final concatenation ?

Could you edit line 117 to:

video.to_videofile(outputfile, audio_codec='libmp3lame', write_logfile=True)

And paste the content of the audio log file (should be named something like output+".mp3.log").

shawnr commented 10 years ago

@antiboredom @Zulko Thanks to you both for your efforts here. I believe the problem I saw yesterday was that I just loaded them up in Quicktime. Playing them in VLC, I can hear and see the video perfectly after the change to libmp3lame. All the MP3s that are generated are fine, even the final one, and the final video actually does have the audio track in it. Here is the content of the logfile:

ffmpeg version 2.2.4 Copyright (c) 2000-2014 the FFmpeg developers
  built on Jun 23 2014 16:31:17 with Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/2.2.4 --enable-shared --enable-pthreads --enable-gpl --enable-version3 --enable-nonfree --enable-hardcoded-tables --enable-avresample --enable-vda --cc=clang --host-cflags= --host-ldflags= --enable-libx264 --enable-libfaac --enable-libmp3lame --enable-libxvid
  libavutil      52. 66.100 / 52. 66.100
  libavcodec     55. 52.102 / 55. 52.102
  libavformat    55. 33.100 / 55. 33.100
  libavdevice    55. 10.100 / 55. 10.100
  libavfilter     4.  2.100 /  4.  2.100
  libavresample   1.  2.  0 /  1.  2.  0
  libswscale      2.  5.102 /  2.  5.102
  libswresample   0. 18.100 /  0. 18.100
  libpostproc    52.  3.100 / 52.  3.100
Guessed Channel Layout for  Input Stream #0.0 : stereo
Input #0, s32le, from 'pipe:':
  Duration: N/A, bitrate: 2822 kb/s
    Stream #0:0: Audio: pcm_s32le, 44100 Hz, stereo, s32, 2822 kb/s
Output #0, mp3, to 'supercutTEMP_MPY_to_videofile_SOUND.mp3':
  Metadata:
    TSSE            : Lavf55.33.100
    Stream #0:0: Audio: mp3 (libmp3lame), 44100 Hz, stereo, s32p
Stream mapping:
  Stream #0:0 -> #0:0 (pcm_s32le -> libmp3lame)
size=     479kB time=00:00:30.59 bitrate= 128.2kbits/s    
size=     941kB time=00:01:00.21 bitrate= 128.1kbits/s    
size=    1406kB time=00:01:29.96 bitrate= 128.1kbits/s    
size=    1895kB time=00:02:01.20 bitrate= 128.0kbits/s    
size=    2362kB time=00:02:31.14 bitrate= 128.0kbits/s    
size=    2480kB time=00:02:38.66 bitrate= 128.0kbits/s    
video:0kB audio:2480kB subtitle:0 data:0 global headers:0kB muxing overhead 0.008940%
shawnr commented 10 years ago

@antiboredom @Zulko Just wanted to mention that I think the move to libmp3lame over ogg is a good idea just because ffmpeg is such a pain to get with ogg built in. A new update to the ffmpeg Homebrew recipe came out yesterday that implies support for building with all the different codecs (including ogg) seems to not actually make builds with those codecs supported. Maybe once that recipe is finalized the problem will be much easier to address on a Mac. Thanks again for all your help!

mplewis commented 10 years ago

Hey @Zulko, thanks for the help—forcing audio_codec='libmp3lame' did the trick.

Is there any possibility forcing the audio codec could make it into master as a command-line flag?

Zulko commented 10 years ago

In the last version of MoviePy the audio codec is libmp3lame by default. Just update MoviePy and it should work fine.

builtbylane commented 10 years ago

Yep. That did the trick. Thanks Zulco

djchou commented 10 years ago

Hi -- I just wanted to follow up with shawnr's issue with the audio not working when he played the supercut in quicktime. I had the exact same issue and while the supercut does work when running it in vlc / ffplay, i'm curious to see if there's a way to have the audio created / concatenated in a quicktime friendly format. I'm no expert but looking at the difference between audio that quicktime can play and audio that quicktime cannot play, it appears that the "aac" format works while the mp3 format does not (even though quicktime should support mp3...)