Moviepy: Problem with writing audio

Created on 22 Jun 2014  Â·  18Comments  Â·  Source: Zulko/moviepy

Hey there. I'm experiencing a problem when writing audio. If I call .to_videofile with audio=False, then there is no problem. My videopy version number is 0.2.1.8.02.

The video I'm working with is here:
https://www.dropbox.com/s/iepmbyieferjbay/test.MP4

The code is here:

clip = mp.VideoFileClip('../snowsports/test.MP4').subclip(220,224)
clip.to_videofile("test-short.MP4")

There error message is:

MoviePy: building video file test-short.MP4
----------------------------------------
Writing audio in test-shortTEMP_MPY_to_videofile_SOUND.ogg
Done writing Audio in test-shortTEMP_MPY_to_videofile_SOUND.ogg !

Writing video into test-shortTEMP_MPY_to_videofile.MP4
Done writing video in test-shortTEMP_MPY_to_videofile.MP4 !

Now merging video and audio:

MoviePy Running:
>>> ffmpeg -y -i test-shortTEMP_MPY_to_videofile_SOUND.ogg -i test-shortTEMP_MPY_to_videofile.MP4 -vcodec copy -acodec copy test-short.MP4
MoviePy: WARNING !
   The following command returned an error:
ffmpeg version 2.2.3 Copyright (c) 2000-2014 the FFmpeg developers
  built on Jun  3 2014 06:47:04 with Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/2.2.3 --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
test-shortTEMP_MPY_to_videofile_SOUND.ogg: No such file or directory
Conversion failed!
---------------------------------------------------------------------------
IOError                                   Traceback (most recent call last)
<ipython-input-13-b6f4d3578283> in <module>()
      1 clip = mp.VideoFileClip('../snowsports/test.MP4').subclip(220,224)
----> 2 clip.to_videofile("test-short.MP4")

/usr/local/lib/python2.7/site-packages/moviepy/video/VideoClip.pyc in to_videofile(self, filename, fps, codec, bitrate, audio, audio_fps, audio_nbytes, audio_codec, audio_bitrate, audio_bufsize, temp_audiofile, rewrite_audio, remove_temp, write_logfile, para, verbose)
    286             verbose_print("\n\nNow merging video and audio:\n")
    287             ffmpeg_merge_video_audio(videofile,temp_audiofile,
--> 288                                   filename, ffmpeg_output=True)
    289 
    290             if remove_temp:

/usr/local/lib/python2.7/site-packages/moviepy/video/io/ffmpeg_tools.pyc in ffmpeg_merge_video_audio(video, audio, output, vcodec, acodec, ffmpeg_output, verbose)
     49              "-vcodec", vcodec, "-acodec", acodec, output]
     50 
---> 51     subprocess_call(cmd, verbose = verbose)
     52 
     53 

/usr/local/lib/python2.7/site-packages/moviepy/tools.pyc in subprocess_call(cmd, verbose, errorprint)
     33                     "   The following command returned an error:\n")
     34             sys_write_flush( err.decode('utf8'))
---> 35         raise IOError
     36     else:
     37         verboseprint( "\n... command successful.\n")

IOError: 

I've also tried explicitly specifying the codec as 'mpeg4'. I'm not sure if that makes any difference, but the result is the same.

Most helpful comment

This worked for me to get the file to play in Quicktime/iMovie:

clip.write_videofile(out_path, 
  codec='libx264', 
  audio_codec='aac', 
  temp_audiofile='temp-audio.m4a', 
  remove_temp=True
)

All 18 comments

The script you provided worked OK on my computer with the file you provided, using either Python2 or Python3.2.3. What Python version are you using ?

That's may be nasty to debug, because apparently on your computer everything goes smoothly until the last step: the temporary audio file seems to be created correctly, but then ffmpeg can't find it.

One possible explanation is that, for some reason, the temporary audio file is removed before ffmpeg can merge it with the video. This used to happen in Python3 (the piping works differently in Python3) but I believe it is now fixed.

One quick fix that could work would be to not delete the temporary files at all like this:

clip.to_videofile("test-short.mp4", remove_temp=False)

Could you try this and describe what happens ? Like, did it work, and if not, is there at least a temporary .ogg audio file ?

First, thanks for taking the time to help. I'm using python 2.7.7_1. The ffmpeg output reports the temp files as having been successfully created, but I can't find the .ogg file. I do, however, see the temp MP4 file.

It should be created in the same folder as your script. Have you tried with remove_temp=False ? Have you tried removing the - in the file name. Otherwise it would mean that the audio cannot be written, but in this case I expect the program to throw an error. What's your OS ?

Yes, with remove_temp=False the .MP4 temp file is still there, but not the
audio file. I'm on OS X 10.8.5.

On Mon, Jun 23, 2014 at 11:33 AM, Zulko [email protected] wrote:

