Convert Mp4 to Mp3 with thumbnail

8

1

I'm able to convert a mp4 to a mp3 in ffmpeg, but I'm not sure how I would add in a thumbnail of the video.

Currently have this to convert from mp4 to mp3

ffmpeg -i video.mp4 -acodec libmp3lame -metadata title=video -b:a 256 -f input.mp3

Would I then have to run that through ffmpeg again with this command?

ffmpeg -i input.mp3 -i cover.jpg -map_metadata 0 -map 0 -map 1 output.mp3

or is there any way to use complex filters to achive this is one run?

I know that this works for FLV but it does not seem to work for mp4

ffmpeg -i video.flv -i cover.jpg -map 0:a -map 1 -c:v copy -metadata title=video -b:a 256k out.mp3

nadermx

Posted 2017-02-26T01:27:48.007

Reputation: 387

Do you really want the convert to automatically set the thumbnail? Because otherwise you could change it manually but I'm not sure if you are after that. If you can do it with FLV but not with MP4 I would just make a script that runs both commands. – RamonRobben – 2017-02-28T22:04:28.200

@RamonRobben I'm trying to avoid having to run ffmpeg twice – nadermx – 2017-02-28T22:22:56.957

you could try something like this ffmpeg -i in.mp4 -i test.png -map 0:0 -map 1:0 -c copy -id3v2_version 3 -metadata:s:v title="Album cover" -metadata:s:v comment="Cover (Front)" out.mp3 I'm downloading ffmpeg myself to start testing with it. – RamonRobben – 2017-02-28T22:29:05.247

Answers

3

Better answer, with explanation, incorporating smart suggestions from slhck and Mulvya, stripped of non-essential parameters.

ffmpeg -i video.mp4 -i cover.jpg -acodec libmp3lame -b:a 256k -c:v copy -map 0:a:0 -map 1:v:0 output.mp3

ffmpeg audio/video manupulation tool: already selected by the OP, it is particularly fit for this job.

-i video.mp4 first input file: a video with some audio that we want to extract.

-i cover.jpg second input file, an image that we want displayed when we play the resulting audio file.

-acodec libmp3lame we want to create an .mp3 file, using the LAME encoder.

-b:a 256k this sets the bitrate for the audio track to a constant 256Kb/s. A smarter option would be to encode with a variable bitrate, specifying the quality parameter: -q:a 0 asks for the maximum quality, while -q:a 4 often represents a good compromise between perceived audio quality and bitrate (and hence, file size).

-c:v copy this indicates that the video stream (the .jpg image) is not to be reencoded but must be copied as it is. This is useful to avoid unnecessary processing and potential quality loss when reencoding to a lossy format. In our case, without this parameter the image would be decoded from .jpg and encoded to .png, which apparently is the native format that gets chosen by default. This would not represent a loss in quality since .png is lossles, but more often than not will cause the file size to increase, due to the fact that .jpg (being lossy) generally offers a better compression rate.

-map 0:a:0 this selects the stream to be used from the first (0:) input file: it has to be the first (:0) audio (a) stream it contains.

-map 1:v:0 this selects the first (:0) video (v) stream from the second (1:) input file.

output.mp3 the name of this parameter was cunningly chosen in order to already suggest that this must be the name to be given to the output audio (.mp3) file with incorporated image that we want to create.

Original answer

This creates an audio (.mp3) file with a static image by putting together a video (.mp4) and a picture (.jpg) from my wedding:

ffmpeg -i video.mp4 -i cover.jpg -acodec libmp3lame -metadata title=video -b:a 256k -map_metadata 0 -map 0:1 -map 1 output.mp3

See ffmpeg documentation on selecting input streams with -map, which is the relevant option here.

I first ran the command with simply -map 0 -map 1 and got this info:

Stream mapping:
  Stream #0:0 -> #0:0 (h264 (native) -> png (native))
  Stream #0:1 -> #0:1 (aac (native) -> mp3 (libmp3lame))
  Stream #1:0 -> #0:2 (mjpeg (native) -> png (native))

Thus I knew I wanted stream 1 (aac audio) from the file which has an index number of 0, i.e. the video file; therefore it had to be explicitly selected with -map 0:1 -map 1.

simlev

Posted 2017-02-26T01:27:48.007

Reputation: 3 184

I've tried this, yet it gives me Stream map '0:1' matches no streams. To ignore this, add a trailing '?' to the map. – nadermx – 2017-03-02T22:00:08.387

Run it with ´-map 0 -map 1´ and see what the available streams are. – simlev – 2017-03-02T22:08:39.080

Stream mapping: Stream #0:0 -> #0:0 (aac (native) -> mp3 (libmp3lame)) Stream #1:0 -> #0:1 (mjpeg (native) -> png (native)) – nadermx – 2017-03-02T22:12:32.810

Well, then in your case it should have worked already, since you have only one stream in each file it is ´-map 0:0 -map 1:0´ or simply just ´-map 0 -map 1´. – simlev – 2017-03-02T22:15:59.710

This is the url I'm running, https://gist.github.com/nadermx/cba3942560af5f5f23df871d1e018d49

– nadermx – 2017-03-02T23:03:26.740

The link probably expired because I get 403 Forbidden (access denied), so I can't test your specific file; I did, however, put online my video and picture and ffmpeg ran successfully with urls instead of local files. – simlev – 2017-03-03T08:19:24.563

1@nadermx You can do -map 0:a:0 -map 1:v:0 to select the first audio and video streams of the first and second file, respectively. – slhck – 2017-03-03T08:28:45.593

@slhck Good tip, this way he doesn't have to check the stream number; unless there are more than one audio streams in the video file, of course. – simlev – 2017-03-03T08:34:02.207

1Avoid encoding the image input, especially with JPG. -c:v copy. – Gyan – 2017-03-03T11:52:39.913

@Mulvya Right: quality should not suffer when transcoding to png, but the file size is likely to increase. Plus, why reencode when there's no need to? I must confess I tried to provide a solution that would alter the original parameters as little as possible. – simlev – 2017-03-03T15:22:40.680

For some reason no matter the mapping I try this isn't working. Even with -c:v copy as well. – nadermx – 2017-03-04T19:04:48.223

@slhck when you say do the mapping that way, should I take out the -map_metadata? – nadermx – 2017-03-04T19:08:37.830

Transitioning to troubleshooting mode: you should first post the exact ffmpeg command and output, then download both files (and try again) then upload them somewhere and provide non-fast-expiring links. Also try with a different video and image. – simlev – 2017-03-04T19:51:44.810

2

you can make use of vlc media player -

Step 1: Open the VLC and select “Media”. Click on the convert/save.

Step 2: On the next window that appears, select “add” and choose the video file (MP4) to be converted.

Step 3: Next, click down arrow on the “Convert/Save” then click “Convert”.

Step 4: On the next window that appears, make sure the “Convert” button is selected. Proceed to choose the type of audio file you wish to create. In our case its MP3 file we are converting to.

Step 5: Choose an output destination, and then press “Start”. The VLC will then commence converting.

for adding thumbnail you can refer this link (explained very well).

AjaiVeer Singh Sandhu

Posted 2017-02-26T01:27:48.007

Reputation: 104