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

好久不见. 提交于 2021-01-07 01:06:57

问题


everybody here! So basically this is what I want to achieve:

I have a muted video about 3 minutes long. I have a list of audio tracks in mp3 format (40 songs in a folder with duration 2 to 6 mins each one)

I want this video to play cycled automatically taking songs from playlist and injecting them to the video one by one. Every time a song finishes the next one from the list should start playing at the moment. Video continues playing and doesn't care duration of tracks.

I consider it as the first step on the way to broadcast radio with a video background on youtube in 24/7 mode with ability to put additional tracks to playlist without need to stop translation.

My problem is that I'm new in FFmpeg and I would appreciate any suggestions regarding which FFMpeg topic to start investigate with in order to achieve my goal


回答1:


Use the concat demuxer

You can do live updates to the playlist for the concat demuxer, but each audio file must have the same attributes, the same number of streams, and all be the same format.

  1. Create input.txt containing:

    ffconcat version 1.0
    file 'audio1.mp3'
    file 'audio2.mp3'
    file 'audio3.mp3'
    file 'audio40.mp3'
    

    All file names must be "safe" or it will fail with Unsafe file name. Basically no special characters in file names and only use absolute paths. See concat demuxer for more info.

  2. Run ffmpeg to stream to YouTube:

    ffmpeg -re -framerate 10 -loop 1 -i image.jpg -re -f concat -i input.txt -map 0:v -map 1:a -c:v libx264 -tune stillimage -vf format=yuv420p -c:a aac -g 20 -b:v 2000k -maxrate 2000k -bufsize 8000k -f flv rtmp://youtube
    
  3. When you are ready to add new songs make temp.txt containing:

    ffconcat version 1.0
    file 'audio41.mp3'
    file 'audio42.mp3'
    file 'audio43.mp3'
    
  4. Replace input.txt atomically:

    mv temp.txt input.txt
    

See FFmpeg Wiki: Concatenate for lots more info.

If your audio files are not the same

The files listed in input.txt must all have the same:

  • Format (AAC, MP3, etc, but not mixed)
  • Sample rate (48000, 44100, etc)
  • Number of channels (mono, stereo, etc).

If they vary then you will have to pre-process them before adding them to the playlist. Bash example conforming each audio to stereo (-ac 2) with 44100 sample rate (-ar 44100) and save as AAC format in M4A container:

mkdir conformed
for f in *.mp3; do ffmpeg -i "$f" -map 0:a -ac 2 -ar 44100 -c:a aac "conformed/${f%.*}.m4a"; done
  • Outputting to AAC is recommended for streaming to YouTube.

  • If you do this then you can avoid re-encoding the audio in the ffmpeg command to YouTube. Just change -c:a aac to -c:a copy in step #2: Run ffmpeg to stream to YouTube.



来源:https://stackoverflow.com/questions/65147086/how-to-make-ffmpeg-automatically-inject-mp3-audio-tracks-in-the-single-cycled-mu

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