Ubuntu Forums ubuntu.com - launchpad.net - ubuntu help  

Go Back   Ubuntu Forums > The Ubuntu Forum Community > Main Support Categories > Multimedia & Video
Register Reset Password Forum Help Forum Council Search Today's Posts Mark Forums Read

Multimedia & Video
Have multimedia question? ATI, Nvidia, Sound cards. Just ask here.

 
Thread Tools Display Modes
Old August 19th, 2009   #1
cudjoe
Just Give Me the Beans!
 
Join Date: Apr 2005
Location: Toulouse,France
Beans: 81
Ubuntu 9.04 Jaunty Jackalope
[SOLVED] Convert mp3 to avi

Hi,

I need to convert a mp3 to avi because my Upnp player (freebox-tv) does not read mp3 but supports well xvid videos. I thus will transcode my music as blank videos.

With ffmpeg, the conversion is easy :
Code:
ffmpeg -i input.mp3 -vn -acodec copy -f avi output.avi
Problem : the output does not have any video stream.

I made a small blank video (attached as txt), 1 black frame.

With mencoder, I managed to have both video and audio :
Code:
mencoder black.avi -o output.avi -ovc xvid -oac copy -audiofile input.mp3
Problem : the output file is 1 frame long.

How can I specify to loop over the blank video accordingly to audio stream length ?

Thank you guyz for any ideas!
Attached Files
File Type: txt black.avi.txt (8.7 KB, 0 views)

Last edited by cudjoe; August 21st, 2009 at 04:06 AM.. Reason: solved
cudjoe is offline   Reply With Quote
Old August 20th, 2009   #2
FakeOutdoorsman
Chocolate Ubuntu Mocha Blend
 
FakeOutdoorsman's Avatar
 
Join Date: Sep 2006
Location: Alaska
Beans: 1,746
Re: Convert mp3 to avi

Create blank black image (convert is part of the imagemagick package):
Code:
convert -size 320x240 xc:black black.png
Or create black image with white text:
Code:
convert -size 320x240 xc:black -fill white -draw "gravity Center text 0,0 'The Pogues'" black.png
Combine audio and black video:
Code:
ffmpeg -loop_input -i black.png -i fairytale_of_new_york.mp3 -shortest -acodec copy output.avi
You will need to enable restricted encoders in FFmpeg to encode with mpeg4:
HOWTO: Easily enable MP3, MPEG4, AAC, and other restricted encoding in FFmpeg

Last edited by FakeOutdoorsman; 2 Weeks Ago at 02:13 AM.. Reason: now use -shortest instead of getting duration and using -t
FakeOutdoorsman is offline   Reply With Quote
Old August 20th, 2009   #3
Locutus_of_Borg
Skinny Soy Caramel Ubuntu
 
Join Date: Aug 2007
Beans: 684
Re: Convert mp3 to avi

save this python script, name it script.py (or whatever you want)
Code:
import os, sys                                           

fps = 30

if len(sys.argv) != 4:
    print "Usage: makevid.py imagefile audiofile seconds"
    sys.exit(1)                                          

secs = int(sys.argv[3])
if secs < 0:
    print "Please pass a positive amount of seconds"
    sys.exit(1)
secs = 1 / float(secs)

filesexist = True
for each_file in sys.argv[1:2]:
    if not os.path.exists(each_file):
        print "%s doesn't exist" % each_file
        filesexist = False
if not filesexist: sys.exit(1)

mencoder_cmd = ('mencoder "mf://%s" -mf fps=%f -vf harddup -ovc lavc'\
    ' -lavcopts vbitrate=100 -audiofile "%s" -oac copy -ofps %d'\
    ' -o output.avi') % (sys.argv[1], secs, sys.argv[2], fps)

print "Starting mencoder"
os.system(mencoder_cmd)
print "Video is complete, output written to output.avi"
open a terminal execute the script with
Code:
python script.py [imagefile] [audiofile] [length in seconds]
this will create a video of a still image that will play your audio for however many seconds you specify
Locutus_of_Borg is offline   Reply With Quote
Old August 21st, 2009   #4
cudjoe
Just Give Me the Beans!
 
Join Date: Apr 2005
Location: Toulouse,France
Beans: 81
Ubuntu 9.04 Jaunty Jackalope
Talking Re: Convert mp3 to avi

Thanks Locutus_of_Borg.
With the information of Fakeoutdoorsman, I made this small shell script (ffmpeg instead of mencoder) :

Code:
input=$1
output=$2

background=/tmp/bng-black.jpg
convert -size 320x240 xc:black $background
duration=`ffmpeg -i $input 2>&1 | grep Duration | cut -f1 -d, | cut -f2,3,4,5 -d:`
ffmpeg -loop_input -i $background -i $input -vcodec mpeg4 -acodec copy -t $duration $output
Thank you very much for your assistance !
cudjoe is offline   Reply With Quote
Old 2 Weeks Ago   #5
Kilarin
Just Give Me the Beans!
 
