The cause of
One day, see a marketing number of video said to do video daily gold, the general idea is to use software to identify the article novel, with some pictures into their own video. After reading it, a thought came to my mind that I could too, so I had this attempt.
plan
First of all, in terms of text, I chose [aggregated data joke interface] (www.juhe.cn/docs/api/id… Secondly, I need to convert text to audio. I choose [Voice Synthesis of IFlycom], because it has Windows offline VERSION SDK, so I can use it according to needs after modifying some. Finally, I use [FFmpeg] to convert audio to video, adding cover image and rolling subtitles for the video
Automatic video generation using scripts
Prepare joke texts
Copy the joke text and save it in a text
Download the IFlytek Voice Synthesis offline package
- Need to register and authenticate after the console download, download address
- The solution in VS Run Samples is used in the installation documentation (using VS2010), and if the project fails to load, upgrade the framework version of the project under upgrade: right-click the project -> Redirect the project -> install the latest version.
- In order to be able to call the SDK directly in the future, you can change this example as needed, changing the input parameter and exit here.
After regenerating, find the exe in the outer bin directory and use tts_offline_sample hello.wav “hello word” to call the program to synthesize the text audio to the specified path.
Copy the bin and libs directories to where you need them, or add the bin directory to your environment variables, and you can use them anywhere
Download ffmpeg
Now that you’ve used TTS to generate the audio file, you can use FFmpeg to convert the audio to video and generate subtitles to get a fresh video
Generate video
Get a copyrighted image from Pixabay and save it as cover.jpg for the cover. Run the following command to generate the video
ffmpeg -y -loop 1 -i cover.jpg -i hello.wav -c:v libx264 -c:a aac -b:a 192k -shortest hello.mp4
Generate scrolling captions
Create a content. TXT file, write the content “Hello Word, hello world” x and Y as coordinates, adjust according to need, fontfile set to Chinese font, do not set, Chinese will be garble, execute the command to get a video with rolling subtitles.
ffmpeg -y -i hello.mp4 -vf "drawtext=fontfile=C\\:/Windows/Fonts/msyh.ttc:fontcolor=ffcc00:fontsize=40:shadowx=2:shadowy=2:textfile=./content.txt:r eload=1:y=h/5:x=w-(mod(8*n\,w+tw)-tw/100)" -c:a copy hellofull.mp4
Notice the path need special handling vf parameters, Replace (” \ \ “, “/”). The Replace (” : “, “\ \”)
From there, the technical points have been explained, and some optimization and scripting combinations follow.
Picture to video
Note: / is the image directory with img_ as the prefix,% 03D as the format (format output %0nd in C language), the left complement 0 with less than 3 bits of length, that is, img_001,img_002, and the images should be ordered from the sequence number of the first image. The suffix of the picture should be consistent with its format. PNG, imG_002. PNG, imG_003.png… -r 0.500, 1/0.500= 2 seconds a picture shows -y overwrite the generated file
Ffmpeg-y -r 0.500 -i./img_% 03D.png
Full script
set coverPath=F:\\Project\\JokeBox\\cover.jpg set contentPath=F:\\Project\\JokeBox\\content.txt set /P Set videoName contentPath jokeContent = < % % % = video date: date: ~ 0, 4% % % % ~ 5, 2 date: ~ 8, 2%0% time: ~ 1, 1 time: % % % % ~ 3, 2 time: % ~ 6, 2 set outDic=F:\\Project\\JokeBox\\output\\%videoName% set voicePath=%outDic%\\voice.wav set videoPath=%outDic%\\video.mp4 set OutDic videoFullPath = % % \ \ % daily jokes - date: ~ 5, 2 - % % date: ~ 8, 2%. Mp4 set srtPath = F \ \ : / Project/JokeBox/zimu. TXT echo audio: %voicePath% echo Video: %videoPath% echo subtitles: %srtPath% echo text: %jokeContent% echo Create output directory mkdir %outDic% echo generate audio tts_offline_sample.exe "%voicePath%" "%jokeContent%" echo generate video ffmpeg -loop 1 -i %coverPath% -I %voicePath% -c:v libx264 -c:a aac -b:a 192K -shortest %videoPath% echo Generates subtitle ffmpeg-y -i %videoPath% -vf "drawtext=fontfile=C\\:/Windows/Fonts/msyh.ttc:fontcolor=ffcc00:fontsize=40:shadowx=2:shadowy=2:textfile=%srtPath%:reloa D =1:y=h/5:x=w-(mod(8*n\,w+tw) -TW /100)" -c:a copy %videoFullPath% copy %contentPath% %outDic% echo %voicePath:\\=\% del %videoPath:\\=\% pauseCopy the code
reference
- FFmpeg
- Iflytek offline speech synthesis