How to take metadata from .mp3 file and put it to a video as a text using FFmpeg?

百般思念 提交于 2021-01-29 11:23:01

问题


In my previously opened topic:

How to make FFmpeg automatically inject mp3 audio tracks in the single cycled muted video

I've got detailed explanation from @llogan how to broadcast looped short muted video on youtube automatically injecting audio tracks in it without interrupting a translation.

I plan to enhance the flow and the next question I faced with is how to dynamically put an additional text to the broadcast.

Prerequisites:

  1. youtube broadcast is up and running by ffmpeg
  2. short 3 min video is paying in infinity loop
  3. audio tracks from playlist are automatically taken by "ffmpeg concat" and injected in the video one by one

this is a basic command to start translation:

ffmpeg -re -fflags +genpts -stream_loop -1 -i video.mp4 -re -f concat -i input.txt -map 0:v -map 1:a -c:v libx264 -tune stillimage -vf format=yuv420p -c:a copy -g 20 -b:v 2000k -maxrate 2000k -bufsize 8000k -f flv rtmp://a.rtmp.youtube.com/live2/my-key

Improvements I want to bring

  1. I plan to store some metadata in audio files (basically it's an artist name and a song name)
  2. At the moment a particular song starts playing artist/song name should be taken from metadata and displayed on the video as text during the whole song is playing.
  3. When the current song finishes and a new one starts playing the previous artist/song text should be replaced with the new one etc

My question is how to properly take metadata and add it to the existing broadcast config using ffmpeg?


回答1:


This is a fairly broad question and I don't have a complete solution. But I can provide a partial answer containing several commands that you can use to help implement a solution.

Update text on video on demand

See Can you insert text from a file in real time with ffmpeg streaming?

Get title & artist metadata

With ffprobe:

ffprobe -v error -show_entries format_tags=title -of default=nw=1:nk=1 input.mp3
ffprobe -v error -show_entries format_tags=artist -of default=nw=1:nk=1 input.mp3

Or combined: format_tags=title,artist (note that title will display first, then artist, regardless of order in the command).

Get duration of a song

See How to get video duration in seconds?

What you need to figure out

The hard part is knowing when to update the file referenced in textfile in drawtext filter as shown in Update text on video on demand above.

Lazy solution

Pre-make a video per song including the title and artist info. Simple Bash example:

audio=input.mp3; ffmpeg -stream_loop -1 -i video.mp4 -i "$audio" -filter_complex "[0:v]scale=1280:720:force_original_aspect_ratio=increase,crop=1280:720,setsar=1,fps=25,drawtext=text='$(ffprobe -v error -show_entries format_tags=title,artist -of default=nw=1:nk=1 $audio)':fontsize=18:fontcolor=white:x=10:y=h-th-10,format=yuv420p[v]" -map "[v]" -map 1:a -c:v libx264 -c:a aac -ac 2 -ar 44100 -g 50 -b:v 2000k -maxrate 2000k -bufsize 6000k -shortest "${audio%.*}.mp4"

Now that you already did the encoding, and everything is conformed to the same attributes for proper concatenation, you can probably just stream copy your playlist to YouTube (but I didn't test):

ffmpeg -re -f concat -i input.txt -c copy -f flv rtmp://a.rtmp.youtube.com/live2/my-key

Refer to your previous question on how to dynamically update the playlist.

References:

  • FFmpeg Wiki: Streaming to YouTube
  • Resizing videos with ffmpeg to fit into specific size
  • How to concatenate videos in ffmpeg with different attributes?


来源:https://stackoverflow.com/questions/65161366/how-to-take-metadata-from-mp3-file-and-put-it-to-a-video-as-a-text-using-ffmpeg

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!