Join Date: Jun 2007
Beans: 71
Re: Convert mp3 to avi

Thanks! I just used your shell script, replacing the background.jpg with an image I wanted to use, and converted an mp3 file into an avi! It was EASY with the script. thank you VERY much!
Kilarin is offline   Reply With Quote
Old 2 Weeks Ago   #6
FakeOutdoorsman
Chocolate Ubuntu Mocha Blend
 
FakeOutdoorsman's Avatar
 
Join Date: Sep 2006
Location: Alaska
Beans: 1,746
Re: Convert mp3 to avi

I updated my first post to now use the -shortest option which will automatically finish encoding the video when the music ends. Much easier than getting the audio duration and then using that number to determine the encoding length. So now cudjoe's script can look like this:
Code:
#!/bin/bash
# Usage: mp32avi.sh input.mp3 output.avi
input=$1
output=$2

background=/tmp/bng-black.jpg
convert -size 320x240 xc:black $background
ffmpeg -loop_input -i $background -i $input -vcodec mpeg4 -acodec copy -shortest -qscale 5 $output
What would be interesting is to get the artist and/or title tag from the audio metadata and feed that to the "create black image with white text" (post #2) example for convert.

Update: I also added -qscale 5 to cudjoe's script so the output quality should be better for more complicated backgrounds other than just a solid color.

Last edited by FakeOutdoorsman; 2 Weeks Ago at 02:34 AM..
FakeOutdoorsman is offline   Reply With Quote
Old 2 Weeks Ago   #7
Lars Noodén
Dark Roasted Ubuntu
 
Lars Noodén's Avatar
 
Join Date: Sep 2006
Beans: 1,102
Kubuntu 9.10 Karmic Koala
transcoding or wrapping

@ FakeOutdoorsman : That looks useful. Sorry for a dumb question: Does that method wrap the MP3s as AVI as is, or does it transcode them and reduce the sound quality further?
__________________
Free as in Freedom, even for Ubuntu. Quality standards for Ubuntu.
Lars Noodén is offline   Reply With Quote
Old 2 Weeks Ago   #8
FakeOutdoorsman
Chocolate Ubuntu Mocha Blend
 
FakeOutdoorsman's Avatar
 
Join Date: Sep 2006
Location: Alaska
Beans: 1,746
Re: transcoding or wrapping

Quote:
Originally Posted by Lars Noodén View Post
@ FakeOutdoorsman : That looks useful. Sorry for a dumb question: Does that method wrap the MP3s as AVI as is, or does it transcode them and reduce the sound quality further?
There is no re-encoding of the audio in the script because it is using the -acodec copy option. It is basically copying the audio from the MP3 file and pasting it into an AVI file.

There is a -vcodec copy option as well which is quite useful. Both of these options are good for trimming a video without re-encoding or adding/removing audio/video to a file without any need to re-encode.
FakeOutdoorsman is offline   Reply With Quote
Old 2 Weeks Ago   #9
FakeOutdoorsman
Chocolate Ubuntu Mocha Blend
 
FakeOutdoorsman's Avatar
 
Join Date: Sep 2006
Location: Alaska
Beans: 1,746
Re: transcoding or wrapping

Here's a new version:
  • add the title to the black background
  • use gif because it is faster to decode than png apparently
  • set frame rate
  • add requirements
  • make background randomly named and then delete it
Code:
#!/bin/bash
# Requires: imagemagick, ffmpeg
# Usage: ./mp32avi.sh input.mp3 output.avi

background=/tmp/$RANDOM.gif

title=`ffmpeg -i $1 2>&1 | grep TIT2 | cut -d: -f 2`

convert -size 320x240 xc:black -fill white -draw "gravity Center text 0,0 '$title'" $background

ffmpeg -loop_input -r ntsc -i $background -i $1 -acodec copy -shortest -qscale 5 $2

rm $background

exit

Last edited by FakeOutdoorsman; 2 Weeks Ago at 04:32 PM..
FakeOutdoorsman is offline   Reply With Quote
Old 2 Weeks Ago   #10
Kilarin
Just Give Me the Beans!
 
Join Date: Jun 2007
Beans: 71
Re: Convert mp3 to avi

this scrip creates the avi just fine, but the background is plain black. For some reason its not grabbing the mp3 title info.

I made certain the title was populated in both v1 and v2 tags. Am I missing some component?

thanks again!
Kilarin is offline   Reply With Quote

Bookmarks

Tags
avi, black video, conversion, ffmpeg, mp3

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 08:14 AM.


vBulletin ©2000 - 2010, Jelsoft Enterprises Ltd. Ubuntu Logo, Ubuntu and Canonical © Canonical Ltd. Tango Icons © Tango Desktop Project. bilberry