It should be created in the same folder as your script. Have you tried
with remove_temp=False ? Have you tried removing the - in the file name.
Otherwise it would mean that the audio cannot be written, but in this case
I expect the program to throw an error. What's your OS ?

—
Reply to this email directly or view it on GitHub
https://github.com/Zulko/moviepy/issues/51#issuecomment-46883739.

Reid Oda
Ph.D. Candidate
Princeton University
858-349-2037
http://www.cs.princeton.edu/~roda

Really strange. Here are a few things I can think of.

Ask for logfiles when writing the video file:

clip.to_videofile("test-short.MP4", write_logfile=True)

This should create a file named test-shortTEMP_MPY_to_videofile_SOUND.ogg.log. If it does, please paste me the content. If it doesn't, try this instead:

clip.audio.to_audiofile("test-short.ogg", write_logfile=True)

If this generates a log file, paste it for me.

In any case, could you also try changing the output codec to mp3 ? Apparently .ogg format gives trouble to a lot of people. To do so, you just write

clip.to_videofile("test-short.mp4", audio_codec='libmp3lame')

Tell me if this works.

It does allow the compilation to occur, but there is no sound. This, at least, shows me that the problem lies with ffmpeg and not moviepy. Thanks for the help!

Here is that log file. I can see that it can't find the libvorbis audio codec. Homebrew reports that I have libvorbis-1.3.4 installed. I know that this is an ffmpeg thing, but do you know if there something I need to do to make sure it finds the library?

ffmpeg version 2.2.4 Copyright (c) 2000-2014 the FFmpeg developers
  built on Jun 23 2014 16:31:19 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
Unknown encoder 'libvorbis'
Conversion failed!

On the videogrep page they say to install ffmpeg with

brew install ffmpeg --with-libvpx --with-libvorbis

But you should give up with libvorbis and try to understand why it doesn't even work with mp3, that would be simpler. You said there is no sound: with what software are you playing it ? Could you try playing it with VLC and see if there is still no sound ?

Yes there is sound in VLC and if I play it in the browser. However, no
sound in quicktime, nor is there a volume slider.

On Wed, Jun 25, 2014 at 2:35 PM, Zulko [email protected] wrote:

On the videogrep page they say to install ffmpeg with

brew install ffmpeg --with-libvpx --with-libvorbis

But you should give up with libvorbis and try to understand why it doesn't
even work with mp3, that would be simpler. You said there is no sound: with
what software are you playing it ? Could you try playing it with VLC and
see if there is still no sound ?

—
Reply to this email directly or view it on GitHub
https://github.com/Zulko/moviepy/issues/51#issuecomment-47162074.

Reid Oda
Ph.D. Candidate
Princeton University
858-349-2037
http://www.cs.princeton.edu/~roda

Ok. To me this sounds more like a FFMPEG/Quicktime issue. On linux there is sound with all the most popular video players.

Does the build line

brew install ffmpeg --with-libvpx --with-libvorbis

allow you to make .ogg soundtracks ?

It does, however, when played in quicktime they are also silent. Thanks for
all the help. I realize it's not your responsibility, and I appreciate the
effort, since I'm relatively new to ffmeg and video processing in general.

On Thu, Jun 26, 2014 at 10:55 PM, Zulko [email protected] wrote:

Ok. To me this sounds more like a FFMPEG/Quicktime issue. On linux there
is sound with all the most popular video players.

Does the build line

brew install ffmpeg --with-libvpx --with-libvorbis

allow you to make .ogg soundtracks ?

—
Reply to this email directly or view it on GitHub
https://github.com/Zulko/moviepy/issues/51#issuecomment-47310227.

Reid Oda
Ph.D. Candidate
Princeton University
858-349-2037
http://www.cs.princeton.edu/~roda

@brownerthanu I changed the default encoding to mp3, now it should work properly. If you have the occasion to try again, tell me if it works, so that I can close the issue

Hi Zulko. As it turns out it was not due to ogg vs mp3. I think there is a bug in ffmpeg...at least there seems to be. One of my friends is experiencing the same problem. No amount of building from source with fancy options fixed it for me, so my solution was to write to .mov and then use ffmpeg to copy to .mp4. That works.

This worked for me to get the file to play in Quicktime/iMovie:

clip.write_videofile(out_path, 
  codec='libx264', 
  audio_codec='aac', 
  temp_audiofile='temp-audio.m4a', 
  remove_temp=True
)

thanks jarib. you save my life :)

thanks jarib!

+1 Thanks! I was super confused about why I had no audio in Quicktime (and therefore QuickLook).

ta

Was this page helpful?
0 / 5 - 0 ratings

Related issues

PyB1l picture PyB1l  Â·  3Comments

keikoro picture keikoro  Â·  4Comments

bilel picture bilel  Â·  4Comments

RahulPrasad picture RahulPrasad  Â·  4Comments

skizzy picture skizzy  Â·  3Comments